Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2022 19:11
Show Gist options
  • Save chaddupuis/9546f31be8acb72b727d824950ede836 to your computer and use it in GitHub Desktop.
Save chaddupuis/9546f31be8acb72b727d824950ede836 to your computer and use it in GitHub Desktop.
Django Image Scraping and Writing to S3 with NamedTemporaryFiles (no local copies)
from csv import DictReader
from django.core.files import File
import urllib.request
from urllib.request import urlopen
from tempfile import NamedTemporaryFile
from import BaseCommand
import time
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
# Partial snippet from a script that reads data (image urls) from a csv,
# gets the image, manipulates it, then save it to a model
# (which writes to s3) - boto3/django_storages.
def crop_center(pil_img, crop_width, crop_height):
img_width, img_height = pil_img.size
return pil_img.crop(((img_width - crop_width) // 2,
(img_height - crop_height) //2,
(img_width + crop_width) //2,
(img_height + crop_height) //2))
def crop_max_square(pil_img):
return crop_center(pil_img, min(pil_img.size), min(pil_img.size))
logo_tmp = NamedTemporaryFile(delete=True)
'''Need below to stop 403 errors from thinking we are scraping...'''
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
this_image =
cv_img = this_image.convert('RGB')
w,h = cv_img.size
if w == h:
'''square so just resize to 200x200 from whatever it is'''
new_image = cv_img.resize((200,200))
new_image = crop_max_square(cv_img).resize((200,200), Image.NEAREST)
img_file_name = f'{yourobject.slug}-logo.jpg'
img_io = BytesIO(), format='JPEG', quality=90)
yourobject.logo_file = ContentFile(img_io.getvalue(), img_file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment