尽管`resample=Image.BICUBIC`,为什么PIL.Image.rotate() 会导致质量下降 [英] Why does PIL.Image.rotate() results in loss of quality despite `resample=Image.BICUBIC`

查看:356
本文介绍了尽管`resample=Image.BICUBIC`,为什么PIL.Image.rotate() 会导致质量下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 https://stackoverflow.com/a/17822099/1639359 使用 resample=PIL.Image.BICUBIC 应该通过轮换来保持质量.这个问题是我对上述答案的评论的后续问题,即它对我不起作用.

Per https://stackoverflow.com/a/17822099/1639359 using resample=PIL.Image.BICUBIC should retain quality through rotation. This question is follow up from my comment on the above answer that it is not working for me.

import sys
print(sys.version)

3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0]

import PIL
import PIL.Image
print(PIL.__version__)
print(PIL.Image.__version__)

6.2.1
6.2.1

打开我们的测试图像,并保存它.
注意质量损失;保存的文件比原始文件小得多:

img = PIL.Image.open('test_image.jpg')
holdexif = img.info['exif']
img.save('testsave.jpg',"jpeg",exif=holdexif)

%ls -l 'test_image.jpg'
%ls -l 'testsave.jpg'

-rwxrwxrwx 1 dino dino 1926859 Dec 24 21:28 test_image.jpg*
-rwxrwxrwx 1 dino dino 452343 Dec 27 13:16 testsave.jpg*

根据 在 Python (PIL) 中确定 JPG 质量
设置quality='keep' 可防止保存时质量下降:

Per Determining JPG quality in Python (PIL)
setting quality='keep' prevents the loss of quality on save:

img.save('testsave.jpg',"jpeg",exif=holdexif,quality='keep')

%ls -l 'test_image.jpg'
%ls -l 'testsave.jpg'

-rwxrwxrwx 1 dino dino 1926859 Dec 24 21:28 test_image.jpg*
-rwxrwxrwx 1 dino dino 1926911 Dec 27 13:16 testsave.jpg*

<小时>

如果我们旋转图像,会发生两件事:

  • img.format 消失
  • 质量显着降低(即使我们使用 resample=PIL.Image.BICUBIC)
  • print(img.format)
    

    JPEG
    

    rotated_img = img.rotate(-90,resample=PIL.Image.BICUBIC,expand=True)
    

    print(img.format)
    print(rotated_img.format)
    

    JPEG
    None
    

    <小时>

    保存旋转文件显示质量下降:

    rotated_img.format="JPEG"  # quality='keep' will raise exception if format != 'JPEG'
    rotated_img.save('testsave.jpg',"jpeg",exif=holdexif,quality='keep')
    %ls -l 'test_image.jpg'
    %ls -l 'testsave.jpg'
    

    -rwxrwxrwx 1 dino dino 1926859 Dec 24 21:28 test_image.jpg*
    -rwxrwxrwx 1 dino dino 450997 Dec 27 13:16 testsave.jpg*
    

    问题:

    1.如果有的话,我做错了什么?

    2.为什么 img.format 在旋转时消失?还有什么正在消失?

    3.而且,顺便说一句,是否有另一种方法来检测质量损失(我只知道保存文件并看到它小得多).

    <小时>

    在@MarkRansom 发表评论后,我想为我的问题添加一些颜色:虽然我已经编码多年,但我上周才开始玩图像.毫无疑问,我对图像处理的了解充其量是最少的,对事物的运作方式存在重大差距和误解.

    Questions:

    1. What, if anything, am I doing wrong?

    2. Why does img.format disappear on rotate? What else is disappearing?

    3. And, just as an aside, is there another way to detect the loss of quality (I only know to save the file and see that it is much smaller).


    After @MarkRansom 's comments, I would like to add some color to my question: Although I have been coding for many years, I only last week began playing with images. Without a doubt my knowledge of image processing is minimal at best, with significant gaps and misunderstandings about how things work.

    也就是说,从编码的角度来看,我希望对象上的方法几乎只执行该方法建议的操作,而没有副作用.例如,我希望 rotate() 仅旋转图像,而不会同时清除元数据.即使为了效率起见,PIL 包的默认行为是始终删除元数据,我希望有一个选项(全局或基于每个方法),例如 保留元数据=真

    That said, from a coding perspective, I would expect that a method on an object would do pretty much only what that method suggests, with no side effects. For example, I would expect rotate() only to rotate the image, and not clear out meta data at the same time. Even if, let's say for efficiency sake, the default behavior for the PIL package is to always drop the meta data, I would hope there would be an option (either global, or on a per-method basis) for something like preserve_metadata=True

    关于判断质量的唯一好方法"是查看图像本身",我当然可以完全理解这种情况.然而,在相对的基础上,最显着的是在比较一些处理前后的相同"图像时,在我看来,可能有一些方法可以物理测量,或至少估计之前的质量差异和处理后.(当然,我对图像的了解还不够多,无法知道最好的方法是什么;这也是我提出问题的部分原因).在我看来(至少从我初学者的角度来看),人们经常希望同时进行任何想要质量损失很少或没有质量损失的处理.感谢您的耐心和帮助填补我的知识空白,并提供答案.

    Regarding "the only good way to judge quality" being "to look at the images themselves," I can certainly understand that being the case on an absolute basis. However on a relative basis, most notably when comparing the "same" image before and after some processing, it seems to me there could be ways to physically measure, or at least estimate, the difference in quality before and after the processing. (Of course, I don't yet know enough about images to know what the best method of doing that would be; thus part of the reason for my question). It also seems to me (at least from my beginner perspective) that very often people would want to do whatever processing that want to do with little or no loss of quality at the same time. Thanks for your patience and help filling in my knowledge gaps, and with the answers.

    推荐答案

    一般来说,任何图像旋转都会导致图像质量下降,因为输出需要对未映射的像素进行输入插值从输入到输出为 1:1.对于 90 度的倍数旋转,可以获得 1:1 映射并进行无损旋转.目前尚不清楚 PIL/Pillow 是否足够聪明来做到这一点,但它可能.您的示例旋转了 -90 度,因此您可能会走运.

    In general, any image rotation is going to result in a loss of image quality simply because the output requires an interpolation of the input for pixels that don't map 1:1 from input to output. For rotations of multiples of 90 degrees, it's possible to get a 1:1 mapping and do a lossless rotation. It's not clear if PIL/Pillow is smart enough to do that, but it might. Your example rotates by -90 degrees so you could luck out.

    元数据正在丢失,因为 rotate 生成了一张新图像,它不会修改现有图像.确定哪些元数据可能与新图像相关并不容易,因此最安全的做法是不要复制任何元数据.正如您所发现的,可以手动复制您认为重要的任何内容.我认为没有自动复制任何内容的选项.

    The metadata is being lost because rotate generates a new image, it doesn't modify the existing one. It's not easy to determine which metadata might be relevant for the new image, so the safest course of action is to simply not copy any of it. As you've discovered, it's possible to manually copy anything you think is important. I don't think there's an option to automatically copy anything.

    正如评论中所指出的,使用文件大小来判断图像质量是一个非常糟糕的指标.较小的尺寸表明质量较差,但并非必须如此;JPEG 压缩的整个前提是在正常查看中根本看不到某些图像质量下降.视觉比较是最终的测试.如果您必须进行自动比较,有一些方法可以这样做,但我认为没有任何方法已成为无可争议的最佳比较方法.

    As noted in the comments, using file size as a judge of image quality is a very poor metric. A smaller size is suggestive of poorer quality, but it doesn't have to be so; the whole premise of JPEG compression is that some image degradations simply won't be visible in normal viewing. A visual comparison is the ultimate test. If you must have an automated comparison there are methods to do so, but I don't think any have emerged as the undisputed best way to compare.

    这篇关于尽管`resample=Image.BICUBIC`,为什么PIL.Image.rotate() 会导致质量下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆