Hello, I'm working on a Django project that need to upload images and there's limit on the image file size(like 5M). I need to compress the image if it is larger than the upper limit.
And my problem is
how can i return the compressed image data just like the format of file.read()?Here's how I did:
(1) get the image as a file object from the Django request.FILE
(2) get file_data, which is to return, from file.read(). and compare the length of data.
(3) Compress the image, using library Image, thumbnail function.
(4) Here comes my problem, How can return the compressed data with the format like file.read
import Image
def upload(request):
"""
return: file_data <- file.read()
"""
file = request.FILE['file'] #get the image as file object
file_data = file.read() #read the file
file_size = len(file_data) #get the file size of the image
if cmp(file_size, MAX_SIZE):
im = Image.open(file)
im.thumbnail((128, 128), Image.ANTIALIAS)
file_data = ???? #Help how can i return the same format like file.read() which didn't come into the 'if' part.
return file_data