In conclusion, for those who, like me, want to connect it to Django FileSystemStorage : (What I am doing here is upload an image, resize it to two sizes and save both files.
utils.py
def resize_and_save(file): size = 1024, 1024 thumbnail_size = 300, 300 uploaded_file_url = getURLforFile(file, size, MEDIA_ROOT) uploaded_thumbnail_url = getURLforFile(file, thumbnail_size, THUMBNAIL_ROOT) return [uploaded_file_url, uploaded_thumbnail_url] def getURLforFile(file, size, location): img = Image.open(file) img.thumbnail(size, Image.ANTIALIAS) thumb_io = BytesIO() img.save(thumb_io, format='JPEG') thumb_file = InMemoryUploadedFile(thumb_io, None, file.name, 'image/jpeg', thumb_io.tell, None) fs = FileSystemStorage(location=location) filename = fs.save(file.name, thumb_file) return fs.url(filename)
In views.py
if request.FILES: fl, thumbnail = resize_and_save(request.FILES['avatar'])
mrj Jun 26 '19 at 21:43 2019-06-26 21:43
source share