ImageMagick: convert keeps changing the colorspace to Gray. How to preserve sRGB colorspace? - imagemagick

I have a batch script that converts my PNG-24 (with Transparency) images to 50% and 25% size (for mobile development). Usually these images have colors in them but now I am trying to convert an image that has no colors and ImageMagick keeps changing the colorspace profile to "Gray", which messes up my image in the 3d engine I'm using (Unity).
I have tried forcing it to use type TrueColor, colorspace sRGB, and the sRGB.icc profile (the one included with OSX) but it doesn't seem to care. It still changes it to Gray.
> convert old.png -profile srgb.icc -colorspace sRGB -type TrueColor new.png
> identify *.png
old.png PNG 140x140 140x140+0+0 8-bit sRGB 3.68KB 0.000u 0:00.000
new.png PNG 140x140 140x140+0+0 8-bit sRGB 256c 2.33KB 0.000u 0:00.000
ImageMagick still identifies it as an 8-bit sRGB image but it puts "256c" after it which I'm assuming means it has reduced it down to 256 colors, which I don't want either. When I look at the image in OSX Preview.app, it says it is using the Gray color profile. The image also visually looks a lot different.
Here is the image I'm using: https://dl.dropbox.com/u/59304/old.png
There is a duplicate question here, ImageMagick Reduces Colorspace to Gray, but the answer does not work for me and I don't have enough reputation to comment on his answer, unfortunately. I imagine my case is different because I'm using PNG and not JPG.
Version: ImageMagick 6.8.0-7 2013-01-02 Q16 http://www.imagemagick.org
Features: OpenCL
edit- After reading the ImageMagick forums as specified in one of the answers, it looks like just prepending PNG32: or PNG24: to the output file solves the problem.

The proper way to keep a grayscale PNG as RGB is to use PNG24:result.png
Input:
convert lena.png -colorspace gray PNG24:lenag_rgb.png
identify -verbose lenag_rgb.png
Image: lenag_rgb.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 256x256+0+0
Units: Undefined
Colorspace: sRGB
Type: Grayscale
So as you see above, Colorspace is RGB while the type is Grayscale.
For other image formats such as JPG and TIFF, use -define colorspace:auto-grayscale=false along with -type truecolor.

You may pass -set colorspace:auto-grayscale off to convert to disable automatic conversion of RGB channels to a single grayscale channel.
This solution was not yet available at the time of your question, but was introduced in 2015 with version 6.9.2:
2015-07-25 6.9.2-0 Dirk Lemstra <dirk#lem.....org>
Added -set colorspace:auto-grayscale=false that will prevent automatic conversion to grayscale inside coders that support grayscale.

Related

How to convert PNG images (specifically grayscale ones) to Indexed color

I'm trying to convert some small PNG images from 32-bit color mode to indexed color mode.
For color images, I ran the command convert IMGS/FLAME.png INDEXED_IMGS/FLAME.png and it converted fine. For an image that had only grayscale colors, I ran that same command (with the filename changed obviously) but I got a warning:
convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG 'INDEXED_IMGS/SHADOW.png' # warning/png.c/MagickPNGWarningHandler/1748.
I ran file IMGS/*.pngand got
IMGS/FLAME.png: PNG image data, 16 x 16, 8-bit/color RGBA, non-interlaced
IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit/color RGBA, non-interlaced
which is expected; both images are in 8-bit RGBA mode (since that's the mode I created them in Photoshop). However, when I run file INDEXED_IMGS/*.png I get
INDEXED_IMGS/FLAME.png: PNG image data, 16 x 16, 4-bit colormap, non-interlaced
INDEXED_IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit grayscale, non-interlaced
The 4-bit colormap part checks out, but the grayscale part does not.
So my question is: how can I convert a grayscale image to indexed mode? What really gets me is that it starts out in RGBA mode like the color image, but for some reason it converts automatically to grayscale mode. Is there a way to prevent it from doing that?
I should add that I have a bash script that looks like this:
#!/bin/bash
for img in IMGS/*.png; do
file=$(basename $img)
convert $img INDEXED_IMGS/$file
done
so I don't wanna manually distinguish between grayscale and colored images. If there's a way to do so automatically with some command that's fine though.
Here is info about my ImageMagick tool:
Version: ImageMagick 7.0.8-42 Q16 x86_64 2019-04-24 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib
With ImageMagick, for 24-bit color, append the output with PNG8:output
convert input.png PNG8:output.png
PNG grayscale images do not support color profiles, so you get that warning. But the resulting image should be 8-bit palette.
If you have 32-bit color, then this needs more to be done. The color under the alpha channel must be one constant color and one not used elsewhere in the image. Find such a color after converting to 256 colors and set the color under the transparency to that color. For example if you have no opaque black in your image after converting to 256 colors, then set the alpha base color to black.
convert image.png +dither -colors 256 -background black -alpha background PNG8:output.png
You can get a list of unique colors from the image as follows:
convert image.png +dither -colors 256 -unique-colors txt:
Here is an example:
Make it 32-bit transparent:
convert rose.png -fuzz 20% -transparent red rose_trans32.png
identify -verbose rose_trans32.png
...
Colorspace: sRGB
Type: TrueColorAlpha
...
Convert to palette alpha:
convert rose_trans32.png -alpha off +dither -colors 256 -unique-colors txt:
List shows no black
convert rose_trans32.png +dither -colors 256 -background black -alpha background PNG8:rose_trans8.png
Or if you already know that the 32-bit version has not black, then just:
convert rose_trans32.png -background black -alpha background PNG8:rose_trans8.png
identify -verbose rose_trans8.png
...
Colorspace: sRGB
Type: PaletteAlpha
...
If you do this adding -colorspace gray, ImageMagick will still report as type grayscalealpha, since it recognizes it as a single channel image with transparency. But using EXIFTOOL, it will report 9 ColorType: 3, which is 3 = RGB Palette
NOTE: For ImageMagick 7, change convert to magick.

Ruby + RMagick + base64 image + RGB conversion from GrayScale doesn't work

I'm uploading base64 encoded image to a RoR application. When I receive the image, it has a rgb color scheme (correct), when I write the image on file to be uploaded with paperclip gem, the image color scheme change from rgb to grayscale.
Here is the code:
source = src.gsub(/^data:image\/(png|jpg|jpeg);base64,/,"")
blob = Base64.decode64(source)
img = Magick::Image.from_blob(blob).first
img.colorspace = Magick::SRGBColorspace
img.add_profile "#{Rails.root.to_s}/lib/color_profiles/RGB.icc"
img.write(url = "#{Rails.root.to_s}/tmp/#{self.id}_logo.png")
image = File.open(url)
the img is correctly a RGB image, if I check the resulted created file:
identify -format "%[colorspace]" #{url}
the color scheme is Gray.
Additional info:
The uploaded image is all black with white text, if I upload same image with red background, the final image is correctly an RGB image.
There seems to be a bug in ImageMagick 6.9.9.27 and 7.0.7.15 when reporting the conversion of a grayscale image to RGB PNG. Identify -verbose is reporting grayscale but the string format %[colorspace] is properly reporting sRGB as are the PNG tags. I have reported this bug. For example:
convert logo: -colorspace gray logo.jpg
convert logo.jpg PNG24:logo.png
convert logo.png -format "%[colorspace]" info:
sRGB
identify -verbose logo.png
...
Colorspace: Gray
...
png:IHDR.color-type-orig: 2
png:IHDR.color_type: 2 (Truecolor)
I do not understand. Is your image a color image or a grayscale only image?
IM 6.7.7.10 was during a time that ImageMagick was changing from non-linear gray to linear gray and back again. And also had RGB and sRGB swapped. So you may have a version where gray was linear (darker than non-linear gray) or where RGB and sRGB were swapped. You can convert back to non-linear using one of the following (I do not recall which to use at this time). The other will convert from linear to non-linear. If I assume your input image was grayscale and not color, then try one of these:
convert input -colorspace RGB -colorspace gray result
or
convert input -colorspace sRGB -colorspace gray result
If it is not grayscale, but color only, then leave off the -colorspace gray in these commands.
I would urge you to upgrade if you can. You are well over 200 versions old.
P.S. It is also possible your profile is causing a problem. I don't know what the RGB.icc profile is. Is that an Adobe RGB profile or an sRGB profile.
Can you reproduce your problem using Command Line ImageMagick? If so, post the command line you used. Sorry I do not know Ruby or RMagick.
P.S. 2 Apart from the lighter/darker issue, if you are trying to convert a grayscale image to color, then you will need to specify the output as PNG24:name.png. That is the only way to force a grayscale image to report colorspace=RGB without inserting color pixels.

Lossless YCbCr Tiff?

My goal is to create a Tiff image that natively holds uncompressed (or with lossless compression) YCbCr data inside (since the original image is YUV420 and IMO it would be a poor choice to upsample and convert to RGB).
I’m able to create (using imagemagick for instance) a valid YCbCr Tiff with a JPEG compressed data. When I try uncompressed / deflate / lzw I get a broken image (neither windows image viewer nor photoshop can open it). On page 94 of the TIFF 6.0 spec (under “Minimum Requirements for YCbCr Images”):
Compression = none (1), LZW (5) or JPEG (6). SHORT.
What’s wrong? Is there a Tiff extension which says that YCbCr color space is supported only when using jpeg compression?
Thanks,
Mark.
Command that works:
convert infileRGB.tif -colorspace ycbcr -compress jpeg outfileYCbCr.tif
Commands that don't work (broken tiff):
convert infileRGB.tif -colorspace ycbcr -compress none outfileYCbCr.tif
convert infileRGB.tif -colorspace ycbcr -compress lzw outfileYCbCr.tif

ImageMagick convert adds several extra "border" colors from tiff to jpeg?

I created an 8-bit .tiff image ("test.tiff") containing a grid of 30 different color patches in the RGB color space using ImageMagick -convert.
When I convert this image into a jpeg (which is what I need) using:
convert -quality 100 -colorspace RGB -depth 8 test.tiff test.jpg
The identify -verbose command reveals that the resulting jpeg has several additional colors in the color table, each only taking up a few (1-4) pixels and residing very near the desired colors in RGB space. My assumption is that some kind of border bleeding is happening; maybe due to compression?
I don't understand why this border bleeding has occurred, especially given that it does not occur when I convert the tiff image to either a bmp or pcx image.
Thank you
By definition, JPEG is a lossy compression. The effects your experiencing are expected with the JPEG format. Setting the -quality of 100 will not have a 1-to-1 image result as tiff.
See additional answers:
Should I use JPG or TIFF for high-quality prints?
[...] because every time [JPEG] would save it it would generate some changes.
Is Jpeg lossless when quality is set to 100?
At [quality] 100, you just get the LEAST loss possible.
I don't know how you created your 30 colour swatch, or how your histogram looks, but you might try adding -dither None and -colors 30 options to your convert commands:
convert test.tiff -dither None -colors 30 ...

imagemagick convert CMYK pdf to RGB jpeg or PNG and preseerve colors

I have a cmyk pdf that I am trying to convert to a RGB jpeg or png file but have the colors stay pretty close to what the CMYK version is (compared to how photoshop does it)
I am trying the following command but the colors change drastically from a red color to almost bright neon red and so on.
Here is the command
convert cmykpdf.pdf +profile icc -profile AdobeRGB1998.icc -colorspace sRGB jpegtesting.jpg
Any ideas? or thoughts on how to do this. I tried saving it as a PNG also and same issue occurs and have tried changing sRGB to just RGB
NOTE: It doesnt necessarily need to be RGB jpeg it can even be CMYK jpeg but i just need it to be displayed in the browser correctly and I know safari does not display cmyk jpegs correctly
My goal is to just display a img in the browser that shows the correct color and correct resolution nothing pixilated
The solution is fairly easy, there's nothing voodoo or special about Photoshop's CMYK to RGB nowadays. Imagemagick uses LCMS color engine, which does its job just fine.
But first you'll need to edit delegates.xml file inside IM's directory. Find the line with delegate decode="ps:cmyk" and insert -dUseCIEColor=false near the end, so it looks like that:
<delegate decode="ps:cmyk" restrain="True" command=""#PSDelegate#" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pamcmyk32" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" -dUseCIEColor=false "-f%s" "-f%s""/>
It's necessary because otherwise Ghostscript (before returning pam image to ImageMagick) will perform CMYK to CMYK convertion (assuming DeviceCMYK to be CIEbased CMYK), and you probably don't want that, as colors will shift considerably.
Then try this command:
convert -density 144 cmyk.pdf -profile USWebCoatedSWOP.icc -resample 72 -profile "sRGB Color Space Profile.icm" -quality 100 out.jpg
Here we take cmyk.pdf (rather, temporary pam image that GS returns to IM), assign CMYK profile (just as you do in Photoshop, when you open a file or do it explicitly - therefore choose profile that describes you input CMYK best), convert it to sRGB profile (because I don't think you want AdobeRGB as color space of images for Internet) and save to jpeg. Reduce quality parameter as needed.
One more trick here is additional manual anti-aliasing -- note intermediate resolution of 144 dpi and final 72 dpi. Because I don't think that Ghostscript's anti-aliasing with -dGraphicsAlphaBits=4 is en par with Photoshop's anti-aliasing.
The result of this command looks exactly the same as converted in Photoshop.
You could try this:
convert -negate -colorspace RGB srcfile.jpg outputfile.jpg
Let me know if it works!
Based on previous answers, I finally managed to keep natural colors from a CMYK pdf to a RGB png simply using:
convert -colorspace sRGB cmyk.pdf rbg.png

Resources