vseventer / sharp-cli

CLI for sharp.
MIT License
189 stars 20 forks source link

Greyscale conversion is not working #88

Closed alanwilter closed 10 months ago

alanwilter commented 11 months ago

I'm doing:

sharp greyscale --format png -i 'folder/**/*.webp' -o {dir}

The PNG created are all RGB. What am I missing? Or is it a bug?

vseventer commented 11 months ago

Can you provide the image?

I just tried with a WEBP file myself, which perfectly results in a grayscale PNG.

alanwilter commented 11 months ago

Using sharp on a Mac M1 installed via homebrew, version 3.0.0

$ identify OCT-44492_11.webp
OCT-44492_11.webp WEBP 512x496 512x496+0+0 8-bit sRGB 49810B 0.000u 0:00.001

$ sharp greyscale -i OCT-44492_11.webp -o grey_or_not.png

$ identify grey_or_not.png
grey_or_not.png PNG 512x496 512x496+0+0 8-bit sRGB 105996B 0.000u 0:00.001
# No, not grey :-(

# The working way I'm using (so slow when dealing with hundreds of images... but it works)
$ convert OCT-44492_11.webp -colorspace Gray grey_yes.png

$identify grey_yes.png
grey_yes.png PNG 512x496 512x496+0+0 8-bit Grayscale Gray 256c 64386B 0.000u 0:00.000

OCT-44492_11.webp.zip

vseventer commented 11 months ago

Using sharp on a Mac M1 installed via homebrew, version 3.0.0

$ identify OCT-44492_11.webp
OCT-44492_11.webp WEBP 512x496 512x496+0+0 8-bit sRGB 49810B 0.000u 0:00.001

$ sharp greyscale -i OCT-44492_11.webp -o grey_or_not.png

$ identify grey_or_not.png
grey_or_not.png PNG 512x496 512x496+0+0 8-bit sRGB 105996B 0.000u 0:00.001
# No, not grey :-(

# The working way I'm using (so slow when dealing with hundreds of images... but it works)
$ convert OCT-44492_11.webp -colorspace Gray grey_yes.png

$identify grey_yes.png
grey_yes.png PNG 512x496 512x496+0+0 8-bit Grayscale Gray 256c 64386B 0.000u 0:00.000

OCT-44492_11.webp.zip

Ah, I see - I think in that case what you want to use is sharp toColourspace b-w -i OCT-44492_11.webp -o grey_or_not.png

alanwilter commented 11 months ago

Thanks for that, but where do I found a proper documentation on this matter? And what this greyscale option does then? It clearly does not do what's written in the "tin" IMO.

 sharp greyscale                           Convert to 8-bit greyscale; 256

It did work as you said:

sharp toColourspace b-w -i OCT-44492_11.webp -o grey.png

identify grey.png grey_yes.png
grey.png PNG 512x496 512x496+0+0 8-bit Grayscale Gray 256c 73715B 0.000u 0:00.000
grey_yes.png PNG 512x496 512x496+0+0 8-bit Grayscale Gray 256c 64386B 0.000u 0:00.000

But grey.png is not binary identical to grey_yes.png (it's a bit KB bigger).

Interestingly:

perceptualdiff -verbose -threshold 1 grey.png grey_yes.png
Field of view is 45.000000 degrees
Threshold pixels is 1 pixels
The Gamma is 2.200000
The Display's luminance is 100.000000 candela per meter squared
Converting RGB to XYZ
Constructing Laplacian Pyramids
Performing test
PASS: Images are perceptually indistinguishable
0 pixels are different

It's PNG lossless 256c gray images, I'm wondering why they are not binary identical.

vseventer commented 11 months ago

The CLI follows the description of the underlying image library, see here. It suggests pairing with gamma, which you can try.

For specific results and sizes, better to inquire with sharp directly.

alanwilter commented 10 months ago

Thanks a lot!