Imagemagick convert grayscale image to color - imagemagick

I created an image with convert -size 1920x1080 xc:white white.png, but it appears to have a greyscale color profile and this is undesirable.
identify -verbose shows
Colorspace: Gray
Type: Grayscale
for "normal" images, these properties don't even seem to be mentioned.
How can I change it so that the image can have color?

You can force an RGB888 image like this:
convert -size 1920x1080 xc:white PNG24:white.png
Check:
identify -verbose white.png | egrep "Colorspace:|Type:"
Colorspace: sRGB
Type: Bilevel

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.

Imagemagick non-conforming drawing primitive definition `matte' # error/draw.c/DrawImage/3284

I executed the following in console (cygwin x64) in Windows
magick convert -verbose difference.png -fuzz 7% -draw 'matte 1,1 floodfill' test.png
difference.png PNG 216x107 216x107+0+0 8-bit Gray 11167B 0.000u 0:00.002
difference.png=>test.png PNG 216x107 216x107+0+0 8-bit Gray 8737B 0.078u 0:00.077
However, I got the following error:
convert: non-conforming drawing primitive definition `matte' # error/draw.c/DrawImage/3284.
Please mention on how I can correct this error.
The drawing primitive matte was replaced by alpha in ImageMagick 7. From Porting to ImageMagick Version 7 article.
The DrawMatte() method is now called DrawAlpha().
As Mark Setchell pointed out in the comments, replacing matte with alpha should work as expected.
magick convert -verbose difference.png -fuzz 7% -draw 'alpha 1,1 floodfill' test.png

Create blank image in Imagemagick

How to create a blank new image in Imagemagick via command line?
Using -background doesn't work:
$ convert -size 800x800 -background white x.png
convert: no images defined `x.png' # error/convert.c/ConvertImageCommand/3257.
White background
convert -size 800x800 xc:white white.png
xc: used to mean "X Constant Image" but now is just a shorthand for canvas:. This means you can also use:
convert -size 800x800 canvas:white white.png
and because "white" is the default value if no color is provided, you can also use:
convert -size 800x800 xc: white.png
convert -size 800x800 canvas: white.png
Transparent background
If by "blank" you mean "transparent", just use that word as the color:
convert -size 800x800 xc:transparent transparent.png
Answer made possible by ImageMagick v6 Examples and How to create a new image?
You need to supply PNG24, PNG8 or PNG32 prefix if planning to use this canvas to layer colour images over. Without it, it creates a Grey colour space. I used 32, as I need "varying degrees of transparency for each pixel" (pixel art)
convert -size 800x800 canvas:transparent PNG32:canvas.png
For more about PNG types see this link.

How to convert gray scale png image to RGB from comand line using image magick

I am trying to convert an png Gray scale image to RGB png image using the following command.
convert HopeLoveJoy.png -size 1x1 -fill "rgba(0%,1%,2%,0)" -draw "color 511,511 point" out_test.png
By using the above the above command I am able to convert but the color of the image is getting changed.
Is any thing wrong in the command??
Any help is appreciated.
Thanks in advance.
If, as your question title implies, you really just want to go from greyscale to sRGB colorspace, use this:
convert image.png -define png:color-type=2 result.png
I check it like this:
convert image.png -define png:color-type=2 result.png && identify -format "%[colorspace]" result.png
sRGB

Convert RGB to Grayscale in ImageMagick command-line

How do I convert a RGB image (3 channels) to a grayscale one, using the (r+g+b)/3 method?
I look through an examples page: http://www.imagemagick.org/Usage/color_mods/#grayscale
but the desired method:
convert test.png -fx '(r+g+b)/3' gray_fx_average.png
gave me a wrong result - the resulted image has still 3 channels.
You can check this by running a command: identify -format "%[colorspace] <== %f\n" *.png.
convert <img_in> -set colorspace Gray -separate -average <img_out> gives the best result for any image for me.
Using the (r+g+b)/3 method will apply the effects of grayscale, but the image will remain in sRGB (which is the expected behavior for this method). You'll need to specify the desired colorspace along with the -fx command.
convert test.png -fx '(r+g+b)/3' -colorspace Gray gray_fx_average.png
Verify with identify -format "%[colorspace] <== %f\n" gray_fx_average.png
Gray <== gray_fx_average.png
To batch convert images in Fish shell:
for file in *.jpg; convert -colorspace Gray $file $file; end;
A few ways to that in Imagemagick command line are:
convert test.png -grayscale average gray_average.png
or
convert test.png -colorspace OHTA -channel r -separate +channel gray_average.png
or
convert test.png -intensity average -colorspace gray gray_average.png
or
convert test.png -colorspace HSI -channel blue -separate +channel gray_average.png
See
https://imagemagick.org/script/command-line-options.php#grayscale
https://imagemagick.org/script/command-line-options.php#intensity
https://imagemagick.org/script/command-line-options.php#colorspace
Seems like you are taking the red channel to do that, on
convert test.png -colorspace OHTA -channel r -separate +channel gray_average.png
i prefer the green channel (i heard that way works on tv sice ancient days, maybe the best)
I use convert mostly to convert colour pictures of documents into grey-scale pdf documents in order to perform OCR. My best results are using Rec709Luminance. So I recommend
convert colourpicture.png -grayscale Rec709Luminance greyscalepicture.png
Short command, nice outputs.
I use this with good result for gray-scale images (I convert from PNG):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20 {}.jpg
I use this for scanned B&W pages get them to gray-scale images (the extra arguments cleans shadows from previous pages):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20 -density 300 -fill white -fuzz 40% +opaque "#000000" -density 300 {}.jpg
I had an issue to convert an sRGB colorspace to a Gray colorspace. I had to delete Alpha channel manually before a conversion. In other case, the image will stay sRGB.
convert image_original.tga -alpha off -set colorspace Gray image_converted.tga

Resources