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

png output with Colorspace Gray #2855

Closed
amitm02 opened this issue Aug 23, 2021 · 5 comments
Closed

png output with Colorspace Gray #2855

amitm02 opened this issue Aug 23, 2021 · 5 comments

Comments

@amitm02
Copy link

amitm02 commented Aug 23, 2021

When packing an image to PNG, sRGB is produced:

Colorspace: sRGB
Depth: 8/1-bit
  Channel depth:
    Red: 1-bit
    Green: 1-bit
    Blue: 1-bit
Colors: 2

It would be great to have the ability to generate Gray colorspace PNG images:

Colorspace: Gray
Depth: 8/1-bit
  Channel depth:
    Gray: 1-bit
Colors: 2
@lovell
Copy link
Owner

lovell commented Aug 23, 2021

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

await sharp(input)
 .toColourspace('b-w')
 .toFile('8-bit-greyscale.png')

@amitm02
Copy link
Author

amitm02 commented Aug 23, 2021

Thanks for the reply.
Howver i get Colormap entries: 256:
out.png PNG 1712x2332 1712x2332+0+0 8-bit Gray 256c 38972B 0.000u 0:00.000.

and if running:

sharp('./in.png')
  .toColorspace('b-w')
  .png({colors: 2})
  .toFile('out.png')

I get sRGB back:
out.png PNG 1712x2332 1712x2332+0+0 8-bit sRGB 2c 33018B 0.000u 0:00.000


The main issue I try to tackle is that I fail to find a way to re-encode the following input PNG configuration without it being inflated significantly in size.

Image:
  Filename: in.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 1712x2332+0+0
  Resolution: 28.34x28.34
  Print size: 60.4093x82.2865
  Units: PixelsPerCentimeter
  Colorspace: Gray
  Type: Bilevel
  Base type: Undefined
  Endianness: Undefined
  Depth: 8/1-bit
  Channel depth:
    Gray: 1-bit
  Channel statistics:
    Pixels: 3992384
    Gray:
      min: 0  (0)
      max: 255 (1)
      mean: 248.265 (0.973588)
      median: 255 (1)
      standard deviation: 40.8907 (0.160356)
      kurtosis: 32.8894
      skewness: -5.90672
      entropy: 0.176064
  Colors: 2
  Histogram:
    105445: (0,0,0) #000000 gray(0)
    3886939: (255,255,255) #FFFFFF gray(255)
  Colormap entries: 2
  Colormap:
    0: (0,0,0,1) #000000FF graya(0,1)
    1: (255,255,255,1) #FFFFFFFF graya(255,1)
  Rendering intent: Undefined
  Gamma: 0.454545
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1712x2332+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2021-08-23T18:06:01+00:00
    date:modify: 2021-08-23T18:06:01+00:00
    png:IHDR.bit-depth-orig: 1
    png:IHDR.bit_depth: 1
    png:IHDR.color-type-orig: 0
    png:IHDR.color_type: 0 (Grayscale)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1712, 2332
    png:pHYs: x_res=2834, y_res=2834, units=1
    signature: f39e65310d6a342089b53b6419fe4e50e680c6d7025aa003ab24c08d2f0fec83
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 22185B
  Number pixels: 3.99238M
  Pixels per second: 97.5965MP
  User time: 0.350u
  Elapsed time: 0:01.040
  Version: ImageMagick 7.1.0-0 Q16 x86_64 2021-06-13 https://imagemagick.org

@lovell
Copy link
Owner

lovell commented Aug 25, 2021

This example looks like a bug, ostensibly in libvips as png->bitdepth is correctly set from colours then erroneously always overwritten as 8 on the next line.

https://github.com/libvips/libvips/blob/1ce301812d1c44ca6421ed93d6ba1ec4b1d14433/libvips/foreign/pngsave.c#L131-L138

However colours is deprecated, so we should probably pass bitdepth, and have the log2(colours) calculation, in sharp instead.

lovell added a commit that referenced this issue Aug 26, 2021
Removes use of deprecated libvips API
@lovell
Copy link
Owner

lovell commented Aug 26, 2021

Commit 3402656 should fix this, and will be in v0.29.1

@lovell
Copy link
Owner

lovell commented Sep 7, 2021

v0.29.1 now available with this fix, thanks for reporting.

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

No branches or pull requests

2 participants