Losslessly remove chrominance channels iff JPEG is grayscale

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
cow10
Posts: 1
Joined: 2017-12-29T01:04:36-07:00
Authentication code: 1152

Losslessly remove chrominance channels iff JPEG is grayscale

Post by cow10 »

Is there a way of losslessly reducing a JPEG image's bit-depth to 8 if it's already in grayscale?

Code: Select all

magick convert image image
losslessly chooses the appropriate bit-depth for a grayscale PNG but results in recompression for JPEG. I'm currently using

Code: Select all

jpegtran -grayscale -outfile tmpImage
and checking to see if

Code: Select all

magick compare -metric rmse image tmpImage NUL
is "0 (0)" before replacing the image, though I'd prefer an IM-only solution if possible.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Losslessly remove chrominance channels iff JPEG is grayscale

Post by snibgo »

IM is a general-purpose raster image processor. So it reads a file and decompresses it, processes the image, then compresses and writes a file. If the output format is JPEG, this will almost certainly be lossy.

Some operations on JPEG can be lossless, but only by doing those operations with intimate knowledge of how JPEG works. IM doesn't do that. Specialist programs such as jpegtran do.
snibgo's IM pages: im.snibgo.com
Post Reply