Skip to content

Instantly share code, notes, and snippets.

@chaddupuis
Created December 21, 2022 19:11
Embed
What would you like to do?
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 django.core.management.base 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=urllib.request.build_opener()
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')]
urllib.request.install_opener(opener)
logo_tmp.write(urlopen(s['Logo']).read())
logo_tmp.flush()
this_image = Image.open(logo_tmp)
cv_img = this_image.convert('RGB')
cv_img.show()
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.show()
else:
new_image = crop_max_square(cv_img).resize((200,200), Image.NEAREST)
new_image.show()
img_file_name = f'{yourobject.slug}-logo.jpg'
img_io = BytesIO()
new_image.save(img_io, format='JPEG', quality=90)
yourobject.logo_file = ContentFile(img_io.getvalue(), img_file_name)
yourobject.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment