当你使用thumbnail()然后crop()时,PIL是否会在图像的底部边缘产生伪像?如果是这样,你的解决方法是什么?
当我调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这也发生在我身上。这对我来说是一个学习练习,因为我从未使用 PIL 裁剪或创建缩略图...
缩略图(大小,过滤器=无)
用给定大小的新图像替换原始图像(第2页) 。可选的
filter 参数的工作方式与 .resize() 方法中的工作方式相同。
此操作保留纵横比(高度:宽度)。生成的图像将一样大
尽可能的同时仍然适合给定的尺寸。 例如,如果图像 im 的大小为 (400,150),则其
im.thumbnail((40,40)) 之后的大小将为 (40,15)。
所以发生的事情是
方面
图像尺寸为 40 x 40
我为重复该问题而编写的代码:
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
the aspect
image to be 40 x 40
Code I wrote to repeat the issue:
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.