问题描述
我正在学习Python和Django。
图像由用户使用forms.ImageField()提供。然后我必须处理它,以创建两个不同大小的图像。
当我提交表单时,Django会返回以下错误:
/ add_event /
中的IOError无法识别图像文件
我调用resize函数:
def create_event(owner_id,name,image):
image_thumb = image_resizer(image,name,'_t','events',180,120)
image_medium = image_resizer(image,name,'_m','events',300,200)
当第二次调用image_resizer时,我遇到en错误:
def image_resizer(image,name,size,app_name,length,height):
im = Image.open(image)
if im.mode!= RGB:
im = im.convert(RGB)
im = create_thumb(im,length,height)
posit = str(MEDIA_ROOT)+'/'+ app_name +'/ '
image_2 = im
image_name = name + size +'。jp g'
imageurl = posit + image_name
image_2.save(imageurl,'JPEG',quality = 80)
url_image ='/'+ app_name +'/'+ image_name
return url_image
版本:
Django 1.3.1
Python 2.7.1
PIL 1.1.7
我正在尝试找到问题,但我不知道该怎么办。谢谢高级!
编辑
我解决了重写功能;现在它批量创建不同的图像:
我调用resize函数:
url_array = image_resizer.resize_batch(image,image_name,[[180,120,'_ t'],[300,200,'_ m']],'/ events /')
/ pre>
so:
image_thumb = url_array [0]
image_medium = url_array [1]
和resize函数:
def resize_batch(image,name,size_array,position):
im = Image.open(image)
if im.mode! =RGB:
im = im.convert(RGB)
url_array = []
size_array中的大小:
new_im = create_thumb(im,size [0] ,size [1])$ b $ b posit = str(MEDIA_ROOT)+ position
image_name = name + size [2] +'。jpg'
imageurl = posit + image_name
new_im。 save(imageurl,'JPEG',quality = 90)
new_url_array = position + image_name
url_array.append new_url_array)
返回url_array
感谢所有!
解决方案As ,什么样的对象是
image
?为了这个答案的目的,我将为此假设,它是来自于...的$ code>文件属性的DjangoImageField
由远程用户上传的文件。
文件上传后,您在
ImageField.file
中获取的对象属性是TemporaryUploadedFile
对象,可能代表,具体取决于上传的大小。此对象的行为非常像正常的,所以在你之后已经读了一次(要做第一个缩略图),你已经到达文件的末尾,所以当你尝试再次阅读它(做第二个缩略图),没有什么,所以IO错误
。要制作第二个缩略图,您需要回到起始点的文件。所以你可以添加行image.seek(0)
到您的
image_resizer
函数的开头。
但这是不必要的!您有这个问题,因为您要求Python Imaging Library重新读取要创建的每个新缩略图的图像。这是浪费时间:最好只是一次阅读图像,然后创建所需的所有缩略图。
I'm learning Python and Django.
An image is provided by the user using forms.ImageField(). Then I have to process it in order to create two different sized images.
When I submit the form, Django returns the following error:
IOError at /add_event/ cannot identify image file
I call the resize function:
def create_event(owner_id, name, image): image_thumb = image_resizer(image, name, '_t', 'events', 180, 120) image_medium = image_resizer(image, name, '_m', 'events', 300, 200)
I get en error when image_resizer is called for the second time:
def image_resizer(image, name, size, app_name, length, height): im = Image.open(image) if im.mode != "RGB": im = im.convert("RGB") im = create_thumb(im, length, height) posit = str(MEDIA_ROOT)+'/'+app_name+'/' image_2 = im image_name = name + size +'.jpg' imageurl = posit + image_name image_2.save(imageurl,'JPEG',quality=80) url_image='/'+app_name+'/'+image_name return url_image
Versions:
Django 1.3.1
Python 2.7.1
PIL 1.1.7I'm trying to find the problem, but i don't know what to do. Thank you in advanced!
EDIT
I solved rewriting the function; now it creates the different images in batch:I call the resize function:
url_array = image_resizer.resize_batch(image, image_name, [[180,120,'_t'], [300,200,'_m']], '/events/')
so:
image_thumb = url_array[0] image_medium = url_array[1]
and the resize function:
def resize_batch(image, name, size_array, position): im = Image.open(image) if im.mode != "RGB": im = im.convert("RGB") url_array = [] for size in size_array: new_im = create_thumb(im, size[0], size[1]) posit = str(MEDIA_ROOT) + position image_name = name + size[2] +'.jpg' imageurl = posit + image_name new_im.save(imageurl,'JPEG',quality=90) new_url_array = position + image_name url_array.append(new_url_array) return url_array
Thanks to all!
解决方案As ilvar asks in the comments, what kind of object is
image
? I'm going to assume for the purposes of this answer that it's thefile
property of a DjangoImageField
that comes from a file uploaded by a remote user.After a file upload, the object you get in the
ImageField.file
property is aTemporaryUploadedFile
object that might represent a file on disk or in memory, depending on how large the upload was. This object behaves much like a normal Python file object, so after you have read it once (to make the first thumbnail), you have reached the end of the file, so that when you try to read it again (to make the second thumbnail), there's nothing there, hence theIOError
. To make a second thumbnail, you need to seek back to the beginning of the file. So you could add the lineimage.seek(0)
to the start of your
image_resizer
function.But this is unnecessary! You have this problem because you are asking the Python Imaging Library to re-read the image for each new thumbnail you want to create. This is a waste of time: better to read the image just once and then create all the thumbnails you want.
这篇关于Django PIL:IOError无法识别图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!