Converting Grayscale file to JPEG

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, helmut, xnview

Post Reply
cday
XnThusiast
Posts: 3985
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Converting Grayscale file to JPEG

Post by cday »

When the attached 8-bit grayscale file is used as input for the following code:

Code: Select all

nconvert -out jpeg -q 80 Test.tif
...the error message below is displayed:

Code: Select all

Error: This picture cannot be written using this format (Test.jpg)
The file can be saved as a JPEG using XnView MP...

Does the above code need to be modified, or is there a bug in NConvert?

Note: Similar result with grayscale PNG, and NConvert versions 7.20 and 6.35.

Test.zip
(242.98 KiB) Downloaded 69 times
User avatar
xnview
Author of XnView
Posts: 43601
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Grayscale file to JPEG [Code error or Bug?]

Post by xnview »

this picture has a greyed colormap, so you need to force with -grey
Pierre.
cday
XnThusiast
Posts: 3985
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Grayscale file to JPEG [Code error or Bug?]

Post by cday »

xnview wrote: Wed Jun 05, 2019 3:29 pm this picture has a greyed colormap, so you need to force with -grey
Thanks, the Help files gives the option as:

Code: Select all

-grey num         : Convert in Grey Scale (256, 128, 64, 32, 16, 8 or 4)
Inserting -grey 64 into the above code:

Code: Select all

nconvert -grey 64 -out jpeg -q 80 Test.tif
...produces the expected JPEG, but the created file seems to have 256 'colours' whatever num value is used... [Unexplained.]
User avatar
XnTriq
Moderator & Librarian
Posts: 6339
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Grayscale file to JPEG [Code error or Bug?]

Post by XnTriq »

cday wrote: Wed Jun 05, 2019 5:06 pm...produces the expected JPEG, but the created file seems to have 256 'colours' whatever num value is used... [Unexplained.]
I believe that's because the JPEG/JFIF file format doen't support these bit depths:
  • 2-bit grayscale → 4 levels of gray
  • 3-bit grayscale → 8 levels of gray
  • 4-bit grayscale → 16 levels of gray
  • 5-bit grayscale → 32 levels of gray
  • 6-bit grayscale → 64 levels of gray
  • 7-bit grayscale → 128 levels of gray
Image
http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.5.3 wrote:
PNG grayscale images support the widest range of pixel depths of any image type. Depths of 1, 2, 4, 8, and 16 bits are supported, covering everything from simple black-and-white scans to full-depth medical and raw astronomical images.

[….]

The most common form of JPEG (the one that uses “lossy” compression, in which some information in the image is thrown away) likewise supports grayscale images in depths of 8 and 12 bits. In addition, there are two variants that use truly lossless compression and support any depth from 2 to 16 bits: the traditional version, known simply as “lossless JPEG”, and an upcoming second-generation flavor called “JPEG-LS”. But the first is extremely rare, and is supported by almost no one, despite having been standardized years ago, and the second is also currently unsupported (although that is to be expected for a new format). Lossy JPEG is very well supported, thanks largely to the Independent JPEG Group's free libjpeg (which, like libtiff, has become the de facto standard for JPEG encoding and decoding) – but, of course, it's lossy. Note that libjpeg can be compiled to support either 8-bit or 12-bit JPEG, but not both at the same time. Thus, from a practical standpoint, only 8-bit, lossy grayscale is supported.
cday
XnThusiast
Posts: 3985
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Converting Grayscale file to JPEG

Post by cday »

XnTriq wrote: Fri Jun 07, 2019 3:30 pm
cday wrote: Wed Jun 05, 2019 5:06 pm...produces the expected JPEG, but the created file seems to have 256 'colours' whatever num value is used... [Unexplained.]
I believe that's because the JPEG/JFIF file format doen't support these bit depths:
  • 2-bit grayscale ~ 4 levels of gray
  • 3-bit grayscale ~ 8 levels of gray
  • 4-bit grayscale ~ 16 levels of gray
  • 5-bit grayscale ~ 32 levels of gray
  • 6-bit grayscale ~ 64 levels of gray
  • 7-bit grayscale ~ 128 levels of gray

Good point, JPEG does indeed only support 8-bit depth grayscale (256 levels)...

But using the -grey forcing switch without a num value didn't work, but perhaps surprisingly any of the small number of num values I tried did produce the required result. Of course, those values could produce a valid PNG, for example, as you indicate.
User avatar
XnTriq
Moderator & Librarian
Posts: 6339
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Converting Grayscale file to JPEG

Post by XnTriq »

cday wrote: Fri Jun 07, 2019 7:24 pmGood point, JPEG does indeed only support 8-bit depth grayscale (256 levels)...

But using the -grey forcing switch without a num value didn't work, but perhaps surprisingly any of the small number of num values I tried did produce the required result. Of course, those values could produce a valid PNG, for example, as you indicate.
So, should the -grey parameter (w/o num switch) produce 8-bit grayscale images (regardless of the output format) by default?
cday
XnThusiast
Posts: 3985
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Converting Grayscale file to JPEG

Post by cday »

XnTriq wrote: Fri Jun 07, 2019 8:30 pm
cday wrote: Fri Jun 07, 2019 7:24 pmGood point, JPEG does indeed only support 8-bit depth grayscale (256 levels)...

But using the -grey forcing switch without a num value didn't work, but perhaps surprisingly any of the small number of num values I tried did produce the required result. Of course, those values could produce a valid PNG, for example, as you indicate.
So, should the -grey parameter (w/o num switch) produce 8-bit grayscale images (regardless of the output format) by default?
Or possibly values other than 256 produce an error message?

Pierre?
Post Reply