So I have a form in Django that is used to upload an image to an ImageField. This works well. I then pass off the request.FILES[‘image’] off to a function called MakeThumbnail for further processing.
MakeThumbnail is not working too well. I have assembled this from a series of StackOverflow threads, but I think I am missing some crucial concepts. Here is my code.
def MakeThumbnail(file):
img = Image.open(file)
thumbnail = img.thumbnail((128, 128), Image.ANTIALIAS)
thumbnailString = StringIO.StringIO(thumbnail)
newFile = ContentFile(thumbnailString)
return newFile
The stacktrace says I am failing on this line: newFile = ContentFile(thumbnailString). It gives me the following error: expected read buffer, instance found
I think I might be failing back at thumbnail = img.thumbnail((128, 128), Image.ANTIALIAS), though, after looking at the local variables:
thumbnailString : <StringIO.StringIO instance at 0x0000000003B39748>
file : <InMemoryUploadedFile: Desert.jpg (image/jpeg)>
thumbnail : None
img : <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x96 at 0x3B25C08>
Notice that thumbnail is none. It should contain something. Does anyone have some hints for me?
thank you!
Thank you so much for the help, Jan Spurny. I figured it out with your help. So, when img is saved to thumbnailString, a few things need to happen. The code above actually produces an error because we pass an instance to ContentFile instead of a buffer. No matter, we just need to call thumbnailString.read(). But thumbnailString.read() returns ”. Turns out we need to do thumbnailString.seek(0) beforehand. But this did not work either!
Ultimately, I was able to get the image file back to a Django file using InMemoryUploadedFile.
Here is what the code looks like, from beginning to end: