当你使用thumbnail()然后crop()时,PIL是否会在图像的底部边缘产生伪像?如果是这样,你的解决方法是什么?

发布于 2024-08-22 16:13:53 字数 261 浏览 4 评论 0原文

当我调用 PIL 对图像进行缩略图(),然后调用裁剪()时,我在最后一行像素上出现了伪影 - 它们要么大部分是黑色,带有强烈的颜色斑点,要么是似乎未调整大小的区域图像的(即,该像素线处于原始分辨率并且没有与其余像素一起缩小)

这似乎不会发生在没有裁剪的缩略图()上。 无论我是否对裁剪后的图像调用 load() ,都会发生这种情况。

为了在视觉上解决这个问题,我将缩略图放大为 1 像素,然后裁剪为相同大小。这似乎有效。但这是一种肮脏的黑客行为。我想知道是否有适当的解决办法。

When I call PIL to thumbnail() an image, and then crop(), I get artifacting on the last row of pixels -- they're either mostly black with specks of intense color, or what seems to be a not-resized area of the image ( ie, that line of pixels is at the original resolution and did not scale down with the rest )

This does not seem to happen on a thumbnail() without a crop.
This happens whether or not I call a load() on the cropped image either.

To get around this visually, i've been thumbnailing to a 1pixel larger image, and then cropping to the same size. That seems to work. It's kind of a dirty hack though. I'm wondering if there's a proper fix.

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

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

发布评论

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

评论(1

少年亿悲伤 2024-08-29 16:13:53

是的,这也发生在我身上。这对我来说是一个学习练习,因为我从未使用 PIL 裁剪或创建缩略图...

缩略图(大小,过滤器=无)

用给定大小的新图像替换原始图像(第2页) 。可选的
filter 参数的工作方式与 .resize() 方法中的工作方式相同。
此操作保留纵横比(高度:宽度)。生成的图像将一样大
尽可能的同时仍然适合给定的尺寸。 例如,如果图像 im 的大小为 (400,150),则其
im.thumbnail((40,40)) 之后的大小将为 (40,15)

所以发生的事情是

  1. 你使用缩略图来维护
    方面
  2. 您所期待的
    图像尺寸为 40 x 40
  3. 您的裁剪超出了缩略图的实际大小
  4. 由于裁剪超出了尺寸,底部很可能出现黑色条带

我为重复该问题而编写的代码:

def croptest(file, width, height):
    import Image as pil
    import os

    max_width = width
    max_height = height
    file, ext = os.path.splitext(file)

    img = pil.open(file)
    img.thumbnail((max_width, max_height), pil.ANTIALIAS)
    img.save(file + ".thumb.jpeg", 'JPEG')
    croppedImage = img.crop((10, 10, 40, 40))
    croppedImage.save(file + ".croppedthumb.jpeg", 'JPEG')

if __name__ == "__main__":
   croptest("Desktop.bmp", 50, 50)

Desktop.thumb.jpeg 为 50 x 37 而 Desktop .croppedthumb.jpeg 是 30 x 30,所以我在底部有一条 3 像素高的黑线。

您的解决方案是要么在缩略图的实际大小内进行裁剪,要么找出如何创建忽略纵横比的缩略图。

Yes, this happens to me also. This was a learning exercise for me because I have never cropped or created thumbnails using the PIL...

thumbnail(size,filter=None)

Replaces the original image, in place, with a new image of the given size (p. 2). The optional
filter argument works in the same way as in the .resize() method.
The aspect ratio (height : width) is preserved by this operation. The resulting image will be as large
as possible while still fitting within the given size. For example, if image im has size (400,150), its
size after im.thumbnail((40,40)) will be (40,15)
.

So what is happening is

  1. You use thumbnail which maintains
    the aspect
  2. You're expecting the
    image to be 40 x 40
  3. You're cropping beyond the actual size of the thumbnail
  4. A black strip most likely on the bottom due to cropping beyond the size

Code I wrote to repeat the issue:

def croptest(file, width, height):
    import Image as pil
    import os

    max_width = width
    max_height = height
    file, ext = os.path.splitext(file)

    img = pil.open(file)
    img.thumbnail((max_width, max_height), pil.ANTIALIAS)
    img.save(file + ".thumb.jpeg", 'JPEG')
    croppedImage = img.crop((10, 10, 40, 40))
    croppedImage.save(file + ".croppedthumb.jpeg", 'JPEG')

if __name__ == "__main__":
   croptest("Desktop.bmp", 50, 50)

Desktop.thumb.jpeg was 50 x 37 whereas Desktop.croppedthumb.jpeg was 30 x 30 so I had a 3 pixel high black line across the bottom.

Your solution would be either to crop inside the actual size of the thumbnail or figure out how to create a thumbnail ignoring the aspect ratio.

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