Skip to content

Instantly share code, notes, and snippets.

@tigranbs
Created July 9, 2019 10:25
Show Gist options
  • Save tigranbs/929aeeb9d97b8d9f5958ffebe68c10b5 to your computer and use it in GitHub Desktop.
Save tigranbs/929aeeb9d97b8d9f5958ffebe68c10b5 to your computer and use it in GitHub Desktop.
Basic example of downloading remote image and making thumbnail with a fixed size
from PIL import Image
import requests
from io import BytesIO

url = "https://i.imgur.com/R3tZo.jpg"

def thumbnailBytes(imgURL):
  response = requests.get(url)
  img = Image.open(BytesIO(response.content))

  img.thumbnail([200, 200], Image.ANTIALIAS)
  imgByteArr = BytesIO()
  img.save(imgByteArr, format='JPEG')
  return imgByteArr.getvalue()

print(len(thumbnailBytes(url)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment