Convert 8bit grayscale jpeg image to 24bit jpeg image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
abhilash.ar
Posts: 3
Joined: 2014-01-13T23:19:29-07:00
Authentication code: 6789

Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by abhilash.ar »

Using CommandLine approach of ImageMagick I wanted to convert 8bit grayscale jpeg image to 24bit jpeg image maintaining the same image density(dpi).

I used the below command, but there is no change in the output image properties and bitdepth is unaltered
“ImageMagick-6.8.8-Q16\convert.exe” “Input_Image_Path” -density 300 -type TrueColor “Output_Image_Path”
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by snibgo »

What is the difference between "8bit grayscale jpeg" and "24bit jpeg"? As far as I know, jpegs can always store three channels, almost always 8 bits per channel.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by fmw42 »

snibgo wrote:What is the difference between "8bit grayscale jpeg" and "24bit jpeg"? As far as I know, jpegs can always store three channels, almost always 8 bits per channel.
I suspect the OP wants to convert the colorspace to sRGB, but have it grayscale. I don't think IM will allow that, unless at least one pixel is changed from gray. IM knows when the channels are all the same and forces the colorspace or type to gray.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by magick »

Add -type TrueColor to your command line.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by fmw42 »

magick wrote:Add -type TrueColor to your command line.

That does not seem to work for me. Afterwards, IM still reports colorspace gray and type grayscale


convert zelda3.png -colorspace gray zelda3g.png

Code: Select all

Image: zelda3g.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 128x128+0+0
  Resolution: 72x72
  Print size: 1.77778x1.77778
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 16-bit
  Channel depth:
    gray: 16-bit


convert zelda3g.png -type truecolor zelda3g_rgb.png

Code: Select all

Image: zelda3g_rgb.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 128x128+0+0
  Resolution: 72x72
  Print size: 1.77778x1.77778
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 16-bit
  Channel depth:
    gray: 16-bit
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by magick »

Does it work for JPEG? If not, we'll investigate. If not for PNG, alert our PNG maintainer that its a bug!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by snibgo »

My first reply was wrong. Jpegs can have just one channel. As magick suggests, "-type truecolor" ensures it has 3 channels.

Code: Select all

F:\web\im>%IM%convert logo: l.jpg

F:\web\im>dumpjpeg /il.jpg
      Input: /i l.jpg
l.jpg: JPEG image is 640w * 480h, 3 color components, 8 bits per sample
JPEG process: Baseline
width 640, height 480

F:\web\im>%IM%convert logo: -monochrome l.jpg

F:\web\im>dumpjpeg /il.jpg
      Input: /i l.jpg
l.jpg: JPEG image is 640w * 480h, 1 color components, 8 bits per sample
JPEG process: Baseline
width 640, height 480

F:\web\im>%IM%convert logo: -monochrome -type truecolor l.jpg

F:\web\im>dumpjpeg /il.jpg
      Input: /i l.jpg
l.jpg: JPEG image is 640w * 480h, 3 color components, 8 bits per sample
JPEG process: Baseline
width 640, height 480
@abhilash.ar: please clarify what you are trying to do. Ideally, please post your input to dropbox.com or similar and paste the URL here.
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by glennrp »

magick wrote:Does it work for JPEG? If not, we'll investigate. If not for PNG, alert our PNG maintainer that its a bug!
The PNG that is written is color-type 2, depth 16. When "identify" evaluates the image (not the input file!), it discovers that the pixels
are all gray and reports "grayscale". If you have to know about the stored PNG colortype, etc., don't use "identify" to find out. Use a third-party
application such as "pngcheck" instead. Probably the same goes for JPG files, too, but you'd need a different third-party application.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by snibgo »

As I did above with dumpjpeg.
snibgo's IM pages: im.snibgo.com
abhilash.ar
Posts: 3
Joined: 2014-01-13T23:19:29-07:00
Authentication code: 6789

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by abhilash.ar »

Thanks all for your replies :)

I had included '-type truecolor' in my command line. But it is not affecting jpeg images :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by fmw42 »

abhilash.ar wrote:Thanks all for your replies :)

I had included '-type truecolor' in my command line. But it is not affecting jpeg images :(

How do you know? Are you using IM identify? If so, that was the point of the comments above. You may need to verify using some other tool besides IM identify, such as Exiftool or one of the other tools mentioned. Apparently IM sees three identical channels as reports it as grayscale. But other tools will recognize it as 3 channels (RGB).
abhilash.ar
Posts: 3
Joined: 2014-01-13T23:19:29-07:00
Authentication code: 6789

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by abhilash.ar »

I used the below command to get the desired output
“ImageMagick-6.8.8-Q16\convert.exe” -density 300 -type bilevel -type TrueColor “Input_Image.jpeg” “Output_Image.jpeg”

Thanks for all your suggestions :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert 8bit grayscale jpeg image to 24bit jpeg image

Post by fmw42 »

abhilash.ar wrote:I used the below command to get the desired output
“ImageMagick-6.8.8-Q16\convert.exe” -density 300 -type bilevel -type TrueColor “Input_Image.jpeg” “Output_Image.jpeg”

Thanks for all your suggestions :)
Proper IM syntax in this case would be to read the input before any other command.

see
http://www.imagemagick.org/Usage/basics/#why
Post Reply