python-imaging-library - 图像的 DPI 信息到底存储在哪里(Exif 除外)?

标签 python-imaging-library exif dpi exiftool

几年来,我们一直在使用名为 Onyx RIPCenter 的程序来管理海报打印。这些图像是使用 Photoshop 生成的,并加载到 Windows 上的 Onyx 应用程序上。

最近,我们决定通过消除 Photoshop 来自动化工作流程,并通过 python 内置的自定义应用程序自动处理图像(使用pillow python - https://pillow.readthedocs.io/en/latest/)

现在我遇到的问题是,当我在 RIPCenter 中打开通过自定义应用程序创建的图像时,我得到的尺寸以毫米为单位是错误的。

图像的实际大小是, 924mm x 2108mm 相反我得到, 2309.99mm x 5269.79mm

原因是应用程序读取了错误的 DPI,它读取的不是 180,而是 72(实际大小以像素为单位 - 6548 x 14938)。

自定义应用程序确实将所有相关信息设置为 Exif 数据,包括 DPI。这可以通过检查 Windows 中图像文件的属性来验证。但不知何故,当在 RIPCenter 中打开图像时,它显示错误的 DPI 值。

这是保存图像的代码。

def save_image(modified_image, image_file, source_image):
    """
    Store the processed image into a file
    :param modified_image: <Obj> modified image object
    :param image_file: <String> output filename
    :param source_image: <Obj> original image object
    :return: <Boolean> True if saved
    """

    # fix Onyx printer error
    # https://media.readthedocs.org/pdf/piexif/latest/piexif.pdf
    exif_dict = piexif.load(source_image.info["exif"])
    exif_dict["0th"][piexif.ImageIFD.XResolution] = (180.0, 1)
    exif_dict["0th"][piexif.ImageIFD.YResolution] = (180.0, 1)
    exif_bytes = piexif.dump(exif_dict)

    # if this is not set, then the save option will throw an error for setting subsampling='keep'
    modified_image.format = source_image.format

    try:
        modified_image.save(
            image_file,
            icc_profile=source_image.info.get('icc_profile'),
            dpi=source_image.info.get('dpi'),
            exif=exif_bytes,
            format=source_image.format,
            subsampling='keep',
            quality=95
        )

        del source_image
        del modified_image

        return True
    except IOError as e:
        print(e)

    return None

如果在 Photoshop 中打开并保存同一张图像,则会显示正确的 DPI。我也尝试过 Gimp,得到了同样的结果。在 Gimp 中打开并保存时,DPI 错误的图像突然获得正确的 DPI。

我的理解是,像素(高度和宽度)和 DPI 值是从不同平台和应用程序的 Exif 数据中读取的。但 Onyx RIPCenter 应用程序有可能在其他地方寻找 DPI 吗?

以下是图像的 exif 数据。

保存在 Photoshop 中的图像的 Exif 数据,

[ExifTool]      ExifTool Version Number         : 11.10
[System]        File Name                       : A3-VictoriaGlenManor-RH-Copper-92.4x210.8-R.jpg
[System]        Directory                       : /Users/vinodsudharshan/Downloads
[System]        File Size                       : 34 MB
[System]        File Modification Date/Time     : 2018:08:24 14:37:25+02:00
[System]        File Access Date/Time           : 2018:08:27 23:27:08+02:00
[System]        File Inode Change Date/Time     : 2018:08:27 22:56:37+02:00
[System]        File Permissions                : rw-r--r--
[File]          File Type                       : JPEG
[File]          File Type Extension             : jpg
[File]          MIME Type                       : image/jpeg
[File]          Exif Byte Order                 : Big-endian (Motorola, MM)
[File]          Current IPTC Digest             : cdcffa7da8c7be09057076aeaf05c34e
[File]          Image Width                     : 6550
[File]          Image Height                    : 14940
[File]          Encoding Process                : Baseline DCT, Huffman coding
[File]          Bits Per Sample                 : 8
[File]          Color Components                : 4
[IFD0]          Image Width                     : 6130
[IFD0]          Image Height                    : 8400
[IFD0]          Bits Per Sample                 : 8 8 8
[IFD0]          Photometric Interpretation      : RGB
[IFD0]          Orientation                     : Horizontal (normal)
[IFD0]          Samples Per Pixel               : 3
[IFD0]          X Resolution                    : 180
[IFD0]          Y Resolution                    : 180
[IFD0]          Resolution Unit                 : inches
[IFD0]          Software                        : Adobe Photoshop CS5.1 Macintosh
[IFD0]          Modify Date                     : 2018:08:23 17:32:21
[ExifIFD]       Exif Version                    : 0221
[ExifIFD]       Color Space                     : Uncalibrated
[ExifIFD]       Exif Image Width                : 6550
[ExifIFD]       Exif Image Height               : 14940
[IFD1]          Compression                     : JPEG (old-style)
[IFD1]          X Resolution                    : 72
[IFD1]          Y Resolution                    : 72
[IFD1]          Resolution Unit                 : inches
[IFD1]          Thumbnail Offset                : 394
[IFD1]          Thumbnail Length                : 2261
[IFD1]          Thumbnail Image                 : (Binary data 2261 bytes, use -b option to extract)
[IPTC]          Coded Character Set             : UTF8
[IPTC]          Application Record Version      : 0
[Photoshop]     IPTC Digest                     : cdcffa7da8c7be09057076aeaf05c34e
[Photoshop]     X Resolution                    : 180
[Photoshop]     Displayed Units X               : inches
[Photoshop]     Y Resolution                    : 180
[Photoshop]     Displayed Units Y               : inches
[Photoshop]     Print Style                     : Centered
[Photoshop]     Print Position                  : 0 0
[Photoshop]     Print Scale                     : 1
[Photoshop]     Global Angle                    : 30
[Photoshop]     Global Altitude                 : 30
[Photoshop]     URL List                        : 
[Photoshop]     Slices Group Name               : A3-VictoriaGlenManor-RH-Copper-92.4x210.8-R
[Photoshop]     Num Slices                      : 1
[Photoshop]     Pixel Aspect Ratio              : 1
[Photoshop]     Photoshop Thumbnail             : (Binary data 2261 bytes, use -b option to extract)
[Photoshop]     Has Real Merged Data            : Yes
[Photoshop]     Writer Name                     : Adobe Photoshop
[Photoshop]     Reader Name                     : Adobe Photoshop CS5.1
[Photoshop]     Photoshop Quality               : 12
[Photoshop]     Photoshop Format                : Standard
[Photoshop]     Progressive Scans               : 3 Scans
[XMP-x]         XMP Toolkit                     : Adobe XMP Core 5.0-c061 64.140949, 2010/12/07-10:57:01
[XMP-xmp]       Modify Date                     : 2018:08:23 17:32:21+02:00
[XMP-xmp]       Creator Tool                    : Adobe Photoshop CS5.1 Macintosh
[XMP-xmp]       Create Date                     : 2018:08:21 15:12:04+02:00
[XMP-xmp]       Metadata Date                   : 2018:08:23 17:32:21+02:00
[XMP-crs]       Already Applied                 : True
[XMP-photoshop] Color Mode                      : CMYK
[XMP-photoshop] ICC Profile Name                : U.S. Web Coated (SWOP) v2
[XMP-dc]        Format                          : image/jpeg
[XMP-xmpMM]     Instance ID                     : xmp.iid:9F9A62F1B5206811BF6AFA75AB99E0BD
[XMP-xmpMM]     Document ID                     : xmp.did:9E9A62F1B5206811BF6AFA75AB99E0BD
[XMP-xmpMM]     Original Document ID            : xmp.did:9E9A62F1B5206811BF6AFA75AB99E0BD
[XMP-xmpMM]     History Action                  : saved, saved
[XMP-xmpMM]     History Instance ID             : xmp.iid:9E9A62F1B5206811BF6AFA75AB99E0BD, xmp.iid:9F9A62F1B5206811BF6AFA75AB99E0BD
[XMP-xmpMM]     History When                    : 2018:08:23 17:32:21+02:00, 2018:08:23 17:32:21+02:00
[XMP-xmpMM]     History Software Agent          : Adobe Photoshop CS5.1 Macintosh, Adobe Photoshop CS5.1 Macintosh
[XMP-xmpMM]     History Changed                 : /, /
[ICC-header]    Profile CMM Type                : Adobe Systems Inc.
[ICC-header]    Profile Version                 : 2.1.0
[ICC-header]    Profile Class                   : Output Device Profile
[ICC-header]    Color Space Data                : CMYK
[ICC-header]    Profile Connection Space        : Lab
[ICC-header]    Profile Date Time               : 2000:07:26 05:41:53
[ICC-header]    Profile File Signature          : acsp
[ICC-header]    Primary Platform                : Apple Computer Inc.
[ICC-header]    CMM Flags                       : Not Embedded, Independent
[ICC-header]    Device Manufacturer             : Adobe Systems Inc.
[ICC-header]    Device Model                    : 
[ICC-header]    Device Attributes               : Reflective, Glossy, Positive, Color
[ICC-header]    Rendering Intent                : Media-Relative Colorimetric
[ICC-header]    Connection Space Illuminant     : 0.9642 1 0.82491
[ICC-header]    Profile Creator                 : Adobe Systems Inc.
[ICC-header]    Profile ID                      : 0
[ICC_Profile]   Profile Description             : U.S. Web Coated (SWOP) v2
[ICC_Profile]   Profile Copyright               : Copyright 2000 Adobe Systems, Inc.
[ICC_Profile]   Media White Point               : 0.7084 0.73595 0.57104
[ICC_Profile]   A To B0                         : (Binary data 41478 bytes, use -b option to extract)
[ICC_Profile]   A To B2                         : (Binary data 41478 bytes, use -b option to extract)
[ICC_Profile]   A To B1                         : (Binary data 41478 bytes, use -b option to extract)
[ICC_Profile]   B To A0                         : (Binary data 145588 bytes, use -b option to extract)
[ICC_Profile]   B To A1                         : (Binary data 145588 bytes, use -b option to extract)
[ICC_Profile]   B To A2                         : (Binary data 145588 bytes, use -b option to extract)
[ICC_Profile]   Gamut                           : (Binary data 37009 bytes, use -b option to extract)
[Adobe]         DCT Encode Version              : 100
[Adobe]         APP14 Flags 0                   : [14]
[Adobe]         APP14 Flags 1                   : (none)
[Adobe]         Color Transform                 : YCCK
[Composite]     Image Size                      : 6550x14940
[Composite]     Megapixels                      : 97.9

保存在Python应用程序中的图像的Exif数据,

[ExifTool]      ExifTool Version Number         : 11.10
[System]        File Name                       : A3-TEST_2_EXIF-RH-Copper-92.4x210.8-R.jpg
[System]        Directory                       : /Users/vinodsudharshan/Desktop
[System]        File Size                       : 36 MB
[System]        File Modification Date/Time     : 2018:08:27 21:55:25+02:00
[System]        File Access Date/Time           : 2018:08:31 16:59:05+02:00
[System]        File Inode Change Date/Time     : 2018:08:27 22:56:37+02:00
[System]        File Permissions                : rw-r--r--
[File]          File Type                       : JPEG
[File]          File Type Extension             : jpg
[File]          MIME Type                       : image/jpeg
[File]          Exif Byte Order                 : Big-endian (Motorola, MM)
[File]          Image Width                     : 6548
[File]          Image Height                    : 14938
[File]          Encoding Process                : Baseline DCT, Huffman coding
[File]          Bits Per Sample                 : 8
[File]          Color Components                : 4
[Adobe]         DCT Encode Version              : 100
[Adobe]         APP14 Flags 0                   : (none)
[Adobe]         APP14 Flags 1                   : (none)
[Adobe]         Color Transform                 : Unknown (RGB or CMYK)
[IFD0]          Image Width                     : 6130
[IFD0]          Image Height                    : 8400
[IFD0]          Bits Per Sample                 : 8 8 8
[IFD0]          Photometric Interpretation      : RGB
[IFD0]          Orientation                     : Horizontal (normal)
[IFD0]          Samples Per Pixel               : 3
[IFD0]          X Resolution                    : 180
[IFD0]          Y Resolution                    : 180
[IFD0]          Resolution Unit                 : inches
[IFD0]          Software                        : Adobe Photoshop CS5.1 Macintosh
[IFD0]          Modify Date                     : 2018:07:11 17:42:24
[ExifIFD]       Exif Version                    : 0221
[ExifIFD]       Color Space                     : Uncalibrated
[ExifIFD]       Exif Image Width                : 5669
[ExifIFD]       Exif Image Height               : 14173
[IFD1]          Compression                     : JPEG (old-style)
[IFD1]          X Resolution                    : 180
[IFD1]          Y Resolution                    : 180
[IFD1]          Resolution Unit                 : inches
[IFD1]          Thumbnail Offset                : 404
[IFD1]          Thumbnail Length                : 2076
[IFD1]          Thumbnail Image                 : (Binary data 2076 bytes, use -b option to extract)
[ICC-header]    Profile CMM Type                : Adobe Systems Inc.
[ICC-header]    Profile Version                 : 2.1.0
[ICC-header]    Profile Class                   : Output Device Profile
[ICC-header]    Color Space Data                : CMYK
[ICC-header]    Profile Connection Space        : Lab
[ICC-header]    Profile Date Time               : 2000:07:26 05:41:53
[ICC-header]    Profile File Signature          : acsp
[ICC-header]    Primary Platform                : Apple Computer Inc.
[ICC-header]    CMM Flags                       : Not Embedded, Independent
[ICC-header]    Device Manufacturer             : Adobe Systems Inc.
[ICC-header]    Device Model                    : 
[ICC-header]    Device Attributes               : Reflective, Glossy, Positive, Color
[ICC-header]    Rendering Intent                : Media-Relative Colorimetric
[ICC-header]    Connection Space Illuminant     : 0.9642 1 0.82491
[ICC-header]    Profile Creator                 : Adobe Systems Inc.
[ICC-header]    Profile ID                      : 0
[ICC_Profile]   Profile Description             : U.S. Web Coated (SWOP) v2
[ICC_Profile]   Profile Copyright               : Copyright 2000 Adobe Systems, Inc.
[ICC_Profile]   Media White Point               : 0.7084 0.73595 0.57104
[ICC_Profile]   A To B0                         : (Binary data 41478 bytes, use -b option to extract)
[ICC_Profile]   A To B2                         : (Binary data 41478 bytes, use -b option to extract)
[ICC_Profile]   A To B1                         : (Binary data 41478 bytes, use -b option to extract)
[ICC_Profile]   B To A0                         : (Binary data 145588 bytes, use -b option to extract)
[ICC_Profile]   B To A1                         : (Binary data 145588 bytes, use -b option to extract)
[ICC_Profile]   B To A2                         : (Binary data 145588 bytes, use -b option to extract)
[ICC_Profile]   Gamut                           : (Binary data 37009 bytes, use -b option to extract)
[Composite]     Image Size                      : 6548x14938
[Composite]     Megapixels                      : 97.8

最佳答案

我怀疑 Onyx RIPCenter 使用 72 作为默认值(这是常见的默认值),并且它通常读取 Photoshop:X/YResolution 来获取 DPI。尝试将 IFD0:X/YResolution 复制到 Photoshop:X/YResolution 或简单地将 Photoshop:X/YResolution 设置为 180。

在 exiftool 中,您可以使用
exiftool -Photoshop:*分辨率=180 文件或目录

星号是一个通配符,允许同时设置XResolutionYResolution。将 FILEorDIR 替换为要处理的文件/目录。此命令创建备份文件。添加 -overwrite_original 以禁止创建备份文件。添加 -r 以递归到子目录。

关于python-imaging-library - 图像的 DPI 信息到底存储在哪里(Exif 除外)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52131657/

相关文章:

c# - 重新计算 RichTextBox 控件的宽度时遇到问题

python - PIL 库 Image.fromarray() 导致 AttributeError : 'tuple' object has no attribute '__array_interface__'

python - 如何将负 (<0) RGB 颜色值放入 PIL 中的图像?

python - 如何使用 PIL 库查找子图像?

c# - 用于无损 Exif 重写的 .NET C# 库?

javascript - node.js 在 JPEG 中创建/修改 Exif

css - 为什么 dpi 设置超过 96 会搞砸我的网站?

c# - DPI 图形屏幕分辨率像素 WinForm PrintPageEventArgs

python - 如何在 Macosx 10.9 上安装 PIL?

Android ExifInterface saveAttributes() 不覆盖以前的数据?