Multiple converts from one source using imagemagick - imagemagick

Is it possible to have one source and from that have 2 types of processed images with different configurations? (and not to read the source image twice)?
Something like
convert input.jpg -resize 300 output1.jpg -resize 600 output2.jpg

Sure, use an intermediate -write like this, and do the big one first:
convert input.jpg -resize 600 -write im600.jpg -resize 300 im300.jpg
Or, if you want to start afresh with each operation:
convert input.png \
\( +clone -resize 300 -write result300.jpg \) \
\( +clone -resize 600 -write result600.jpg \) null:
If using GraphicsMagick, I am not sure if there is a better way than the following:
#!/bin/bash
{ echo convert input.png mpr:orig;
echo convert mpr:orig -resize 300 result300.jpg;
echo convert mpr:orig -resize 600 result600.jpg; } | gm batch -prompt off
This is a slightly different version that only invokes convert twice:
#!/bin/bash
cat - <<EOF | gm batch -prompt off
convert input.png -write mpr:orig -resize 300 result300.jpg
convert mpr:orig -resize 600 result600.jpg
EOF

Related

Print pixel values where images differ with imagemagick?

Is there an easy way to print the pixel value where two images differ using imagemagick?
To be clear, I want to know what the value of that pixel is, as well as its coordinate. It doesn't matter from which image, since I can simply swap them to get the right one.
Let's make two images, both 3px wide and 1px tall:
convert xc:red xc:lime xc:blue +append 1.png
convert 1.png -flop 2.png
If we do the following, we can make any pixels that are identical in the two images become transparent:
convert {1,2}.png -compose changemask -composite mask.png # Note that {1,2}.png is just bash shorthand for "1.png" "2.png"
And if we re-order the input images:
convert {2,1}.png -compose changemask -composite mask.png # Note that {2,1}.png is just bash shorthand for "2.png" "1.png"
So, I assume you want the above, but in text format with the transparent pixels suppressed:
convert {1,2}.png -compose changemask -composite txt: | grep -v ",0)"
Output
# ImageMagick pixel enumeration: 3,1,65535,srgba
0,0: (65535,0,0,65535) #FF0000FF red
2,0: (0,0,65535,65535) #0000FFFF blue
Note that if you want to permit a small difference between the images, you can add some "fuzz-factor". So, if you want rgb(0,100,200) to be considered near enough equal to rgb(3,96,205), you could add -fuzz 5 at the start of the command.
In Imagemagick 6, you can do the following to list the coordinates where the two images differ:
convert image1 image2 -compose difference -composite -threshold 0 txt: | tail -n +2 | grep "white" | awk '{print $1}' | sed 's/://g'
If using Imagemagick 7, change convert to magick
ADDITION:
If you want both the coordinates and the color in one of the two images, then assuming the image has no perfect black pixels, you can do the following:
convert image1 image2 \
\( -clone 0,1 -compose difference -composite -threshold 0 \) \
-delete 1 \
-compose multiply -composite txt: |\
tail -n +2 | grep -v "black" | awk '{print $1,$4}'
For example, I take the lena image and put a blue square in the top left corner to make a second image.
Input:
convert lena.png \( -clone 0 -size 5x5 xc:blue -composite \) \
\( -clone 0,1 -compose difference -composite -threshold 0 \) \
-delete 1 \
-compose multiply -composite txt: |\
tail -n +2 | grep -v "black" | awk '{print $1,$4}'
Results:
0,0: srgb(226,137,124)
1,0: srgb(224,137,130)
2,0: srgb(225,135,121)
3,0: srgb(228,134,121)
4,0: srgb(227,138,125)
0,1: srgb(226,137,124)
1,1: srgb(224,137,131)
2,1: srgb(225,135,121)
3,1: srgb(228,134,121)
4,1: srgb(227,138,126)
0,2: srgb(226,138,124)
1,2: srgb(224,136,127)
2,2: srgb(225,135,120)
3,2: srgb(228,134,121)
4,2: srgb(227,137,121)
0,3: srgb(228,137,122)
1,3: srgb(225,134,114)
2,3: srgb(225,134,118)
3,3: srgb(229,132,112)
4,3: srgb(227,133,113)
0,4: srgb(224,130,109)
1,4: srgb(223,132,110)
2,4: srgb(224,132,116)
3,4: srgb(226,131,112)
4,4: srgb(226,134,117)
If you do have black and the images have no transparency, then you can do:
convert image1 image2 \
\( -clone 0,1 -compose difference -composite -threshold 0 \) \
-delete 1 \
-alpha off -compose copy_opacity -composite \
-background black -alpha background txt: |\
tail -n +2 | grep -v "none" | awk '{print $1,$4}'
For example:
convert lena.png \( -clone 0 -size 5x5 xc:blue -composite \) \
\( -clone 0,1 -compose difference -composite -threshold 0 \) \
-delete 1 \
-alpha off -compose copy_opacity -composite \
-background black -alpha background txt: |\
tail -n +2 | grep -v "none" | awk '{print $1,$4}'
Results:
0,0: srgba(226,137,124,1)
1,0: srgba(224,137,130,1)
2,0: srgba(225,135,121,1)
3,0: srgba(228,134,121,1)
4,0: srgba(227,138,125,1)
0,1: srgba(226,137,124,1)
1,1: srgba(224,137,131,1)
2,1: srgba(225,135,121,1)
3,1: srgba(228,134,121,1)
4,1: srgba(227,138,126,1)
0,2: srgba(226,138,124,1)
1,2: srgba(224,136,127,1)
2,2: srgba(225,135,120,1)
3,2: srgba(228,134,121,1)
4,2: srgba(227,137,121,1)
0,3: srgba(228,137,122,1)
1,3: srgba(225,134,114,1)
2,3: srgba(225,134,118,1)
3,3: srgba(229,132,112,1)
4,3: srgba(227,133,113,1)
0,4: srgba(224,130,109,1)
1,4: srgba(223,132,110,1)
2,4: srgba(224,132,116,1)
3,4: srgba(226,131,112,1)
4,4: srgba(226,134,117,1)

Convert PDF to Image(s) with different resolution using ImageMagick

I have to convert the pdf file (with more text) to images with different resolution.
I am executing the following commands:
width:1024px
convert -density 150 -antialias -resize 1024x -quality 80 "${inputFilePath}" "${outputFilePath}"
width:720px
convert -density 150 -antialias -resize 720x -quality 80 "${inputFilePath}" "${outputFilePath}"
width:320px
convert -density 150 -antialias -resize 320x -quality 80 "${inputFilePath}" "${outputFilePath}"
The generated images are not clear.
Can you help me to understand what I have to change?
Thanks in advance.
Increase your density to get better quality. -antialias will not help in your ImageMagick command. It is only used when adding new text. Put the density after convert and then put the input. Put your quality after the input and before the output. Use parenthesis processing with clones to generate all 3 results in one convert command.
convert -density 300 input \
\( -clone 0 -resize 1024x -quality 80 +write output1 \) \
\( -clone 0 -resize 720x -quality 80 +write output2 \) \
\( -clone 0 -resize 320x -quality 80 +write output3 \) \
null:

Imagemagick (convert: pixels are not authentic)

I have a command-line that outputs an image as intended but gives me an error on completion of convert: pixels are not authentic. Why could this be happening?
I am using ImageMagick 6.9.2-8 Q16 x86_64 2015-12-06 in Term2 on OSX El Capitan.
The command / output / error :
convert -verbose artwork.jpg -resize 1800x \
\( +clone -gravity center -background white -extent 2000x2000 \) \
\( -clone 1 displaceY.png -compose displace -define compose:args=0x5% -composite \) \
\( -clone 2 -gravity west displaceX.png -compose displace -define compose:args=5x0% -composite \) \
-delete 0--2 \( +clone alpha.png -compose copy_opacity -composite \) -delete 0 out.png
artwork.jpg JPEG 2952x2124 2952x2124+0+0 8-bit sRGB 911KB 0.000u 0:00.000
displaceY.png PNG 2000x2000 2000x2000+0+0 8-bit sRGB 109KB 0.000u 0:00.000
displaceX.png PNG 2400x2400 2400x2400+0+0 8-bit sRGB 104KB 0.000u 0:00.000
alpha.png PNG 2000x2000 2000x2000+0+0 8-bit sRGB 63.9KB 0.000u 0:00.000
artwork.jpg=>out.png JPEG 2952x2124=>2000x2000 2000x2000+0+0 8-bit sRGB 572KB 0.000u 0:00.000
convert: pixels are not authentic `artwork.jpg' # error/cache.c/QueueAuthenticPixelCacheNexus/4017.
It's hard to debug without the files and without knowing what you are trying to achieve, but I'll say what I see and maybe that will help. Here is what I think you have in the various layers:
0 - artwork 1800px wide
1 - artwork extended to 2000x2000
2 - clone of (1)
3 - clone of (2)
and then we come to the last line... and you delete 0--2 which is suspicious, did you mean 0-2, because 0--2 is actually 0,1.
So what did you mean to have in your image list after this -delete 0--2, I mean, how many images? I guess you meant to have 1 left.
Then you clone it, why do you do that? You could just copy the opacity right onto it and then you wouldn't need a -delete at the end?
No one can answer why this may have been happening but most could agree there is a better way of doing it, a few of these produce no error. It seems to be related to using clone request after a certain point in the chain.
Here are two commands that resolve this issue and provide the correct output image:
convert artwork.jpg +repage -thumbnail 1800x -gravity center -background white -extent 2000x2000 \
-gravity northwest displaceY.png +repage -compose over -compose displace -define compose:args=0x5% -composite \
-gravity northwest displaceX.png +repage -compose over -compose displace -define compose:args=5x0% -composite \
-gravity center alpha.png -compose over -compose copy_opacity -composite final.png
or
convert artwork.jpg +repage -thumbnail 1800x -gravity center -background white -extent 2000x2000 \
-gravity northwest displaceX.png displaceY.png +repage -compose over -compose displace -define compose:args=0x5% -composite \
-gravity center alpha.png -compose over -compose copy_opacity -composite final.png
Thanks to Fred over in the ImageMagick forums.

Generate thumbnails for only the first frame of animated gif with mogrify

How to modify this code so mogrify will generate thumbnails only for the first frame of animated gifs?:
mogrify -resize 80x80 -background white -gravity center -extent 80x80 -format jpg -quality 75 -path thumbnails *.gif
The select frame notation would be [N] immediately after the filename; where N is the frame number you wish to select. See Selecting Frames section under the input filename examples.
mogrify -resize 80x80 \
-background white \
-gravity center \
-extent 80x80 \
-format jpg \
-quality 75 \
-path thumbnails \
*.gif[0]

Transparent PNGs and a JPEG combine in ImageMagick

I have a case where I need to combine two transparent layers on top of JPEG files.
Here a sample setup:
wget -O bg.jpg http://www.grahamowengallery.com/photography/Flowers/roadside-flowers.jpg
wget -O layer.png http://www2.picturepush.com/photo/a/6271450/640/TRANSPARENT-EMBELLISHMENTS/pink-flower-transparent-png.png
wget -O logo.png http://upload.wikimedia.org/wikipedia/commons/0/0d/Imagemagick-logo.png
I can get desired result with commands:
composite bg.jpg \( -compose Overlay layer.png \) bg2.jpg
composite bg2.jpg \( -compose Overlay logo.png \) result.jpg
This is good, but I want to avoid writing bg2.png to drive.
I tried:
composite bg.jpg \( -compose Overlay layer.png \) \( -compose Overlay logo.png \) result2.jpg
but this results on layer.png on black background. How can I fix this?
I couldn't make composite working, but convert works:
convert.exe bg.jpg layer.png -compose Overlay -composite logo.png -compose Overlay -composite result2.jpg
Further reading: http://www.imagemagick.org/Usage/compose/
I can not test this at the moment but you may be able to use layers merge and you should be able to use the URLs in your code.
$cmd = "http://www.grahamowengallery.com/photography/Flowers/roadside-flowers.jpg ".
" http://www2.picturepush.com/photo/a/6271450/640/TRANSPARENT-EMBELLISHMENTS/pink-flower-transparent-png.png ".
" http://upload.wikimedia.org/wikipedia/commons/0/0d/Imagemagick-logo.png -layers merge ";
exec(" convert $cmd result.jpg ");
You are not using any positioning for your layers - are you going to introduce that later? If so you can add -page +0+0 in front of each image to locate them where you want. +0+0 would be changed to the location.

Resources