Transparent PNGs and a JPEG combine in ImageMagick - 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.

Related

Unable to copy alpha channel using ImageMagick

I have two tif images - input.tif and test.tif - and what I want to achieve is apply alpha channel of the first image to the second one. This is what I do now, using ImageMagick:
C:\Images> magick input.tif -alpha extract alpha.pgm
C:\Images> magick test.tif alpha.pgm -compose copyalpha -composite output.tif
When, however, I try to open output.tif, I get a message that the image is corrupt.
try adding -alpha off in your second command
C:\Images> magick input.tif -alpha extract alpha.pgm
C:\Images> magick test.tif alpha.pgm -alpha off -compose copy_opacity -composite output.tif
Or you can do it in one command as:
C:\Images> magick test.tif ( input.tif -alpha extract ) -alpha off -compose copy_opacity -composite output.tif

Multiple converts from one source using 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

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.

ImageMagick: How to resize proportionally with mogrify without a background

I was following this example http://cubiq.org/create-fixed-size-thumbnails-with-imagemagick, and it's exactly what I want to do with the image, with the exception of having the background leftovers (i.e. the white borders). Is there a way to do this, and possibly crop the white background out? Is there another way to do this? The re-size needs to be proportional, so I don't just want to set a width re-size limit or height limit, but proportionally re-size the image.
The example you link to uses this command:
mogrify \
-resize 80x80 \
-background white \
-gravity center \
-extent 80x80 \
-format jpg \
-quality 75 \
-path thumbs \
*.jpg
First, mogrify is a bit dangerous. It manipulates your originals inline, and it overwrites the originals. If something goes wrong you have lost your originals, and are stuck with the wrong-gone results. In your case the -path thumbs however alleviates this danger, because makes sure the results will be written to sub directory thumbs
Another ImageMagick command, convert, can keep your originals and do the same manipulation as mogrify:
convert \
input.jpg \
-resize 80x80 \
-background white \
-gravity center \
-extent 80x80 \
-quality 75 \
thumbs/output.jpg
If want the same result, but just not the white canvas extensions (originally added to make the result a square 80x80 image), just leave away the -extent 80x80 parameter (the -background white and gravity center are superfluous too):
convert \
input.jpg \
-resize 80x80 \
-quality 75 \
thumbs/output.jpg
or
mogrify \
-resize 80x80 \
-format jpg \
-quality 75 \
-path thumbs \
*.jpg
I know this is an old thread, but by using the -write flag with the -set flag, one can write to files in the same directory without overwriting the original files:
mogrify -resize 80x80 \
-set filename:name "%t_small.%e" \
-write "%[filename:name]" \
*.jpg
As noted at http://imagemagick.org/script/escape.php, %t is the filename without extension and %e is the extension. So the output of image.jpg would be a thumbnail image_small.jpg.
This is the command I use each time I want to batch resized everything to 1920x and keep aspect ratio.
mogrify -path . -resize 1920x1920 -format "_resized.jpg" -quality 70 *.jpg

GraphicsMagick - position item in bottom left

After trying every combination of commands that I could possibly think of I still can't get this to work.
I have a large image that can vary in size: Logo.png
I have a small image of a 'known' size: Wallpaper.png
I want Logo to appear in the Bottom Left of Wallpaper.
This has to be done using the 'gm convert' command using -flatten. Using 'gm composite' would require me to run two commands which isn't acceptable as it would add too much time to our processing per image.
Here is the command so far (there will be more added to this command but here is the core of it):
wallpaper.png -page +0+0 -gravity SouthWest logo.png -compose over -flatten result.jpg
This puts the logo in the top left. Gravity appears to be ignored. Using +100% for -page does not work either.
I don't see the need for your use of -flatten and +page
The following ImageMagick command should work:
convert \
-composite \
-geometry +10+20 \
-gravity southwest \
background.png \
logo.png \
result.png
For GraphicsMagick this needs to change to:
gm \
composite \
-geometry +10+20 \
-gravity southwest \
logo.png \
background.png \
result.png
I added +10+20 to demonstrate how you can offset the overlaid logo a little bit from the extreme lower left corner.

Resources