Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is it not possible to set jfif data when saving a jpeg file? #3328

Closed
vinodMS opened this issue Sep 2, 2018 · 3 comments · Fixed by #4639
Closed

Why is it not possible to set jfif data when saving a jpeg file? #3328

vinodMS opened this issue Sep 2, 2018 · 3 comments · Fixed by #4639
Labels
JPEG Platform A catchall for platform-related

Comments

@vinodMS
Copy link

vinodMS commented Sep 2, 2018

What did you do?

I'm trying to modify and save an image with jfif data, because an application I am using (Onyx RipCenter) requires this information to set the DPI. Currently it shows incorrect width and height because it has the wrong DPI. Instead of 180, it shows 72.

What did you expect to happen?

That the new image contains jfif application marker

What actually happened?

contains no jfif data. According to the doc here - https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg you could get jfif when opening an image file but cannot save the same info. Why is that?

What versions of Pillow and Python are you using?

Python 3.6 and Pillow 5.2.0

from PIL import Image, ImageOps
Image.MAX_IMAGE_PIXELS = None
image_file = 'in.jpg'
image_file_out = 'out.jpg'
source_image = Image.open(image_file)

def cm_to_pixel(cms):
    one_cm = 180 / 2.54  # 1 inch is equal to 2.54 centimeters.

    return int(cms * one_cm)

modified_image = source_image.resize(
                size=(cm_to_pixel(92.4), cm_to_pixel(210.8)),
                resample=0
            )
modified_image.format = source_image.format
modified_image.save(
            image_file_out,
            icc_profile= source_image.info.get('icc_profile'),
            dpi= source_image.info.get('dpi'),
            format= source_image.format,
            exif= source_image.info.get('exif'),
            subsampling='keep',
            quality=95,
            jfif= source_image.info.get('jfif'),
            jfif_version= source_image.info.get('jfif_version'),
            jfif_density= source_image.info.get('jfif_density'),
            jfif_unit= source_image.info.get('jfif_unit')
        )

Source Image

[ExifTool]      ExifTool Version Number         : 11.10
[System]        File Name                       : A3-TEST_7_EXIF-RH-Copper-92.4x210.8-R.jpg
[System]        Directory                       : .
[System]        File Size                       : 36 MB
[System]        File Modification Date/Time     : 2018:09:02 02:55:26+02:00
[System]        File Access Date/Time           : 2018:09:02 22:02:05+02:00
[System]        File Inode Change Date/Time     : 2018:09:02 21:47:34+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
[JFIF]          JFIF Version                    : 1.02
[JFIF]          Resolution Unit                 : inches
[JFIF]          X Resolution                    : 180
[JFIF]          Y Resolution                    : 180
[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                    : 72
[IFD1]          Y Resolution                    : 72
[IFD1]          Resolution Unit                 : inches
[IFD1]          Thumbnail Offset                : 412
[IFD1]          Thumbnail Length                : 2106
[IFD1]          Thumbnail Image                 : (Binary data 2106 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

Target Image

[ExifTool]      ExifTool Version Number         : 11.10
[System]        File Name                       : out.jpg
[System]        Directory                       : /Users/vinodsudharshan/Desktop/Print Files
[System]        File Size                       : 37 MB
[System]        File Modification Date/Time     : 2018:09:02 22:59:23+02:00
[System]        File Access Date/Time           : 2018:09:02 22:59:34+02:00
[System]        File Inode Change Date/Time     : 2018:09:02 22:59:33+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                    : 72
[IFD1]          Y Resolution                    : 72
[IFD1]          Resolution Unit                 : inches
[IFD1]          Thumbnail Offset                : 410
[IFD1]          Thumbnail Length                : 2106
[IFD1]          Thumbnail Image                 : (Binary data 2106 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
@radarhere
Copy link
Member

Hi. Just to note in this issue, given that you’re using ONYX RIPCenter, I conclude that you’re using Windows.

My partial answer is that I think Pillow does actually write JFIF data - https://github.com/python-pillow/Pillow/blob/master/src/libImaging/JpegEncode.c#L221

  • The resolution is set using the 'dpi' key that you're already using.
  • density_unit is fixed at 1.
  • I don’t think this is low level enough for you to arbitrarily set the JFIF version. I would guess that is set by libjpeg.

All that said, I’m surprised that ‘JFIF version’ is not present on out.jpg. What’s the exact exiftool command that you’re running? Are you able to upload in.jpg?

@vinodMS
Copy link
Author

vinodMS commented Sep 3, 2018

Hi, I'm actually using OSX for development and the production application is running on Ubuntu. ONYX RIPCenter is used by the printing company.

Is it possible that pillow writes JFIF data only when the JPEG image is progressive? - https://github.com/python-pillow/Pillow/blob/master/src/libImaging/JpegEncode.c#L217

Here is the exiftool(v11.10) command I use,
exiftool out.jpg -a -G1 -w txt

I do have to mention one thing though, when I use exiftool to set JFIF, it fails when the adobe application marker is present. This is the warning I get "Warning = Not creating JFIF in JPEG with Adobe APP14".

The workaround I found is,

exiftool -Adobe= out.jpg
exiftool -JFIF:*Resolution=180 out.jpg

Here is the file,
in

@radarhere radarhere added the JPEG label Sep 4, 2018
@aclark4life aclark4life added the Platform A catchall for platform-related label May 11, 2019
@radarhere
Copy link
Member

Some notes

  • modified_image.format = source_image.format. There's no need for this line. It wouldn't have any effect on which format is chosen to save the image. That is instead determined by the extension of the destination file path, or the format argument for save(). For an explanation of why format became None after resize(), see this comment.
  • jfif, jfif_version, jfif_density and jfif_unit have no effect as save() arguments at present.

When you say that the DPI changes on saving, I don't think this is quite so simple. When I inspect the original and the saved images in macOS Preview, they both have a DPI of 180.

Your exiftool output shows for the original image

  • JFIF X Resolution 180
  • IFD0 X Resolution 180
  • IFD1 X Resolution 72

and for the output image

  • IFD0 X Resolution 180
  • IFD1 X Resolution 72

So, back to the basic question, why is the JFIF X Resolution missing on save? I've created PR #4639 to fix this.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JPEG Platform A catchall for platform-related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants