Skip to content

Add withDensity for fine-grained control over EXIF resultion data #4496

Closed

Description

@ctlcltd

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • Running npm install sharp completes without error.
  • Running node -e "require('sharp')" completes without error.

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

Does this problem relate to file caching?

The default behaviour of libvips is to cache input files, which can lead to EBUSY or EPERM errors on Windows.
Use sharp.cache(false) to switch this feature off.

  • Adding sharp.cache(false) does not fix this problem.

Does this problem relate to images appearing to have been rotated by 90 degrees?

Images that contain EXIF Orientation metadata are not auto-oriented. By default, EXIF metadata is removed.

  • To auto-orient pixel values use the parameter-less rotate() operation.

  • To retain EXIF Orientation use keepExif().

  • Using rotate() or keepExif() does not fix this problem.

What are the steps to reproduce?

Trying to resize a 96dpi image using withExif(object) feature, the default option density: 72 is not respected.

The resulting image has 96dpi in metadata.

And there is no way to strip Xmp, Iptc and ICC profile, when using withMetadata({density: 72}).

What is the expected behaviour?

The default option density: 72 is respected.

A way to strip Xmp, Iptc and ICC profile, cloud be desiderable.

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

const sharp = require('sharp');

const exif = {IFD0: {UserComment: 'A comment'}};

sharp(image.jpg, {
  density: 72 // default
})
  .resize({width: 600})
  .jpeg({trellisQuantisation: true})
  .withExif(exif)
  .toFile(out.jpg) // 96dpi from metadata (strip all, Exif)
  .then(info => console.log(info))
  .catch(err => console.error(err));

/* attempt to force 72dpi metadata */
sharp(image.jpg, {
  density: 72 // default
})
  .resize({width: 600})
  .jpeg({trellisQuantisation: true})
  .withMetadata({
    density: 72 // forces 72dpi metadata
    //, exif // same as below
  })
  .withExif(exif)
  .toFile(out.jpg) // 72dpi from metadata (Exif, no strip Xmp Iptc ICC)
  .then(info => console.log(info))
  .catch(err => console.error(err));

Please provide sample image(s) that help explain this problem

Image

Activity

  1. ctlcltd commented on Feb 8, 2026

    @ctlcltd
    Author

    I solved with this workaround, to obtain strip all and 72dpi from metadata.

    sharp(image.jpg, {
      density: 72 // default
    })
      .resize({width: 600})
      .jpeg({trellisQuantisation: true})
      .toBuffer()
        .then(data => {
          new sharp(data)
            .withExif({IFD0: {UserComment: 'A comment'})
            .toFile(out.jpg)
            .then(info => console.log(info))
            .catch(err => console.error(err))
        })
        .catch(err => console.error(err));

    Thank you so much, very happy to use your library.

  2. lovell commented on Feb 8, 2026

    @lovell
    Owner

    Please see #3017 (comment)

  3. ctlcltd commented on Feb 8, 2026

    @ctlcltd
    Author

    I already searched the closed issues and I saw that.
    I think is another issue, related to the default option correctness. The default sharp.Options option density is ignored using withExif, the resulting jpeg image will have 96dpi in metadata.
    The resulting image is ok and resized, but it has 96dpi in metadata, maybe XReso and YReso come from the original metadata, maybe from XMP metadata. When using withMetadata({ density: 72 }) it correctly applies 72dpi in the metadata, but strip all will be turned off.
    With the default options, without withExif is 96 to 72, using withExif is 96 to 72. If you open the resulting jpeg file with a software, it opens it to 96dpi resolution.
    I don't know if this is an important issue.

  4. ctlcltd commented on Feb 11, 2026

    @ctlcltd
    Author

    @lovell an update. I'm trying sharp, vips cli, wasm-vips to generate thumbnails, the keep-exif behavior is identical.

    My origin file has xres,yres = 96dpi (~3.78 px/mm). I prefer images without this data 96dpi. Softwares, including viewers, are applying this data.

    So xres/yres should be set to 72 (~2.83 px/mm) before saving. This can be achieved with sharp using withMetadata({ density: 72 }), but there is no strip at all.

    In a comparison, this is vips cli:

    # jpeg 200x200 (72dpi) strip all, without exif
    vipsthumbnail image.jpg -s 200 --vips-info -o "thumbv-test1.jpg[Q=82,trellis-quant=true,keep=none]"
    
    # jpeg 200x200 (96dpi) strip, with exif only
    vipsthumbnail image.jpg -s 200 --vips-info -o "thumbv-test2.jpg[Q=82,trellis-quant=true,keep=exif]"
    
    # jpeg 200x200 (72dpi) strip, with exif only
    vips thumbnail image.jpg out1.v 200 --vips-info
    vips copy out1.v out2.v --xres 2.834645669291339 --yres 2.834645669291339 --vips-info
    vips jpegsave out2.v thumbv-test3.jpg -Q 82 --trellis-quant --keep exif --vips-info

    This is sharp, with a custom exif:

    // jpeg 200x200 (96dpi), strip all, with custom exif
    sharp('image.jpg')
      .resize({width: 200})
      .jpeg({quality: 82, trellisQuantisation: true})
      .withExif({IFD2: {UserComment: 'Comment'}})
      .toFile('thumbs-test1.jpg');
    
    // jpeg 200x200 (72dpi), no strip all, with custom exif
    sharp('image.jpg')
      .resize({width: 200})
      .jpeg({quality: 82, trellisQuantisation: true})
      .withMetadata({density: 72})
      .withExif({IFD2: {UserComment: 'Comment'}})
      .toFile('thumbs-test2.jpg');

    I'm concluding, I don't know what is the best method with sharp to: strip all metadata, custom exif, Xres/Yres (density) to 72dpi.

  5. lovell commented on Mar 4, 2026

    @lovell
    Owner

    The density constructor property only applies to vector images and I've clarified this via commit ff4626d.

    The use of withMetadata ensures the presence of EXIF metadata, which is necessary for a JPEG image to contain density information. Without this, JPEG images do not contain any density information. The function also ensures/adds an ICC profile as documented

    If you only want to set EXIF metadata then please see withExif.

    withExif({
      IFD0: {
        XResolution: '300dpi',
        YResolution: '300dpi',
      }
    })
  6. lovell commented on Mar 7, 2026

    @lovell
    Owner

    I think this relates to libvips always replacing XResolution and YResolution values with its internal xres and yres (which use pixels/mm):

    https://github.com/libvips/libvips/blob/460579a2a93bbd4c482fb22f01af9624b2ca72d5/libvips/foreign/exif.c#L1459-L1468

    When you call withMetadata({ density }) these values are set, but withExif does not. Perhaps a new withDensity(density) is required to offer more fine-grained control over the output density?

  7. added this to the v0.35.0 milestone on Mar 7, 2026
  8. changed the title [-]Image density 96dpi from metadata and default density is 72dpi, using withExif[/-] [+]Add withDensity for fine-grained control over EXIF resultion data[/+] on Mar 7, 2026
  9. lovell commented on Jun 10, 2026

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions