I want to create two thumbnails (a small one and a medium one)
This is my code (using python2.7+django1.3):
import Image
...
def thumbnail(f,file_name,path):
small = 35,35
small_file_name = path+'/small_'+file_name
medium = 310,235
medium_file_name = path+'/medium_'+file_name
small_im = Image.open(f)
small_im.thumbnail(small)
small_im.save(small_file_name)
medium_im = Image.open(f)
medium_im.thumbnail(medium)
medium_im.save(medium_file_name)
I am getting this error:
IOError at /upload/
cannot identify image file
f
is image FILES post from another page, f = request.FILES['photo']
Some errors in last three lines, I have to comment last three lines,it's not a error, but this is not what I want.
If I run the code in the python shell it doesn't raise errors:
>>> from users.upload import thumbnail
>>> import Image
>>> f = '/home/david/1.jpg'
>>> file_name = 'test.jpg'
>>> path = '/home/david'
>>> thumbnail(f,file_name,path)
I don't understand why I'm getting an error.
You can try doing this:
def thumbnail(f,file_name,path):
img = Image.open(f)
small = 35,35
small_file_name = path+'/small_'+file_name
medium = 310,235
medium_file_name = path+'/medium_'+file_name
img.thumbnail(medium)
img.save(medium_file_name)
img.thumbnail(small)
img.save(small_file_name)
You are having a problem because f = request.FILES['photo']
is not a file path, as you assume it is. Instead it is a Django UploadedFile
. You should write this file to the disk somewhere before passing its path to PIL Image.open()
.
You could also try passing the temporary_file_path
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.