Python PIL / Pillow - panel image to the desired size (e.g. A4)

I have a JPG / PNG / PDF image, and I would like to receive it on an A4 page, in the center, in PDF format (FYI: so that my end users can display / print it).

In any order:

  • enter image to fill A4 (with white)
  • convert to pdf

I can do im.save('filename.pdf', 'PDF', resolution=100.0) , to save the object Image to PDF, but I do not know how to perform a different task.

I would prefer to use Pillow, but other answers are welcome.

+5
source share
1 answer
 from PIL import Image im = Image.open(my_image_file) a4im = Image.new('RGB', (595, 842), # A4 at 72dpi (255, 255, 255)) # White a4im.paste(im, im.getbbox()) # Not centered, top-left corner a4im.save(outputfile, 'PDF', quality=100) 

This takes as a hypothesis that my_image_file has the same resolution, 72dpi.

+6
source

Source: https://habr.com/ru/post/1208228/


All Articles