Skip to content

Instantly share code, notes, and snippets.

@sandys
Created February 27, 2018 09:27
Show Gist options
  • Save sandys/a04c684c8c4511dd3e022ab638ebb3a8 to your computer and use it in GitHub Desktop.
Save sandys/a04c684c8c4511dd3e022ab638ebb3a8 to your computer and use it in GitHub Desktop.
Converting set of images into PDF using Pillow
from PIL import Image
import requests
from io import BytesIO
from io import StringIO
url1 = "https://res.cloudinary.com/prestige-gifting/image/fetch/fl_progressive,q_95,e_sharpen:50,w_480/e_saturation:05/https://www.prestigeflowers.co.uk/images/NF4016-130116.jpg"
url2 = "https://static.pexels.com/photos/39517/rose-flower-blossom-bloom-39517.jpeg"
url3 = "https://i.pinimg.com/736x/f2/01/57/f201574c8705365b2c3e182953f765f2--blue-roses-blue-flowers.jpg"
im1 = Image.open(BytesIO(requests.get(url1).content)).convert("RGB")
cs1 = BytesIO()
im1.save(cs1,format="JPEG", dpi=(19.0,19.0), quality=60, optimize=True)
cs1.seek(0)
imcs1 = Image.open(cs1)
print(imcs1.info)
im2 = Image.open(BytesIO(requests.get(url2).content)).convert("RGB")
cs2 = BytesIO()
im2.save(cs2,format="JPEG", dpi=(19.0,19.0),quality=60, optimize=True)
cs2.seek(0)
imcs2 = Image.open(cs2)
im3 = Image.open(BytesIO(requests.get(url3).content)).convert("RGB")
cs3 = BytesIO()
im3.save(cs3,format="JPEG", dpi=(19.0,19.0),quality=60, optimize=True)
cs3.seek(0)
imcs3 = Image.open(cs3)
images = [imcs1, imcs2, imcs3]
#images = [im1, im2, im3]
images[0].save("out.pdf",quality=60,resolution=19.0, optimize=True, save_all=True, append_images=images[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment