Combining tiff files by channel with imagemagick - imagemagick

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

Related

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

Wrap a gif with a png

There are two image, one is gif, another is png. Now I need to wrap the gif image with png image. I use ImageMagick and I think it may work for me.
convert src.png input.gif -gravity Center -composite des.gif;
As you see, the output is unexpected while there is a black block.
And the des.gif is not animated.
left is des.gif the right is input.gif
EDIT
Here is my test files. https://drive.google.com/drive/folders/0B-64AliLi9OnWVQyLWU3TElHNHM?usp=sharing
You have 77 frames. But the first frame is much smaller than all the rest. From Imagemagick (6.9.9.13 Q16 Mac OSX Sierra), I can see this from
identify input.gif
input.gif[0] GIF 216x384 216x384+0+0 8-bit sRGB 256c 3.20786MiB 0.010u 0:00.009
input.gif[1] GIF 1080x1920 216x384+0+0 8-bit sRGB 64c 3.20786MiB 0.010u 0:00.009
...
input.gif[76] GIF 1080x1920 216x384+0+0 8-bit sRGB 64c 3.20786MiB 0.000u 0:00.000
Also all frames but the first are near solid green. Only the first has a circle in it.
Nevertheless, if you fix your input.gif, you can composite them so all the gif frames animate over the background png using the following command:
convert image.png null: \( input.gif -coalesce \) -gravity Center -layers composite des.gif
Note that after the coalesce all frames become 216x384. That makes the overlay frames way too small. This will make all frames larger so that they fit the background. But still all will be green exact for the first.
convert image.png null: \( input.gif -coalesce -resize 1080x1920! \) -gravity Center -layers composite des.gif
See http://www.imagemagick.org/Usage/anim_mods/#background
If using Imagemagick 7, then change convert to magick.

Usage of -density switch with the do not enlarge flag for -resize does not work

I am working on a feature of an image conversion service, and I need to
scale an image - if the requested dimensions are bigger of the original image, it should not enrlage.
ImageMagick supports that, but when I use -density to improve quality of converted image (I convert from pdf to png) , the "do not enlarge" behavior does not work well.
examples:
Image attributes for /home/yzaslavs/Downloads/drawing.pdf
/home/yzaslavs/Downloads/drawing.pdf PBM 2271x1610 2271x1610+0+0 16-bit Bilevel Gray 457KB 0.000u 0:00.000
Starting conversions.............
convert /home/yzaslavs/Downloads/drawing.pdf -resize 5000x5000\> out.png
out.png PNG 2271x1610 2271x1610+0+0 8-bit sRGB 383KB 0.000u 0:00.000
convert -resize 5000x5000\> -background white -depth 8 -density 160x160 /home/yzaslavs/Downloads/drawing.pdf out.png
out.png PNG 5000x3545 5000x3545+0+0 8-bit sRGB 1.943MB 0.000u 0:00.000
-------
convert -background white -depth 8 -density 160x160 -resize 5000x5000\> /home/yzaslavs/Downloads/drawing.pdf out.png
out.png PNG 5000x3545 5000x3545+0+0 8-bit sRGB 1.943MB 0.000u 0:00.000
-------
convert -background white /home/yzaslavs/Downloads/drawing.pdf -resize 5000x5000\> out.pn
out.png PNG 2271x1610 2271x1610+0+0 8-bit sRGB 383KB 0.000u 0:00.000
-------
convert -background white -depth 8 /home/yzaslavs/Downloads/drawing.pdf -resize 5000x5000\> out.png
out.png PNG 2271x1610 2271x1610+0+0 8-bit sRGB 383KB 0.000u 0:00.000
-------
Thank you in advance for any help
This is how it works with ImageMagick v7:
# Basic, default density is 72dpi
convert -depth 8 a.pdf -resize 5000x5000\> info:
a.pdf PDF 595x842 595x842+0+0 8-bit sRGB 0.000u 0:00.000
# I specify density to match default and file comes out the same
convert -depth 8 -density 72 a.pdf -resize 5000x5000\> info:
a.pdf PDF 595x842 595x842+0+0 8-bit sRGB 0.000u 0:00.000
# I increase the density 10% and image gets 10% bigger - fair enough!
convert -depth 8 -density 80 a.pdf -resize 5000x5000\> info:
a.pdf PDF 661x935 661x935+0+0 8-bit sRGB 0.000u 0:00.000
# I double the density and the image doubles too - fair enough!
convert -depth 8 -density 144 a.pdf -resize 5000x5000\> info:
a.pdf PDF 1191x1684 1191x1684+0+0 8-bit sRGB 0.000u 0:00.000
# I quadruple the density and the image quadruples too
convert -depth 8 -density 288 a.pdf -resize 5000x5000\> info:
a.pdf PDF 2381x3368 2381x3368+0+0 8-bit sRGB 0.000u 0:00.000
# 8x density and image gets resized now too
convert -depth 8 -density 576 a.pdf -resize 5000x5000\> info:
a.pdf PDF 3535x5000 3535x5000+0+0 8-bit sRGB 2.700u 0:02.699
# Still bigger density and image still resized
convert -depth 8 -density 800 a.pdf -resize 5000x5000\> info:
a.pdf PDF 3535x5000 3535x5000+0+0 8-bit sRGB 3.890u 0:03.890

ImageMagick: Achieve multiple (and partially *conditional*) operations on an image with one single command

Sorry if my question is not a complex hard question but i tried a lot and didn't achieved what i expected :(
How do i use ImageMagic to achieve all these goals at the same time (win32):
Resize (scale) image ONLY if the (width/height) is bigger than 1000px so that the resulting image's max width/height is 1000px or less
Convert any type to JPG with 90% quality
Place a Trade mark in right bottom of the image (not required but it's better to have it)
Thanks for sharing you knowledge and experiences
I'll assume you want the aspect ratio being preserved if the resize operation should happen for the bigger images.
convert \
input-image \
-resize 1000x1000\> \
-quality 90% \
output.jpg
Please not the \> suffix to the 1000x1000 geometry setting! It is crucial. If you skip it in your command because you've overlooked it, then the command will not work as expected.
This command should fulfill your first two points. Key in the resize parameter is the \> addition to the usual {width}x{height} part. It tells ImageMagick to only resize the input file if either of width or height are more than 1000 pixels. (The \ is only there as an escape to keep your shell from interpreting the > as a redirection command.)
To include the 3rd point, there are a lot of different kinds of 'watermarks' I could imagine. I'll show only a very simple one here:
convert \
input-image \
-resize 1000x1000\> \
-quality 90% \
\
-fill white \
-undercolor '#00000080' \
-gravity southeast \
-annotate +5+5 \
' My Trademark ' \
\
output.jpg
I hope this example gives you enough of an idea in order to continue with your own experiments.
Update
As Jimmy B. thinks my command does not work, let me proof my case with an example...
Create three example images
for i in 2000x2000 2000x500 500x2000; do
convert -size $i xc:red red-${i}.jpg
done
Check the example images' size
identify red-*x*.jpg
red-2000x2000.jpg JPEG 2000x2000 2000x2000+0+0 8-bit sRGB 23.7KB 0.000u 0:00.000
red-2000x500.jpg[1] JPEG 2000x500 2000x500+0+0 8-bit sRGB 6.29KB 0.000u 0:00.000
red-500x2000.jpg[2] JPEG 500x2000 500x2000+0+0 8-bit sRGB 6.29KB 0.000u 0:00.000
Resize the three example images with my command
for img in red-*x*.jpg; do
convert \
${img} \
-resize 1000x1000\> \
-quality 90% \
\
-fill white \
-undercolor '#00000080' \
-gravity southeast \
-annotate +5+5 \
' My Trademark ' \
\
resized-${img}
done
Check the resized images' size
identify resized-red-*x*.jpg
resized-red-2000x2000.jpg JPEG 1000x1000 1000x1000+0+0 8-bit sRGB 7.43KB 0.000u 0:00.000
resized-red-2000x500.jpg[1] JPEG 1000x250 1000x250+0+0 8-bit sRGB 3.31KB 0.000u 0:00.000
resized-red-500x2000.jpg[2] JPEG 250x1000 250x1000+0+0 8-bit sRGB 2.99KB 0.000u 0:00.000

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