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
Related
I am trying to combine three channel-based TIFF files into a single RGB one. The individual files look like this (this one is red):
Based on another answer here, I tried this command to merge them:
convert \
\( ./raw/red.TIF -channel r -separate +channel \) \
\( ./raw/green.TIF -channel g -separate +channel \) \
\( ./raw/blue.TIF -channel b -separate +channel \) \
-set colorspace sRGB -combine \
./test.tif
However, instead of a nice full-colour image, what I get instead is:
(you can just make out the tiny, incorrectly combined image in the top left corner).
What is it I'm doing wrong here?
EDIT: You can get these TIFF files here. (These are Landsat images.)
Your TIFFs are pyramidal, or multi-resolution. You can see that with this command to look at the individual images in the blue TIFF:
magick identify LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[0] TIFF 7591x7731 7591x7731+0+0 16-bit Grayscale Gray 83.8667MiB 0.010u 0:00.006
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[1] TIFF 3796x3866 3796x3866+0+0 16-bit Grayscale Gray 0.010u 0:00.001
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[2] TIFF 1898x1933 1898x1933+0+0 16-bit Grayscale Gray 0.010u 0:00.001
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[3] TIFF 949x967 949x967+0+0 16-bit Grayscale Gray 0.010u 0:00.000
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[4] TIFF 475x484 475x484+0+0 16-bit Grayscale Gray 0.010u 0:00.000
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[5] TIFF 238x242 238x242+0+0 16-bit Grayscale Gray 0.000u 0:00.000
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[6] TIFF 119x121 119x121+0+0 16-bit Grayscale Gray 0.000u 0:00.000
To make the full-size RGB render you need to take the zero-index (highest resolution) image by using the sub-image number in square brackets after the filename:
magick LC08_L2SP_039035_20210708_20210713_02_T1_red.TIF[0] \
LC08_L2SP_039035_20210708_20210713_02_T1_green.TIF[0] \
LC08_L2SP_039035_20210708_20210713_02_T1_blue.TIF[0] \
-combine result.tif
In ImageMagick convert, I can select a specific color with e.g. -opaque blue. How can I select all grayscale colors (e.g. #000000, #707070, #ffffff)?
Not sure what you are trying to do, but this may help. The greyscale pixels will have a saturation of zero, so that is probably the easiest way to identify them.
First, make a funky sample image:
convert -size 400x100 gradient:black-white -bordercolor red -border 80 image.png
Now make all grey areas (those with very low saturation) transparent:
convert image.png -alpha on -channel A -fx "saturation<0.01?0:1" result.png
Note
Note that the -fx operator is extremely powerful but notoriously slow because it is actually interpolated for each and every pixel. If your images are large, the following technique may be more appropriate.
Basically, I clone the image and convert the whole thing to HSL colorspace and separate the channels. Then I discard the Hue and Lightness channels so I am left with just the Saturation. I then threshold that and copy that back to the original image as the alpha channel. On a 2000x2000 pixel image, this method will run in under a second whereas the -fx method will require 5-6 seconds.
convert image.png \( +clone -colorspace hsl -separate -delete 0,2 -threshold 1% \) -compose copy-opacity -composite result.png
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
With ImageMagick shell command (convert?)...
Given a colorful input.png image.
How to us input.png to produce a white output.jpg version with similar dimensions an opacity of 100% ?
I will later on use this layer in my workflow.
This should work:
convert input.png -threshold -1 output.jpg
This transforms any pixel with an intensity greater than (-1), i.e., all of them, to white.
It does not work with GraphicsMagick, though, because in GM the threshold value is unsigned (in ImageMagick it's a signed "double"). Neither of the applications documents exactly what is supposed to happen when the threshold is negative.
Here's a command that works on both ImageMagick and GraphicsMagick, and is documented:
[gm] convert input.png -fuzz 100% -fill white -opaque gray output.jpg
You could use fill, like this:
convert input.png -fill "#ffffff" output.jpg
or
convert input.png -fill white output.jpg
Or you can convert all three channels (red, green and blue) to "1" which is full intensity, like this:
convert input.png -channel red -fx "1" -channel green -fx "1" -channel blue -fx "1" output.jpg
I want to convert (a lot of) JPEG images to the Sun Raster format (see here for instance), because my program can only use this format.
The issue is that my program only allows images of type Old
or Standard i.e. with pixel written in the BGR order. (I should change that but don't have the time).
By default, ImageMagick generates files in the RGB format.
I have found a trick to swap the color planes, but as the file is style written as RGB I get fancy colors.
This is probably a simple option to pass to ImageMagick, but I didn't find it.
Do you have an idea?
Another way to swap colors is to use the -color-matrix option. For example, to convert RGB to BGR:
convert input.png -color-matrix '0 0 1
0 1 0
1 0 0' output.png
Reference: http://www.imagemagick.org/Usage/color_mods/
In Imagemagick 6, you can do that easily with -swap command. Lets swap the red and blue channels in the logo image.
convert logo.png -separate +channel \
-swap 0,2 -combine -colorspace sRGB logo_bgr.png
For very old versions of Imagemagick use RGB rather than sRGB.
In Imagemagick 7, use magick rather than convert.
I have an idea how to swap Red and Blue colors in RGB image:
convert ( image_RGB.bmp -channel B -separate ) \
( image_RGB.bmp -channel G -separate ) \
( image_RGB.bmp -channel R -separate ) -channel RGB -combine image_BGR.bmp
You just cut channels form original image and write them in reverse order.
I would have thought that in Imagemagick
convert image.jpg image.sun
or
convert image.jpg SUN:image.sun
would convert to BGR format if that is the standard for the sun raster format. See imagemagick.org/script/formats.php.
Have you tried that?
If that does not work, then use my -swap command but write to .sun file format rather than png, which I used above. That is
convert logo.png -separate +channel -swap 0,2 -combine -colorspace sRGB logo_bgr.sun