我正在将一些图像处理功能从.NET移植到Python,但输出图像必须以与.NET相同的方式进行压缩。然而,当我在text-compare等工具上比较
例如: Python
现在表格相同,但文件之间的差异保持不变。JPEGsnoop显示的唯一差异是在“压缩统计”和“霍夫曼编码直方图统计”中。
.jpg
输出文件并选择Ignore nothing
时,这些文件的压缩方式存在显著差异。例如: Python
bmp = PIL.Image.open('marbles.bmp')
bmp.save(
'output_python.jpg',
format='jpeg',
dpi=(300,300),
subsampling=2,
quality=75
)
.NET
ImageCodecInfo jgpEncoder = ImageCodecInfo.GetImageDecoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters myEncoderParameters = new EncoderParameters(1);
myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 75L);
Bitmap bmp = new Bitmap(directory + "marbles.bmp");
bmp.Save(directory + "output_net.jpg", jgpEncoder, myEncoderParameters);
exiftool output_python.jpg -a -G1 -w txt
[ExifTool] ExifTool Version Number : 12.31
[System] File Name : output_python.jpg
[System] Directory : .
[System] File Size : 148 KiB
[System] File Modification Date/Time : 2021:09:28 09:19:20-06:00
[System] File Access Date/Time : 2021:09:28 09:19:21-06:00
[System] File Creation Date/Time : 2021:09:27 21:33:35-06:00
[System] File Permissions : -rw-rw-rw-
[File] File Type : JPEG
[File] File Type Extension : jpg
[File] MIME Type : image/jpeg
[File] Image Width : 1419
[File] Image Height : 1001
[File] Encoding Process : Baseline DCT, Huffman coding
[File] Bits Per Sample : 8
[File] Color Components : 3
[File] Y Cb Cr Sub Sampling : YCbCr4:2:0 (2 2)
[JFIF] JFIF Version : 1.01
[JFIF] Resolution Unit : inches
[JFIF] X Resolution : 300
[JFIF] Y Resolution : 300
[Composite] Image Size : 1419x1001
[Composite] Megapixels : 1.4
exiftool output_net.jpg -a -G1 -w txt
[ExifTool] ExifTool Version Number : 12.31
[System] File Name : output_net.jpg
[System] Directory : .
[System] File Size : 147 KiB
[System] File Modification Date/Time : 2021:09:28 09:18:05-06:00
[System] File Access Date/Time : 2021:09:28 09:18:52-06:00
[System] File Creation Date/Time : 2021:09:27 21:32:19-06:00
[System] File Permissions : -rw-rw-rw-
[File] File Type : JPEG
[File] File Type Extension : jpg
[File] MIME Type : image/jpeg
[File] Image Width : 1419
[File] Image Height : 1001
[File] Encoding Process : Baseline DCT, Huffman coding
[File] Bits Per Sample : 8
[File] Color Components : 3
[File] Y Cb Cr Sub Sampling : YCbCr4:2:0 (2 2)
[JFIF] JFIF Version : 1.01
[JFIF] Resolution Unit : inches
[JFIF] X Resolution : 300
[JFIF] Y Resolution : 300
[Composite] Image Size : 1419x1001
[Composite] Megapixels : 1.4
文本比较的差异
问题
- 假设这两种JPEG压缩的实现可以产生完全相同的输出文件,这是合理的吗?
- 如果是这样,
PIL
或System.Drawing.Image
是否执行了任何额外的步骤,如反锯齿,使结果不同? - 还是
PIL
.save()
有其他参数可以使其更像C#中的JPEG编码器?
谢谢
更新
根据 Jeremy's recommendation,我使用 JPEGsnoop比较了文件之间的更多细节,并发现亮度和色度表是不同的。我修改了代码:
bmp = PIL.Image.open('marbles.bmp')
output_net = PIL.Image.open('output_net.jpg')
bmp.save(
'output_python.jpg',
format='jpeg',
dpi=(300,300),
subsampling=2,
qtables=output_net.quantization,
#quality=75
)
现在表格相同,但文件之间的差异保持不变。JPEGsnoop显示的唯一差异是在“压缩统计”和“霍夫曼编码直方图统计”中。
output_net.jpeg
*** Decoding SCAN Data ***
OFFSET: 0x0000026F
Scan Decode Mode: Full IDCT (AC + DC)
Scan Data encountered marker 0xFFD9 @ 0x00024BE7.0
Compression stats:
Compression Ratio: 28.43:1
Bits per pixel: 0.84:1
Huffman code histogram stats:
Huffman Table: (Dest ID: 0, Class: DC)
# codes of length 01 bits: 0 ( 0%)
# codes of length 02 bits: 1664 ( 7%)
# codes of length 03 bits: 18238 ( 81%)
# codes of length 04 bits: 1807 ( 8%)
# codes of length 05 bits: 715 ( 3%)
# codes of length 06 bits: 4 ( 0%)
# codes of length 07 bits: 0 ( 0%)
...
output_python.jpg
*** Decoding SCAN Data ***
OFFSET: 0x0000026F
Scan Decode Mode: Full IDCT (AC + DC)
Scan Data encountered marker 0xFFD9 @ 0x00025158.0
Compression stats:
Compression Ratio: 28.17:1
Bits per pixel: 0.85:1
Huffman code histogram stats:
Huffman Table: (Dest ID: 0, Class: DC)
# codes of length 01 bits: 0 ( 0%)
# codes of length 02 bits: 1659 ( 7%)
# codes of length 03 bits: 18247 ( 81%)
# codes of length 04 bits: 1807 ( 8%)
# codes of length 05 bits: 711 ( 3%)
# codes of length 06 bits: 4 ( 0%)
# codes of length 07 bits: 0 ( 0%)
...
我现在正在寻找一种通过 PIL
同步这些值的方法。
subsampling=1
进行测试吗? - user16930239