使用 PIL 在 Django 中调整图像大小两次

发布于 2024-08-06 04:13:51 字数 1271 浏览 4 评论 0原文

我有一个函数,我试图根据 request.FILES['image'] 调整照片大小两次。我也将 image.thumbnail() 与解析器一起使用。当我创建一个缩略图时,这工作得很好,但在我看来,如果我再次重复完全相同的事情,它会在解析器中因 IOError 无法解析图像而失败。我很困惑。我在内存中创建了 StringIO 文件,而不是按原样使用 Django 的 UploadedFile 对象,它仍然做同样的事情。非常感谢任何帮助。

假设我想执行以下两次操作(使用两种不同的缩略图大小),而不检索 URL 两次:

import urllib2
from PIL import Image, ImageFile, ImageEnhance

# create Image instance
file = urllib2.urlopen(r'http://animals.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/animals/images/primary/kemps-ridley-sea-turtle.jpg')
parser = ImageFile.Parser()
while True:
    s = file.read(1024)
    if not s:
        break
    parser.feed(s)
image = parser.close()

# make thumbnail
size = (75, 75)
image.thumbnail(size, Image.ANTIALIAS)
background = Image.new('RGBA', size, (255, 255, 255, 0))
background.paste(
    image,
    ((size[0] - image.size[0]) / 2, (size[1] - image.size[1]) / 2))

background.save('copy.jpg')

例如:

image = parser.close()
image2 = parser.close() # Obviously this doens't work
image2 = image # Obviously this doesn't either but you get what I need to do here
# Do 2 thumbnails with only one original source.

...省略其他代码...

image.save('copy.jpg')
image2.save('copy.jpg')

I have a function in which I'm trying to resize a photo twice from request.FILES['image']. I'm using the image.thumbnail() with the Parser as well. This works fine when I create one thumbnail, but in my view if I repeat the exact same thing again, it fails in the parser via IOError cannot parse image. I'm very confused. I've created StringIO files in memory instead of using Django's UploadedFile object as-is and it still does the same thing. Any help is much appreciated.

Suppose I wanted to do the following twice (with two different thumbnailing sizes) all without retrieving the URL twice:

import urllib2
from PIL import Image, ImageFile, ImageEnhance

# create Image instance
file = urllib2.urlopen(r'http://animals.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/animals/images/primary/kemps-ridley-sea-turtle.jpg')
parser = ImageFile.Parser()
while True:
    s = file.read(1024)
    if not s:
        break
    parser.feed(s)
image = parser.close()

# make thumbnail
size = (75, 75)
image.thumbnail(size, Image.ANTIALIAS)
background = Image.new('RGBA', size, (255, 255, 255, 0))
background.paste(
    image,
    ((size[0] - image.size[0]) / 2, (size[1] - image.size[1]) / 2))

background.save('copy.jpg')

For instance:

image = parser.close()
image2 = parser.close() # Obviously this doens't work
image2 = image # Obviously this doesn't either but you get what I need to do here
# Do 2 thumbnails with only one original source.

... other code ommitted ...

image.save('copy.jpg')
image2.save('copy.jpg')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨离汐 2024-08-13 04:13:51

如果这一次有效,正如你所说,你检索到的图像就很好了。至少有两种不同的方法可以从单个 PIL 图像中获取多个缩略图。

  1. 您可以使用 PIL 的 resize 方法,该方法将返回原始文件大小调整后的副本。如果您想保持比例完整,您只需计算所需的尺寸即可。
  2. 使用 Image.copy() 获取图像的副本。

像这样:

original = parser.close()
...

thumb1 = original.copy()
size = (75,75)
thumb1.thumbnail(size, Image.ANTIALIAS)
...

thumb2 = original.copy()
thumbnail2 = original.resize(size2, Image.ANTIALIAS)
...

这样,原件就不会被更改,并且您可以根据需要获得任意数量的副本。

If this works once, as you say, the image you retrieved is just fine. There are at least two different ways to get multiple thumbnails out of single PIL images.

  1. You can use PIL's resize method, which will return a resized copy of the original. You just have to calculate the dimensions you'll need if you want to keep the proportions intact.
  2. Use Image.copy() to get a copy of the image.

Like this:

original = parser.close()
...

thumb1 = original.copy()
size = (75,75)
thumb1.thumbnail(size, Image.ANTIALIAS)
...

thumb2 = original.copy()
thumbnail2 = original.resize(size2, Image.ANTIALIAS)
...

This way, the original will not be altered and you can get as many copies as you need.

因为看清所以看轻 2024-08-13 04:13:51

比复制原始图像更简单的解决方案是在调用缩略图(...)之间重置文件指针,如下所示:

original.seek(0)

A simpler solution than copying the original image is to instead reset the file pointer between calls to thumbnail(...) like so:

original.seek(0)
无边思念无边月 2024-08-13 04:13:51

我假设它在 image = parser.close() 行失败并出现 IOError。所以 ImageFile 获取图像数据的方式可能有问题。您是否尝试过从本地文件读取?

如果解析器成功解码图像,它将返回一个 Image 对象。
否则,此方法将引发 IOError 异常。

来源

I'm assuming it's failing on the image = parser.close() line with an IOError. so there's probably something wrong with the way ImageFile is getting the image data. Have you tried making reading from a local file instead?

If the parser managed to decode an image, it returns an Image object.
Otherwise, this method raises an IOError exception.

Source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文