1

我在 Django 项目中使用 wand,从不同类型的文件(例如 pdf)生成缩略图,所有缩略图生成过程都在内存中完成,源文件是从请求中获取的,缩略图保存到临时文件, 然后 Django FileFiled 将图像保存在正确的路径中,但生成的缩略图保持初始大小,这是我的代码:

with image.Image(file=self.content.file, format="png") as im: # self.content is a django model FileField didn't saved yet, so the file inside is still in memory (from the request)
    im.resize(200, 200)
    name = self.content.file.name
    self.temp = tempfile.NamedTemporaryFile()
    im.save(file=self.temp)
    self.thumbnail = InMemoryUploadedFile(self.temp, None, name + ".png", 'image/png', 0, 0, None)  # then self.thumnail as FileField saves the image

你知道会发生什么吗?可能是一个错误?我已经在 wand github 页面上将其报告为问题。

4

1 回答 1

0

问题在于您的 PDF 有不止一页。如果您只调整第一页的大小(这是您要显示的那一页),它可以工作。with尝试在语句后添加以下行:

im = image.Image(image=im.sequence[0])

但我同意你的观点,你的版本也应该可以工作。

于 2015-07-13T19:56:08.353 回答