from django.core.files.base import ContentFile from PIL import Image from StringIO import StringIO import requests def set_photo(self, url, filename): image_request_result = requests.get(url) image = Image.open(StringIO(image_request_result.content)) width, height = image.size max_size = [200, 200] if width > 200 or height > 200: image.thumbnail(max_size) image_io = StringIO() image.save(image_io, format='JPEG') self.photo.save(filename, ContentFile(image_io.getvalue()))