Skip to content

Image colour is changed without applying any operations #4468

Closed

Description

@Juneezee

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?

  System:
    OS: Linux 6.17 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 PRO 4750U with Radeon Graphics
    Memory: 15.66 GB / 22.84 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 24.10.0 - /home/jun/.nvm/versions/node/v24.10.0/bin/node
    npm: 11.6.1 - /home/jun/.nvm/versions/node/v24.10.0/bin/npm

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?

  1. Download the provided original.jpg
  2. Copy the provided minimal reproducible code below into main.mjs file. This file should be in the same directory as original.jpg.
  3. Run node main.mjs.

What is the expected behaviour?

The colour of the image should not change.

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

import { readFileSync, writeFileSync } from "node:fs";
import sharp from "sharp";

const buf = readFileSync("original.jpg");
const s = sharp(buf);
const newBuf = await s.toBuffer();
writeFileSync("processed.jpg", newBuf);

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

Original image:

original.jpg

Processed image:

processed.jpg

Activity

  1. lovell commented on Oct 31, 2025

    @lovell
    Owner

    The sample input image provided is in the CMYK colourspace with an ICC profile. If you wish to retain this same profile and colourspace in the output, rather than the default behaviour of converting to profile-less sRGB, please see https://sharp.pixelplumbing.com/api-output/#keepiccprofile

  2. Juneezee commented on Oct 31, 2025

    @Juneezee
    Author

    The sample input image provided is in the CMYK colourspace with an ICC profile. If you wish to retain this same profile and colourspace in the output, rather than the default behaviour of converting to profile-less sRGB, please see https://sharp.pixelplumbing.com/api-output/#keepiccprofile

    @lovell I just tried. Even with keepIccprofile(), the colour is still different.

  3. lovell commented on Nov 2, 2025

    @lovell
    Owner

    Ah, sorry, you'll need to explicitly add toColourspace('cmyk') to maintain CMYK colourspace in the output image.

    https://sharp.pixelplumbing.com/api-colour/#tocolourspace

    (I might need to update the keepIccProfile docs as there have been some recent upstream changes in libvips around how colourspaces and profiles are handled when saving.)

  4. Juneezee commented on Nov 2, 2025

    @Juneezee
    Author

    Ah, sorry, you'll need to explicitly add toColourspace('cmyk') to maintain CMYK colourspace in the output image.

    @lovell With toColourspace('cmyk').keepIccProfile(), the colour is still different.

    I also noticed that WebStorm reports the original image as having 32-bit colour, while the processed image only has 24-bit colour.

    Image

    Below are the outputs of magick inspect --verbose from ImageMagick for both the original and processed images.

    Original image:

    Image:
      Filename: original.jpg
      Permissions: rw-r--r--
      Format: JPEG (Joint Photographic Experts Group JFIF format)
      Mime type: image/jpeg
      Class: DirectClass
      Geometry: 1654x1169+0+0
      Resolution: 100x100
      Print size: 16.54x11.69
      Units: PixelsPerInch
      Colorspace: CMYK
      Type: ColorSeparation
      Base type: Undefined
      Endianness: Undefined
      Depth: 8-bit
      Channels: 4.0
      Channel depth:
        Cyan: 8-bit
        Magenta: 8-bit
        Yellow: 8-bit
        Black: 8-bit
      Channel statistics:
        Pixels: 1933526
        Cyan:
          min: 210  (0.823529)
          max: 255 (1)
          mean: 252.624 (0.990681)
          median: 254 (0.996078)
          standard deviation: 3.12871 (0.0122694)
          kurtosis: 4.25942
          skewness: -1.83013
          entropy: 0.542567
        Magenta:
          min: 36  (0.141176)
          max: 200 (0.784314)
          mean: 141.508 (0.554935)
          median: 136 (0.533333)
          standard deviation: 23.5531 (0.092365)
          kurtosis: -0.702555
          skewness: 0.453851
          entropy: 0.896817
        Yellow:
          min: 0  (0)
          max: 51 (0.2)
          mean: 5.75338 (0.0225623)
          median: 5 (0.0196078)
          standard deviation: 4.62731 (0.0181463)
          kurtosis: 0.0165718
          skewness: 0.648222
          entropy: 0.739265
        Black:
          min: 0  (0)
          max: 167 (0.654902)
          mean: 25.3131 (0.0992672)
          median: 20 (0.0784314)
          standard deviation: 21.0454 (0.0825309)
          kurtosis: 0.892005
          skewness: 1.08159
          entropy: 0.836792
      Image statistics:
        Overall:
          min: 0  (0)
          max: 255 (1)
          mean: 106.3 (0.416861)
          median: 103.75 (0.406863)
          standard deviation: 13.0886 (0.0513279)
          kurtosis: 1.11636
          skewness: 0.0883836
          entropy: 0.75386
      Total ink density: 253.725%
      Rendering intent: Perceptual
      Gamma: 0.454545
      Chromaticity:
        red primary: (0.64,0.33,0.03)
        green primary: (0.3,0.6,0.1)
        blue primary: (0.15,0.06,0.79)
        white point: (0.3127,0.329,0.3583)
      Matte color: grey74
      Background color: white
      Border color: srgb(223,223,223)
      Transparent color: black
      Interlace: None
      Intensity: Undefined
      Compose: Over
      Page geometry: 1654x1169+0+0
      Dispose: Undefined
      Iterations: 0
      Compression: JPEG
      Quality: 96
      Orientation: TopLeft
      Profiles:
        Profile-8bim: 3538 bytes
        Profile-exif: 1507 bytes
        Profile-iptc: 25 bytes
          City[1,90]: 0x00000000: 254700                                        -%G
          unknown[2,0]:
          Image Name[2,5]: Print
        Profile-xmp: 4708 bytes
      Properties:
        date:create: 2025-10-31T09:36:22+00:00
        date:modify: 2025-10-31T09:36:06+00:00
        date:timestamp: 2025-11-02T17:27:09+00:00
        exif:BitsPerSample: 8, 8, 8, 8
        exif:ColorSpace: 65535
        exif:DateTime: 2023:04:12 11:10:36
        exif:ExifOffset: 236
        exif:ExifVersion: 0231
        exif:ImageLength: 3508
        exif:ImageWidth: 4961
        exif:PhotometricInterpretation: 5
        exif:PixelXDimension: 1654
        exif:PixelYDimension: 1169
        exif:SamplesPerPixel: 4
        exif:Software: Adobe Photoshop 24.3 (Windows)
        exif:thumbnail:Compression: 6
        exif:thumbnail:JPEGInterchangeFormat: 386
        exif:thumbnail:JPEGInterchangeFormatLength: 1115
        exif:thumbnail:ResolutionUnit: 2
        exif:thumbnail:XResolution: 72/1
        exif:thumbnail:YResolution: 72/1
        jpeg:colorspace: 4
        jpeg:sampling-factor: 1x1,1x1,1x1,1x1
        signature: 2890372682892ea595a79948cfeb89a9ba3266475f3c9ea51c287a6164abcb10
      Artifacts:
        verbose: true
      Tainted: False
      Filesize: 1.97452MiB
      Number pixels: 1.93353M
      Pixel cache type: Memory
      Pixels per second: 23.0365MP
      User time: 0.080u
      Elapsed time: 0:01.083
      Version: ImageMagick 7.1.2-8 Q16-HDRI x86_64 2300ae543:20251025 https://imagemagick.org
    

    Processed image:

    Note

    I have no idea why the Colorspace field is still sRGB even when toColourspace('cmyk') is set.

    Image:
      Filename: processed.jpg
      Permissions: rw-r--r--
      Format: JPEG (Joint Photographic Experts Group JFIF format)
      Mime type: image/jpeg
      Class: DirectClass
      Geometry: 1654x1169+0+0
      Units: Undefined
      Colorspace: sRGB
      Type: TrueColor
      Base type: Undefined
      Endianness: Undefined
      Depth: 8-bit
      Channels: 3.0
      Channel depth:
        Red: 8-bit
        Green: 8-bit
        Blue: 8-bit
      Channel statistics:
        Pixels: 1933526
        Red:
          min: 0  (0)
          max: 32 (0.12549)
          mean: 2.51997 (0.00988225)
          median: 2 (0.00784314)
          standard deviation: 2.93445 (0.0115076)
          kurtosis: 1.71212
          skewness: 1.31988
          entropy: 0.606245
        Green:
          min: 22  (0.0862745)
          max: 126 (0.494118)
          mean: 77.5976 (0.304304)
          median: 82 (0.321569)
          standard deviation: 16.2868 (0.06387)
          kurtosis: -0.710247
          skewness: -0.641201
          entropy: 0.875237
        Blue:
          min: 91  (0.356863)
          max: 188 (0.737255)
          mean: 144.056 (0.564926)
          median: 148 (0.580392)
          standard deviation: 14.2468 (0.05587)
          kurtosis: -0.437819
          skewness: -0.745231
          entropy: 0.861213
      Image statistics:
        Overall:
          min: 0  (0)
          max: 188 (0.737255)
          mean: 74.7246 (0.293038)
          median: 77.3333 (0.303268)
          standard deviation: 11.156 (0.0437492)
          kurtosis: 0.188019
          skewness: -0.0221852
          entropy: 0.780899
      Rendering intent: Perceptual
      Gamma: 0.454545
      Chromaticity:
        red primary: (0.64,0.33,0.03)
        green primary: (0.3,0.6,0.1)
        blue primary: (0.15,0.06,0.79)
        white point: (0.3127,0.329,0.3583)
      Matte color: grey74
      Background color: white
      Border color: srgb(223,223,223)
      Transparent color: black
      Interlace: None
      Intensity: Undefined
      Compose: Over
      Page geometry: 1654x1169+0+0
      Dispose: Undefined
      Iterations: 0
      Compression: JPEG
      Quality: 80
      Orientation: Undefined
      Profiles:
        Profile-icc: 480 bytes
      Properties:
        date:create: 2025-11-02T17:27:01+00:00
        date:modify: 2025-11-02T17:27:01+00:00
        date:timestamp: 2025-11-02T17:28:03+00:00
        icc:copyright: CC0
        icc:description: sRGB
        jpeg:colorspace: 2
        jpeg:sampling-factor: 2x2,1x1,1x1
        signature: db19ccdd85fc06b5d6f73bce8563da39e8510a96c6478c926648f9186a408f02
      Artifacts:
        verbose: true
      Tainted: False
      Filesize: 197048B
      Number pixels: 1.93353M
      Pixel cache type: Memory
      Pixels per second: 78.8921MP
      User time: 0.020u
      Elapsed time: 0:01.024
      Version: ImageMagick 7.1.2-8 Q16-HDRI x86_64 2300ae543:20251025 https://imagemagick.org
    
  5. lovell commented on Nov 4, 2025

    @lovell
    Owner

    Thanks, I've been able to reproduce locally and can see that the expected output is CMYK but the actual output is RGB. Commit e1628d8 simplifies some of the logic around this and adds the scenario from this issue as a test cases to help prevent regression.

  6. added this to the v0.34.5 milestone on Nov 4, 2025
  7. lovell commented on Nov 5, 2025

    @lovell
    Owner

    There's a release candidate available if you'd like to test.

  8. Juneezee commented on Nov 6, 2025

    @Juneezee
    Author

    There's a release candidate available if you'd like to test.

    @lovell I install the release candidate and followed the updated comments of commit e1628d8. In my case toColourspace("cmyk") isn't enough, pipelineColourspace("cmyk") must also be used.

    import { readFileSync, writeFileSync } from "node:fs";
    import sharp from "sharp";
    
    const buf = readFileSync("original.jpg");
    const s = sharp(buf)
    	.pipelineColourspace("cmyk")
    	.toColourspace("cmyk")
    	.keepIccProfile();
    const newBuf = await s.toBuffer();
    writeFileSync("processed.jpg", newBuf);
    Image
    Image:
      Filename: processed.jpg
      Permissions: rw-r--r--
      Format: JPEG (Joint Photographic Experts Group JFIF format)
      Mime type: image/jpeg
      Class: DirectClass
      Geometry: 1654x1169+0+0
      Units: Undefined
      Colorspace: CMYK
      Type: ColorSeparation
      Base type: Undefined
      Endianness: Undefined
      Depth: 8-bit
      Channels: 4.0
      Channel depth:
        Cyan: 8-bit
        Magenta: 8-bit
        Yellow: 8-bit
        Black: 8-bit
      Channel statistics:
        Pixels: 1933526
        Cyan:
          min: 221  (0.866667)
          max: 255 (1)
          mean: 252.54 (0.990353)
          median: 253 (0.992157)
          standard deviation: 2.38092 (0.00933693)
          kurtosis: 4.27696
          skewness: -1.73582
          entropy: 0.581343
        Magenta:
          min: 40  (0.156863)
          max: 202 (0.792157)
          mean: 141.51 (0.554941)
          median: 136 (0.533333)
          standard deviation: 23.5807 (0.0924733)
          kurtosis: -0.714126
          skewness: 0.446926
          entropy: 0.898469
        Yellow:
          min: 0  (0)
          max: 53 (0.207843)
          mean: 5.79022 (0.0227068)
          median: 5 (0.0196078)
          standard deviation: 4.15623 (0.0162989)
          kurtosis: 1.11321
          skewness: 0.969187
          entropy: 0.732562
        Black:
          min: 0  (0)
          max: 166 (0.65098)
          mean: 25.4264 (0.0997113)
          median: 20 (0.0784314)
          standard deviation: 21.1147 (0.0828029)
          kurtosis: 1.03726
          skewness: 1.12899
          entropy: 0.833843
      Image statistics:
        Overall:
          min: 0  (0)
          max: 255 (1)
          mean: 106.317 (0.416928)
          median: 103.5 (0.405882)
          standard deviation: 12.8081 (0.050228)
          kurtosis: 1.42833
          skewness: 0.202321
          entropy: 0.761554
      Total ink density: 257.647%
      Rendering intent: Perceptual
      Gamma: 0.454545
      Chromaticity:
        red primary: (0.64,0.33,0.03)
        green primary: (0.3,0.6,0.1)
        blue primary: (0.15,0.06,0.79)
        white point: (0.3127,0.329,0.3583)
      Matte color: grey74
      Background color: white
      Border color: srgb(223,223,223)
      Transparent color: black
      Interlace: None
      Intensity: Undefined
      Compose: Over
      Page geometry: 1654x1169+0+0
      Dispose: Undefined
      Iterations: 0
      Compression: JPEG
      Quality: 80
      Orientation: Undefined
      Properties:
        date:create: 2025-11-06T01:27:15+00:00
        date:modify: 2025-11-06T01:27:15+00:00
        date:timestamp: 2025-11-06T01:28:09+00:00
        jpeg:colorspace: 4
        jpeg:sampling-factor: 1x1,1x1,1x1,1x1
        signature: b2d2d7bdd4cabad2da8f478c0712330f1bb014a2671718b6291cee9640681916
      Artifacts:
        verbose: true
      Tainted: False
      Filesize: 998098B
      Number pixels: 1.93353M
      Pixel cache type: Memory
      Pixels per second: 35.831MP
      User time: 0.050u
      Elapsed time: 0:01.053
      Version: ImageMagick 7.1.2-8 Q16-HDRI x86_64 2300ae543:20251025 https://imagemagick.org
    

    Thanks for fixing the issue 😊

  9. lovell commented on Nov 6, 2025

    @lovell
    Owner

    v0.34.5 now available

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