Skip to content

Instantly share code, notes, and snippets.

@quietcricket
Created November 8, 2017 10:10
Show Gist options
  • Save quietcricket/8560a5735cd56c9e4a01cf848ed57226 to your computer and use it in GitHub Desktop.
Save quietcricket/8560a5735cd56c9e4a01cf848ed57226 to your computer and use it in GitHub Desktop.
Download image using requests and process it with PIL then upload to s3
def download_file(url):
r = requests.get(url)
if r.status_code != 200 or len(r.content) < 100:
raise 'Download Failed'
if re.search('\.png', url):
file_type = 'png'
elif re.search('\.gif', url):
file_type = 'gif'
else:
file_type = 'jpg'
filename = url[url.rfind('/'):]
# Upload original image
upload_s3(filename, r.content)
# Generate thumbnail if not gif
if file_type != 'gif':
img_file = StringIO(r.content)
pimg = Image.open(img_file)
pimg.thumbnail((250, 250))
# -------------------------------------------------------------
# Some images are not in RGB mode.
# Resizing them will give you a weired image
# -------------------------------------------------------------
if pimg.mode != 'RGB':
pimg = pimg.convert('RGB')
# -------------------------------------------------------------
# This is the tricky part. Took me a long time to find
# Need to save PIL image into a StringIO first
# then use getvalue() function to get the file like object
# -------------------------------------------------------------
thumbnail = StringIO()
if file_type == 'jpg':
file_type = 'jpeg'
pimg.save(thumbnail, file_type.upper())
# Upload thumbnail
upload_s3('thumbnail/' + filename, thumbnail.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment