Downsizing PNGs to PDF in Pillow

Viewed 42

My question is about tuning image quality & filesize in a PNG to PDF conversion.

I'm moving a reveal.js slide deck from my site into PDF. For a host of reasons I've ended up scraping the slides, thus producing 115 PNG files. Lotta slides. Pillow is handling the processing. 115 PNG into a single PDF.

Out of the box the final PDF comes in about 10M.

converted_pngs = []
seed_image = ''
i = 0
for png in pngs:
   img = Image.open(cwd + "/scrapes/" + png)
   rgb = img.convert('RGB')
   if i > 0: 
       converted_pngs.append(rgb)
   else:
       seed_image = rgb
   i=i+1

seed_image.save(pdf.fullpath, 'PDF', resolution=100, save_all=True, append_images=converted_pngs)

I've been through the some of the Pillow docs, and a few howto's. I have been toying with 3 levers in my efforts to shrink the PDF while also maintaining readability.

  • downsizing dimensions using resize()
  • setting the resample= parameter in resize()
  • changing the resolution= parameter in the call to size()

A sample of what I'm up to as of now:

RESIZE_FACTOR = 0.50
RESAMPLE_ALGO = Image.Resampling.LANCZOS
PDF_RES = 100

converted_pngs = []
seed_image = ''
i = 0
for png in pngs:
    img = Image.open(cwd + "/scrapes/" + png)
    w, h = img.size
    less_img = img.resize((int(w*RESIZE_FACTOR), int(h*RESIZE_FACTOR)), resample=RESAMPLE_ALGO)
    rgb = less_img.convert('RGB')
    if i > 0: 
        converted_pngs.append(rgb)
    else:
        seed_image = rgb
    i=i+1

seed_image.save(pdf.fullpath, 'PDF', resolution=PDF_RES, save_all=True, append_images=converted_pngs)

My results aren't great.

This process feels more an art than a science, making this question a bit soft. I guess I'm hoping for leads or general guidelines. Also,

  • are there other levers I should explore? besides the resizing and the resampling I'm doing?
  • are there other tools besides Pillow?
  • or should I go back to the PNG capture stage and look into any pyautogui settings before I do any more slogging in this processing stage?

Many Thanks,

1 Answers

There are different problems in using images in PDF than just file size and colour contents is one of the better means to reduce size but it is only good for text with simple graphs etc. Thus not suited to high colour images that are almost photographic, where jpeg would be better. Here are some sizes for one page and a sample why reducing colour can have a poor effect in some cases.

Number of Pages: 1
Page Size: 8.27 x 11.69 in (A4)

Title: PNG with nothing on it       File Size:  1.33 KB     (1,363 Bytes)
Title: A4 blank at 96 dpi           File Size:  5.55 KB     (5,684 Bytes)
Title: A4 blank at 300 dpi          File Size: 18.47 KB    (18,909 Bytes)
Title: A4 light density Tagged PDF  File Size: 87.29 KB    (89,390 Bytes)

Title: 33% busy 300dpi.pdf          File Size:  1.62 MB (1,701,148 Bytes)
Title: Full Colour 300dpi.pdf       File Size:  6.65 MB (6,976,364 Bytes)

Title: 256 colours at 300dpi        File Size:  3.43 MB (3,594,054 Bytes)
Title: 16 colour at 300 dpi         File Size:  1.43 MB (1,502,115 Bytes)
Title: 16 colour at 150 dpi         File Size:379.88 KB   (388,994 Bytes)

enter image description here

So depending on mix of content dropping resolution of images only to 100dpi these 3 pages are only just under 1 MB so your 115 pages in 10MB is very good.

File: python - Downsizing PNGs to PDF in Pillow - StackOverflow.pdf PDF Optimizations: Tagged PDF File Size: 889.29 KB (910,637 Bytes) Number of Pages: 3 Page Size: 11.69 x 8.26 in (A4)

enter image description here

Related