这似乎是一个相当容易的问题,但我无法弄清楚这里发生了什么。 基本上,我想要做的是在Django模型上从一个图像创建两个不同的缩略图。最终发生的事情是它似乎在循环并重新创建相同的图像(每次向它附加一个下划线),直到它引发一个文件名很大的错误。所以,你最终是这样的:从Django中的相同图像生成两个缩略图
OSError: [Errno 36] File name too long: 'someimg________________etc.jpg'
这里是代码(保存方法是对艺术家模型):
def save(self, *args, **kwargs):
if self.image:
iname = os.path.split(self.image.name)[-1]
fname, ext = os.path.splitext(iname)
tlname, tsname = fname + '_thumb_l' + ext, fname + '_thumb_s' + ext
self.thumb_large.save(tlname, make_thumb(self.image, size=(250,250)))
self.thumb_small.save(tsname, make_thumb(self.image, size=(100,100)))
super(Artist, self).save(*args, **kwargs)
def make_thumb(infile, size=(100,100)):
infile.seek(0)
image = Image.open(infile)
if image.mode not in ('L', 'RGB'):
image.convert('RGB')
image.thumbnail(size, Image.ANTIALIAS)
temp = StringIO()
image.save(temp, 'png')
return ContentFile(temp.getvalue())
我没有表现出对进口为了简洁起见。假设Artist模型上有两个ImageField:thumb_large和thumb_small。
我测试,如果这个工程的方式是,在shell:
artist = Artist.objects.get(id=1)
artist.save()
#error here after a little wait (until I assume it generates enough images that the OSError gets raised)
如果不这样做的正确的方式,我会很感激的任何反馈。谢谢!
假设'保存()'正在从Django模块调用时,它是很难在不调用代码来回答,或艺术家的定义.save,或ContentFile等 – msw 2010-03-15 02:25:03
我把它的方式是,在shell: 艺术家= Artist.objects.get(ID = 1) artist.save() 那是怎样我一直在测试如果它有效,艺术家就会有一个形象。所以如果self.image是真的。 这是在Artist模型上定义的保存方法,与ContentFile一样:从django.core.files.base导入ContentFile – tsoporan 2010-03-15 02:45:14