Skip to content

JPG resolution drops to 96 dpi #3017

Closed
Closed

Description

@AdiLew

Hi,
I have a few scripts that I'm using to flip and rotate JPG files.
Below is the rotating function:

async function rotateImage(targetFilePath, deg = 0) {
    const sharpImage = sharp(targetFilePath)
    sharpImage.rotate(deg);
    const buffer = await sharpImage.toBuffer();
    await sharp(buffer).toFile(targetFilePath)
}

The image I'm processing is available here:
https://drive.google.com/file/d/1WmqFj14LaUA0OAgWiiTgWUiTfLDaCqwj/view?usp=sharing

The rotation works well, but it seems that the images lose resolution and drop from 600 dpi to 96 dpi.
Is there a way I can keep the original image's resolution while processing it?

Thanks!

Activity

  1. lovell commented on Dec 17, 2021

    @lovell
    Owner

    JPEG doesn't have a concept of resolution, but this image has EXIF metadata:

    $ vipsheader -a I600.jpg | grep Resolution
    exif-ifd0-XResolution: 600000/1000 (600.000, Rational, 1 components, 8 bytes)
    exif-ifd0-YResolution: 600000/1000 (600.000, Rational, 1 components, 8 bytes)
    exif-ifd0-ResolutionUnit: 2 (Inch, Short, 1 components, 2 bytes)
    

    Please see https://sharp.pixelplumbing.com/api-output#withmetadata for control over this.

  2. AdiLew commented on Dec 18, 2021

    @AdiLew
    Author

    I've tried adding the withMetadata() method, to keep all original exif like so, but it doesn't seem to work.

    async function rotateImage(targetFilePath, deg = 0) {
        const sharpImage = sharp(targetFilePath).withMetadata();
    
        sharpImage.rotate(deg);
        const buffer = await sharpImage.toBuffer();
        await sharp(buffer).toFile(targetFilePath)
    }
    

    I've also tried dictating a hardcoded value for the X and Y resolution, as attached below, but the result is still 96dpi:

    async function rotateImage(targetFilePath, deg = 0) {
        const exifObject = {
            exif: {
                IFD0: {
                        XResolution: '600 dpi',
                        YResolution: '600 dpi'
                }
            }
        };
        const sharpImage = sharp(targetFilePath).withMetadata(exifObject);
        console.dir(sharpImage);
        sharpImage.rotate(deg);
        const buffer = await sharpImage.toBuffer();
        await sharp(buffer).toFile(targetFilePath)
    }
    

    Is there a working example for retaining both of these values?

  3. lovell commented on Dec 20, 2021

    @lovell
    Owner

    Did you see the example at https://sharp.pixelplumbing.com/api-output#withmetadata

    // Set output metadata to 96 DPI
    const data = await sharp(input)
      .withMetadata({ density: 96 })
      .toBuffer();

    (I need to fix the formatting of this example in the docs as it's currently being displayed as part of the previous example.)

  4. AdiLew commented on Jan 1, 2022

    @AdiLew
    Author

    I did, the density property did not seem to have any affect over the output file's x and y resolution.
    I also looked in the output unit tests and tries to imitate the way the exif should have been dictated, but it didn't seem to work either.
    Is this the proper way to describe the exif data?

    {
            exif: {
                IFD0: {
    
                    XResolution: '600dpi',
                    YResolution: '600dpi'
                }
            }
    
  5. lovell commented on Apr 18, 2022

    @lovell
    Owner

    @AdiLew Were you able to make any progress with this? Did you try .withMetadata({ density: 600 })?

  6. lovell commented on May 3, 2022

    @lovell
    Owner

    Closing due to inactivity but please feel free to reopen with more details if further help is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions