可以旋转JPEG压缩图像而不会降低质量吗?

时间:2022-10-25 21:16:00

JPEG is a lossy compression scheme, so decompression-manipulation-recompression normally reduces the image quality further for each step. Is it possible to rotate a JPEG image without incurring further loss? From what little I know of the JPEG algorithm, it naively seems possible to avoid further loss with a bit of effort. Which common image manipulation programs (e.g. GIMP, Paint Shop Pro, Windows Photo Gallery) and graphic libraries cause quality loss when performing a rotation and which don't?

JPEG是一种有损压缩方案,因此解压缩 - 操作 - 重新压缩通常会进一步降低每一步的图像质量。是否可以旋转JPEG图像而不会造成进一步的损失?从我对JPEG算法的了解很少,它似乎可以通过一些努力避免进一步的损失。哪些常见的图像处理程序(例如GIMP,Paint Shop Pro,Windows照片库)和图形库在执行旋转时会导致质量损失,哪些不会?

10 个解决方案

#1


Yes, it is possible for certain cases: 90-degree rotations and flips on images with dimensions that are a multiple of 8. The heart of the JPEG algorithm -- the lossy part -- involves breaking the image into 8x8 pixel blocks, performing a discrete cosine transform on the block and then quantizing the result. There's also some color space conversion and lossless compression of the blocks on top of this.

是的,对于某些情况是可能的:90度旋转和尺寸为8的倍数的图像翻转.JPEG算法的核心 - 有损部分 - 涉及将图像分成8x8像素块,执行块上的离散余弦变换然后量化结果。在此之上还有一些色块空间转换和块的无损压缩。

Rotating or flipping an 8x8 block will give a DCT with the same basic coefficients, but possibly transposed and/or with some sign changes depending on the transformation. So the basic steps to rotate or flip an image losslessly would involve:

旋转或翻转8x8块将使DCT具有相同的基本系数,但可能转换和/或根据变换进行一些符号变化。因此,无损旋转或翻转图像的基本步骤包括:

  1. Decompress and extract the blocks
  2. 解压缩并提取块

  3. Transpose and/or sign flip the DCT coefficients for each block
  4. 对每个块进行转置和/或符号翻转DCT系数

  5. Reshuffle the blocks into their new order (otherwise the 8x8 blocks would be rotated but still in the old place)
  6. 将块重新调整为新的顺序(否则8x8块将旋转但仍旧在旧位置)

  7. Recompress it all with the lossless compression steps.
  8. 使用无损压缩步骤将其全部重新压缩。

#2


There is a program named jpegtran

有一个名为jpegtran的程序

jpegtran – a utility for lossless transcoding between different JPEG formats.

jpegtran - 一种用于在不同JPEG格式之间进行无损转码的实用程序。

And Here is a list of applications which provide the JPEG lossless rotation feature based on the IJG code

以下是基于IJG代码提供JPEG无损旋转功能的应用程序列表

#3


Absolutely - just change the orientation value in the EXIF data. The vast majority of image programs will respect this setting and show the picture "rotated".

绝对 - 只需更改EXIF数据中的方向值即可。绝大多数图像程序将尊重此设置并显示图片“旋转”。

It's also possibly to "manually" (e.g. programatically) rotate the image in a lossless fashion if certain criteria are true - rotation must be 90/180 degrees and the width/height must multiples of the block-size. You can also flip/mirror it. I don't know whether image programs are smart enough to special-case this operation though. I would guess not.

如果某些标准为真,也可以“手动”(例如以编程方式)以无损方式旋转图像 - 旋转必须是90/180度,宽度/高度必须是块大小的倍数。你也可以翻转/镜像它。我不知道图像程序是否足够聪明,特殊情况下这个操作。我猜不会。

#4


From the JPEG FAQ:

来自JPEG FAQ:

"There are a few specialized operations that can be done on a JPEG file without decompressing it, and thus without incurring the generational loss that you'd normally get from loading and re-saving the image in a regular image editor. In particular it is possible to do 90-degree rotations and flips losslessly, if the image dimensions are a multiple of the file's block size (typically 16x16, 16x8, or 8x8 pixels for color JPEGs).
...

“有一些专门的操作可以在JPEG文件上完成,无需解压缩,因此不会产生通常从加载和重新保存图像的常规图像编辑器中产生的代数损失。特别是如果图像尺寸是文件块大小的倍数(对于彩色JPEG通常为16x16,16x8或8x8像素),则可以无损地进行90度旋转和翻转。...

But you do need special software; rotating the image in a regular image editor won't be lossless."

但你确实需要特殊的软件;在常规图像编辑器中旋转图像不会是无损的。“

#5


Not a jpg expert, but it seems that the answer would be Yes for 90, 180, 270 degree rotations. (maybe even for 360! :))

不是jpg专家,但似乎对于90,180,270度旋转的答案是肯定的。 (甚至可能是360!:))

#6


Yes, it is possible.
A quick google search gave this list of programs which do this

对的,这是可能的。快速谷歌搜索给出了这样做的程序列表

#7


According to the excellent article on Understanding Digital Image Interpolation by Sean McHugh:

根据Sean McHugh关于理解数字图像插值的优秀文章:

Interpolation also occurs each time you rotate or distort an image. (...) The 90° rotation is lossless because no pixel ever has to be repositioned onto the border between two pixels (and therefore divided).

每次旋转或扭曲图像时也会发生插值。 (...)90°旋转是无损的,因为没有像素必须重新定位在两个像素之间的边界上(因此被分割)。

and eventually concludes with

并最终以

avoid rotating your photos when possible; if an unleveled photo requires it, rotate no more than once.

尽可能避免旋转照片;如果照片需要不平整,请旋转不超过一次。

#8


If you are talking of rotating a JPEG image then there is no further compression right? It is about rotating pixel locations.

如果您正在谈论旋转JPEG图像,那么没有进一步压缩吗?它是关于旋转像素位置。

Doing rotation with any program will potentially change intermediate dimensions, as it needs to preserver original image, this may be an issue to consider.

使用任何程序进行旋转都可能会改变中间维度,因为它需要保留原始图像,这可能是需要考虑的问题。

#9


Unless you rotate by multiples of 90 degrees then your image will have to perform some kind of interpolation which might reduce the quality of your image. Using a good interpolation algorithm will help here.

除非您旋转90度的倍数,否则您的图像将必须执行某种插值,这可能会降低图像的质量。使用良好的插值算法将有助于此。

As for opening and recompressing, I am not sure you would actually get worse quality, but then I am not sure exactly how JPEG works.

至于打开和重新压缩,我不确定你的质量会不会更差,但是我不确定JPEG的工作原理。

I suggest you try to compress, manipulate and recompress and see for yourself if the result is good enough. What is good enough is subject to your application.

如果结果足够好,我建议您尝试压缩,操作和重新压缩并自己查看。什么是好的取决于您的申请。

#10


If it can help :

如果它可以帮助:

Trying to do better than the Microsoft (Windows 7) native picture viewer and its right-click rotation options, I tried several apps of the following link : http://jpegclub.org/losslessapps.html

尝试做得比Microsoft(Windows 7)原生图片查看器及其右键单击旋转选项更好,我尝试了以下链接的几个应用程序:http://jpegclub.org/losslessapps.html

For instance, I tried FastStone Image Viewer, XnView, Photosurfer, JPEG Lossless Rotator, ExifPro Image Viewer.

例如,我尝试了FastStone图像查看器,XnView,Photosurfer,JPEG无损旋转器,ExifPro图像查看器。

NONE of them yields a bigger picture than the basic Windows 7 picture viewer after a simple 90° rotation. It's admittedly limited to conclude so quickly, but I still don't have found a real lossless rotation .jpeg app for the moment, and in any case not better than the built-in Windows one.

在简单的90°旋转之后,它们中的任何一个都比基本的Windows 7图片查看器产生更大的图像。它被认为仅限于如此快速地结束,但我仍然没有找到真正的无损旋转.jpeg应用程序暂时,并且无论如何不比内置的Windows更好。

#1


Yes, it is possible for certain cases: 90-degree rotations and flips on images with dimensions that are a multiple of 8. The heart of the JPEG algorithm -- the lossy part -- involves breaking the image into 8x8 pixel blocks, performing a discrete cosine transform on the block and then quantizing the result. There's also some color space conversion and lossless compression of the blocks on top of this.

是的,对于某些情况是可能的:90度旋转和尺寸为8的倍数的图像翻转.JPEG算法的核心 - 有损部分 - 涉及将图像分成8x8像素块,执行块上的离散余弦变换然后量化结果。在此之上还有一些色块空间转换和块的无损压缩。

Rotating or flipping an 8x8 block will give a DCT with the same basic coefficients, but possibly transposed and/or with some sign changes depending on the transformation. So the basic steps to rotate or flip an image losslessly would involve:

旋转或翻转8x8块将使DCT具有相同的基本系数,但可能转换和/或根据变换进行一些符号变化。因此,无损旋转或翻转图像的基本步骤包括:

  1. Decompress and extract the blocks
  2. 解压缩并提取块

  3. Transpose and/or sign flip the DCT coefficients for each block
  4. 对每个块进行转置和/或符号翻转DCT系数

  5. Reshuffle the blocks into their new order (otherwise the 8x8 blocks would be rotated but still in the old place)
  6. 将块重新调整为新的顺序(否则8x8块将旋转但仍旧在旧位置)

  7. Recompress it all with the lossless compression steps.
  8. 使用无损压缩步骤将其全部重新压缩。

#2


There is a program named jpegtran

有一个名为jpegtran的程序

jpegtran – a utility for lossless transcoding between different JPEG formats.

jpegtran - 一种用于在不同JPEG格式之间进行无损转码的实用程序。

And Here is a list of applications which provide the JPEG lossless rotation feature based on the IJG code

以下是基于IJG代码提供JPEG无损旋转功能的应用程序列表

#3


Absolutely - just change the orientation value in the EXIF data. The vast majority of image programs will respect this setting and show the picture "rotated".

绝对 - 只需更改EXIF数据中的方向值即可。绝大多数图像程序将尊重此设置并显示图片“旋转”。

It's also possibly to "manually" (e.g. programatically) rotate the image in a lossless fashion if certain criteria are true - rotation must be 90/180 degrees and the width/height must multiples of the block-size. You can also flip/mirror it. I don't know whether image programs are smart enough to special-case this operation though. I would guess not.

如果某些标准为真,也可以“手动”(例如以编程方式)以无损方式旋转图像 - 旋转必须是90/180度,宽度/高度必须是块大小的倍数。你也可以翻转/镜像它。我不知道图像程序是否足够聪明,特殊情况下这个操作。我猜不会。

#4


From the JPEG FAQ:

来自JPEG FAQ:

"There are a few specialized operations that can be done on a JPEG file without decompressing it, and thus without incurring the generational loss that you'd normally get from loading and re-saving the image in a regular image editor. In particular it is possible to do 90-degree rotations and flips losslessly, if the image dimensions are a multiple of the file's block size (typically 16x16, 16x8, or 8x8 pixels for color JPEGs).
...

“有一些专门的操作可以在JPEG文件上完成,无需解压缩,因此不会产生通常从加载和重新保存图像的常规图像编辑器中产生的代数损失。特别是如果图像尺寸是文件块大小的倍数(对于彩色JPEG通常为16x16,16x8或8x8像素),则可以无损地进行90度旋转和翻转。...

But you do need special software; rotating the image in a regular image editor won't be lossless."

但你确实需要特殊的软件;在常规图像编辑器中旋转图像不会是无损的。“

#5


Not a jpg expert, but it seems that the answer would be Yes for 90, 180, 270 degree rotations. (maybe even for 360! :))

不是jpg专家,但似乎对于90,180,270度旋转的答案是肯定的。 (甚至可能是360!:))

#6


Yes, it is possible.
A quick google search gave this list of programs which do this

对的,这是可能的。快速谷歌搜索给出了这样做的程序列表

#7


According to the excellent article on Understanding Digital Image Interpolation by Sean McHugh:

根据Sean McHugh关于理解数字图像插值的优秀文章:

Interpolation also occurs each time you rotate or distort an image. (...) The 90° rotation is lossless because no pixel ever has to be repositioned onto the border between two pixels (and therefore divided).

每次旋转或扭曲图像时也会发生插值。 (...)90°旋转是无损的,因为没有像素必须重新定位在两个像素之间的边界上(因此被分割)。

and eventually concludes with

并最终以

avoid rotating your photos when possible; if an unleveled photo requires it, rotate no more than once.

尽可能避免旋转照片;如果照片需要不平整,请旋转不超过一次。

#8


If you are talking of rotating a JPEG image then there is no further compression right? It is about rotating pixel locations.

如果您正在谈论旋转JPEG图像,那么没有进一步压缩吗?它是关于旋转像素位置。

Doing rotation with any program will potentially change intermediate dimensions, as it needs to preserver original image, this may be an issue to consider.

使用任何程序进行旋转都可能会改变中间维度,因为它需要保留原始图像,这可能是需要考虑的问题。

#9


Unless you rotate by multiples of 90 degrees then your image will have to perform some kind of interpolation which might reduce the quality of your image. Using a good interpolation algorithm will help here.

除非您旋转90度的倍数,否则您的图像将必须执行某种插值,这可能会降低图像的质量。使用良好的插值算法将有助于此。

As for opening and recompressing, I am not sure you would actually get worse quality, but then I am not sure exactly how JPEG works.

至于打开和重新压缩,我不确定你的质量会不会更差,但是我不确定JPEG的工作原理。

I suggest you try to compress, manipulate and recompress and see for yourself if the result is good enough. What is good enough is subject to your application.

如果结果足够好,我建议您尝试压缩,操作和重新压缩并自己查看。什么是好的取决于您的申请。

#10


If it can help :

如果它可以帮助:

Trying to do better than the Microsoft (Windows 7) native picture viewer and its right-click rotation options, I tried several apps of the following link : http://jpegclub.org/losslessapps.html

尝试做得比Microsoft(Windows 7)原生图片查看器及其右键单击旋转选项更好,我尝试了以下链接的几个应用程序:http://jpegclub.org/losslessapps.html

For instance, I tried FastStone Image Viewer, XnView, Photosurfer, JPEG Lossless Rotator, ExifPro Image Viewer.

例如,我尝试了FastStone图像查看器,XnView,Photosurfer,JPEG无损旋转器,ExifPro图像查看器。

NONE of them yields a bigger picture than the basic Windows 7 picture viewer after a simple 90° rotation. It's admittedly limited to conclude so quickly, but I still don't have found a real lossless rotation .jpeg app for the moment, and in any case not better than the built-in Windows one.

在简单的90°旋转之后,它们中的任何一个都比基本的Windows 7图片查看器产生更大的图像。它被认为仅限于如此快速地结束,但我仍然没有找到真正的无损旋转.jpeg应用程序暂时,并且无论如何不比内置的Windows更好。