检测JPG压缩率?

发布于 2025-01-06 22:35:10 字数 288 浏览 1 评论 0原文

通常,当图像进入我的网站时,我使用我编写的图像库将其保存为 jpg,默认质量为 80%。现在,当我需要对其进行其他操作(例如裁剪,甚至只是调整其大小)时,图像将以 jpg 格式打开、处理,然后保存回来。但是,如果之前已经压缩过,我不想再压缩它,否则每次需要执行操作时质量都会下降。

有没有一种方法可以使用标准 GD php 库中的工具检测图像之前已经被压缩了多少(我猜与它的 png 版本相比)?我知道检测图像经过 Photoshop 处理的位置的工具是通过比较相对压缩量来实现的,因此我认为可以检测压缩量,但有人知道我将如何进行此计算吗?谢谢。

Normally when an image comes into my site I save it as jpg using an image library I wrote with the default quality of 80%. Now when I need to do some other operation on it (like cropping it, or even just resizing it) the image will be opened as jpg, processed, then saved back. However, if it has been compressed before I don't want to compress it again or else every time I need to do an operation the quality will fall.

Is there a way that I can detect how much the image has already been compressed before (in comparison to a png version of it I guess) using tools in the standard GD php libraries? I know that tools which detect where an image has been Photoshopped do so by comparing relative amounts of compression so I would think it is possible to detect the amount of compression but does anyone know how I would go about doing this calculation? Thanks.

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

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

发布评论

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

评论(4

腹黑女流氓 2025-01-13 22:35:11

您无法从 JPG 中获得质量价值。此外,质量值取决于编码器。它不是标准或类似的东西。有些程序只有(低、中、高),有些 20 可能比 90 更好。

其次,JPG 在每次后续编码中都会损失质量,即使您每次都将其保存为最佳质量。生活中的悲惨事实:)唯一不会降低质量的操作是翻转和旋转(以及裁剪,如果它们与 JPEG 块大小对齐)。

经验法则:每次都以相同的质量值进行编码。示例:如果您以 60 保存一次,那么如果下次以 80 保存,则不会有任何增益。只是文件大小更大。

另外,如果您有足够的可用存储空间,请尝试减少此类重新编码的数量并在原始 上执行每个操作。

You cannot get quality value from JPG. Moreover, the quality value is encoder dependent. It is not a standard or anything like this. Some programs have only (low, medium, high), in some 20 might be better than 90.

Secondly, JPG simply will lose quality in each cosnequent encoding, even if you save it as best quality every time. Sad truth of life :) The only operations that do not worse the quality are flips and rotations (and crops, if they are aligned to JPEG block size).

Rule of the thumb: Encode it every time at the same quality value. Example: If you save it once at let's say 60, then there is no gain if you save it at 80 next time. Just bigger file size.

Also, try to reduce the number of such re-encodings and perform each manipulation on the original , if you have enough storage available.

浅紫色的梦幻 2025-01-13 22:35:11

您必须将图像的质量存储在数据库中,以便您知道它是否已经被压缩。

You will have to store the image's quality in a database so you can know if it has already been compressed or not.

笨死的猪 2025-01-13 22:35:11

为了避免多次压缩图像,您可以简单地将调整后的文件大小与原始文件大小进行比较。

  • 如果文件大小明显较大,则表明您已提高速率,因此请勿使用它。
  • 如果它明显低于压缩后的值,请使用它。

此外,由于以相同的速率重新压缩文件只会减少文件大小的一小部分,因此如果您使用整数作为速率(60%、70%、80%等),则可以确定调整大小的文件的速率尺寸与原件非常相似。

例如,压缩 1,844 KB 文件:
- 90% = 2,115 KB。尺寸增加了,所以我不会使用它。
- 80% = 1,843 KB。这与原始文件大小几乎相同,因此我可以假设原始文件的比率为 80%
- 70% = 1,567 KB。这个已经压缩了,所以我会用它。

最后,如果您只关心自己压缩的图像的速率,那么您可以使用 PHP 来保存文件元数据中使用的速率。

To avoid compressing the image multiple times you could simply compare the file size of the resize with the original.

  • If the file size is noticeably larger then you've increased the rate, so don't use it.
  • If it's noticeably lower than it has compressed, so use it.

Also, as recompressing a file with the same rate only shaves a small amount off of the file size, if you use round numbers for your rates (60%, 70%, 80% etc.) you could ascertain the rate if the resize's file size is very similar to the original's.

For example, compressing a 1,844 KB file at:
- 90% = 2,115 KB. The size increased, so I won't use it.
- 80% = 1,843 KB. This is almost identical to the original's file size so I can assume that the original has an 80% rate.
- 70% = 1,567 KB. This has compressed, so I'll use it.

Finally, if you're only concerned with the rates of images that you've compressed yourself, then you could use PHP to save the rate you use in the file's metadata.

岁月流歌 2025-01-13 22:35:11

如果 JPG 图像仅被编码一次,您实际上可以检测 JPG 图像的压缩质量,并且具有一定的确定性。

您可以使用名为 identify 的命令行工具来完成此操作,该工具是 Imagemagick 的一部分(也可以作为 PHP 库提供,请参见下文)。例如,您可以运行:

identify -verbose 11397254.jpg

,您将得到如下输出:

Image: 11397254.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 600x833+0+0
  Units: Undefined
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 499800
    Red:
      min: 7 (0.027451)
      max: 251 (0.984314)
      mean: 205.535 (0.80602)
      standard deviation: 40.8098 (0.160038)
      kurtosis: 7.3041
      skewness: -2.81763
      entropy: 0.627683
    Green:
      min: 4 (0.0156863)
      max: 237 (0.929412)
      mean: 200.186 (0.785044)
      standard deviation: 45.9241 (0.180095)
      kurtosis: 3.86968
      skewness: -2.25667
      entropy: 0.6339
    Blue:
      min: 0 (0)
      max: 241 (0.945098)
      mean: 191.701 (0.75177)
      standard deviation: 54.6716 (0.214399)
      kurtosis: 1.22795
      skewness: -1.70134
      entropy: 0.664107
  Image statistics:
    Overall:
      min: 0 (0)
      max: 251 (0.984314)
      mean: 199.141 (0.780945)
      standard deviation: 47.4814 (0.186202)
      kurtosis: 3.61004
      skewness: -2.23079
      entropy: 0.641896
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: JPEG
  Intensity: Undefined
  Compose: Over
  Page geometry: 600x833+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 100
  Orientation: Undefined
  Properties:
    date:create: 2020-10-28T09:13:49+01:00
    date:modify: 2020-10-28T09:13:45+01:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x2,1x1,1x1
    signature: 7b5dc010915e4ae8b89891f7259a6f22efc69a31e5244403db580b70adc2ee94
  Artifacts:
    filename: 11397254.jpg
    verbose: true
  Tainted: False
  Filesize: 147KB
  Number pixels: 500K
  Pixels per second: 49.98MB
  User time: 0.010u
  Elapsed time: 0:01.010
  Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org

重要的几行是:

  Compression: JPEG
  Quality: 100

如果质量为 100,则意味着图像尚未压缩根本不。

我针对 GIMP JPG 压缩测试了该工具,它在检测图像上使用的压缩百分比方面非常准确。

在 PHP 中,ImageMagick 库中有一个函数:

https://www.php .net/manual/en/imagick.identifyimage.php

这似乎是同一工具的移植,所以你可以检查一下。

You actually can detect the quality at which a JPG image has been compressed, with some degree of certainty, if it has been encoded only once.

You can do it with a command-line tool called identify, which is part of Imagemagick (which is also available as a PHP library, see below). You can run, for example:

identify -verbose 11397254.jpg

and you will get an output like this:

Image: 11397254.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 600x833+0+0
  Units: Undefined
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 499800
    Red:
      min: 7 (0.027451)
      max: 251 (0.984314)
      mean: 205.535 (0.80602)
      standard deviation: 40.8098 (0.160038)
      kurtosis: 7.3041
      skewness: -2.81763
      entropy: 0.627683
    Green:
      min: 4 (0.0156863)
      max: 237 (0.929412)
      mean: 200.186 (0.785044)
      standard deviation: 45.9241 (0.180095)
      kurtosis: 3.86968
      skewness: -2.25667
      entropy: 0.6339
    Blue:
      min: 0 (0)
      max: 241 (0.945098)
      mean: 191.701 (0.75177)
      standard deviation: 54.6716 (0.214399)
      kurtosis: 1.22795
      skewness: -1.70134
      entropy: 0.664107
  Image statistics:
    Overall:
      min: 0 (0)
      max: 251 (0.984314)
      mean: 199.141 (0.780945)
      standard deviation: 47.4814 (0.186202)
      kurtosis: 3.61004
      skewness: -2.23079
      entropy: 0.641896
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: JPEG
  Intensity: Undefined
  Compose: Over
  Page geometry: 600x833+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 100
  Orientation: Undefined
  Properties:
    date:create: 2020-10-28T09:13:49+01:00
    date:modify: 2020-10-28T09:13:45+01:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x2,1x1,1x1
    signature: 7b5dc010915e4ae8b89891f7259a6f22efc69a31e5244403db580b70adc2ee94
  Artifacts:
    filename: 11397254.jpg
    verbose: true
  Tainted: False
  Filesize: 147KB
  Number pixels: 500K
  Pixels per second: 49.98MB
  User time: 0.010u
  Elapsed time: 0:01.010
  Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org

Where the important lines are:

  Compression: JPEG
  Quality: 100

If quality is 100, it means that the image hasn't been compressed at all.

I tested this tool against GIMP JPG compression, and it was very accurate in detecting the percentage of compression I used on images.

In PHP, there's a function in the ImageMagick library:

https://www.php.net/manual/en/imagick.identifyimage.php

It seems to be a porting of the same tool, so you could check that.

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