ImageMagick - Make a color semi-transparent - imagemagick

I am trying to overlay image1 over image2. I want to make a color in image1 semi-transparent. So far I have only been able to overlay image1 over image2 using
composite -gravity north image2.png image1.png image3.png
How do I make a color (grey in this case) in image1 semi-transparent before overlaying it over image2?
I am using:
Version: ImageMagick 7.0.8-23 Q16 x86_64 2019-01-09
Thnx

Let's make an overlay (image2.png) first, with 3 progressively lighter shades of grey starting with 80/255 on the left, 128/255 in the middle and 200/255 on the right:
convert -size 200x438 xc:"gray(80,80,80)" xc:gray xc:"gray(200,200,200)" +append image2.png
Assuming our starting image is this:
We can now make precisely mid-grey into semi-transparent mid-grey and overlay like this:
convert bean.jpg \( image2.png -fill "rgba(128,128,128,0.5)" -opaque gray \) -composite result.png
Or, if we want to also affect the grey(80), we can incorporate some fuzz:
convert bean.jpg \( image2.png -fill "rgba(128,128,128,0.5)" -fuzz 20% -opaque gray \) -composite result.png
Note that at Version 7 of ImageMagick, the commands changed:
Version 6 | Version 7
=================================
identify | magick identify
convert | magick
mogrify | magick mogrify
composite | magick composite
montage | magick montage
compare | magick compare
animate | magick animate
stream | magick stream
The order of parameters is also stricter, favouring:
magick [settings] INPUT [settings] [operators] OUTPUT
over:
convert [settings] [operators] INPUT [settings] [operators] OUTPUT
What I mean is that you are expected to load an image prior to applying operators to it, rather than to build up a list of operators and then load an image and hope ImageMagick remembers what you said you wanted done if you ever loaded anything.

Related

imagemagick montage: insert images of different sizes

I have an image a.png of size 800x600, and two images b.png and c.png of size 400x300 each. I want to montage (insert) the three images without size changes into a png image of size 800x900, such that a.png is at the top, and b.png and c.png side-by-side underneath.
How can I do this with imagemagick convert or montage?
Generate sample images, 1/10th the real size:
magick -size 80x60 -background red -gravity center label:"A" a.png
magick -size 40x30 -background lime -gravity center label:"B" b.png
magick -size 40x30 -background blue -gravity center label:"C" c.png
Now, you can load A, and inside parentheses, load B and C and place side-by-side as a new image, then append that result below A:
magick a.png \( b.png c.png +append \) -append result.png
Or, if you dislike the parentheses, you can join B and C side-by-side into a single image, load A, swap the order so A is at the top and then append the combined image below:
magick b.png c.png +append a.png +swap -append result.png
Note the distinction between:
+append meaning "append to the right", and
-append meaning "append below".
Note that, in addition to +append and -append, there are the newer tools called +smush and -smush which do exactly the same but take a parameter which is the number of pixels to offset the appending. So if you do +smush 5 it will do the same as +append but leave 5 pixels of background colour showing in the new gap between the images. If you use a negative offset, it will append in the same place but overlap the two images by the offset.
Here's an example, I make the background magenta and smush B and C with a 10 pixel gap. Then change the background to yellow before smushing the result below A with a 15 pixel offset.
magick -background magenta b.png c.png +smush 10 a.png +swap -background yellow -smush 15 result.png
If still using v6 ImageMagick, replace magick with convert.

How to split an image with a grid and preserve transparency bounding box

I have some png images that I want to split it into parts, like by grid or size.
But each part should have the same bounding box (transparency) as original image.
Example:
Splitting image into 2 parts.
Original: 200 × 89
Output:
part_1.png, 200 × 89
part_2.png, 200 × 89
Can ImageMagick do this? Or any other app or method.
My actual goal is to split into 100+ slices images.
EDIT:
Another goal to have an indents for each slice. Say indent = 10px.
Example:
Input: 200 x 100
Output:
part_1.png, 200 x 100
part_2.png, 200 x 100
And just as example, to visually compare input and output: combined output images in Photoshop as layer added one onto another
200 x 100 :
Also this is showing input image added onto combined(so it's better to see what was cropped and how):
In ImageMagick, you can split an image into many parts with the -crop command. For your example above with two parts, you can do that with the following commands. ImageMagick will append -0, -1 ... to the output file names.
ImageMagick 6:
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% \) -layers composite result.png
ImageMagick 7:
magick \( image.png -set option:dim "%wx%h" -crop 50x100% \) null: \( -size "%[dim]" xc:none \) -reverse -layers composite result.png
The results are:
See
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/crop/#crop_percent
http://www.imagemagick.org/Usage/crop/#crop_tile
http://www.imagemagick.org/Usage/crop/#crop_quad
http://www.imagemagick.org/Usage/crop/#crop_equal
http://www.imagemagick.org/script/command-line-options.php#layers
Note that -crop keeps the virtual canvas information if you do not add +repage afterwards. So to put the individual images back into their original placement, you have to composite them onto a transparent background the size of the input. That is done in one command using -layers composite using the null: separator.
Here is another way to add transparent areas between parts of a crop in ImageMagick. Crop the image into pieces, chop off the parts you want to remove, then pipe to montage to add the spacing back.
Input:
Here I make this into a 4x4 grid of images with 10 pixel spacing:
convert lena.png -crop 25%x25% +repage -gravity east -chop 10x0 -gravity south -chop 0x10 +repage miff:- | montage - -background none -tile 4x4 -geometry +5+5 result.png
To answer your new question, you can do that with a script loop. On a Unix-like platform, assuming your images do not have spaces, you can do the following:
cd path/to/current_folder
list=`ls *.png`
for img in $list; do
name=`convert $img -format "%t" info:`
dim=`convert $img -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( $img -crop 50x100% \) -layers composite -scene 1 path/to/new_folder/${name}_%d.png
done
If you want leading 0s in the output, say 3, use path/to/new_folder/${name}_%03d.png.
Note that to start with 1 rather than 0, I have added -scene 1.
Sorry, I do not know how to script for Windows.
Please always provide your ImageMagick version and platform.
In ImageMagick, the best way to put transparent areas into your image is with a binary mask that is put into the alpha channel of your image.
convert input.png \( -size 200x89 xc:white -size 10x89 xc:black -gravity center -composite \) -alpha off -compose copy_opacity -composite result.png
You can add as many blank areas as you want by adding more white areas to the mask or by tiling out one region of black and one region of white to create the mask with regular spacing of black and white.
Edited to add this ImageMagick 6 example which splits the input image into 4 pieces, 25% of the original width and 100% of its height, then creates a transparent canvas for each piece the same dimensions of the input image, and locates the pieces at their original offsets on those canvases.
convert input.png -set option:distort:viewport %[w]x%[h] -crop 25x100% \
-virtual-pixel none -distort affine "0,0 %[fx:s.page.x],%[fx:s.page.y]" out%03d.png
The output file names will be numbered starting from zero like "out000.png", etc.
Original message...
Here's a simple command using ImageMagick 7 that can crop an image into any number of pieces, and output all the pieces at their original offsets on transparent backgrounds of the original input dimensions...
magick input.png -crop 100x1# -background none \
-extent "%[fx:s.page.width]x%[fx:s.page.height]-%[fx:s.page.x]-%[fx:s.page.y]" out%03d.png
That "-crop 100x1#" tells it to split the image into a grid 100 pieces wide by 1 piece high. You could just as well specify the crop sizes as percents or numbers of pixels.
Edited again to add:
This following command will split the input image into the individual pieces specified with the "-crop" operator, then shave 5 pixels from every side of each piece, then apply a 5 pixel transparent border to every side of each piece. It will still remember the original locations of the pieces within the input canvas, so the "-distort affine ..." can extend the canvases and place the pieces where they were in the input image.
convert input.png -set option:distort:viewport %[w]x%[h] \
-bordercolor none -background none -virtual-pixel none \
-crop 25x100% -shave 5x5 -border 5x5 \
-distort affine "0,0 %[fx:s.page.x],%[fx:s.page.y]" out%03d.png
To use this command with IM7 you need to change "convert" to "magick".
Given the changes of requirements provided by Kamikaze, here is one way to achieve the split with indent in ImageMagick, assuming I understand correctly.
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% -shave 5x5 \) -geometry +5+5 -layers composite result.png
To check, I flatten over a blue background:
convert result-0.png result-1.png -background blue -flatten result.png

Overlay PNGs with ImageMagick while keeping transparency

I have two images:
Image 1
In this image, the white region plus the white + pink region are transparent.
Image 2
GOAL
I want to merge both images (Image 1 in front, Image 2 behind) by:
Keeping the transparent region from Image 1 so that Image 2 can be
seen through the white mask.
Having the chance to locate Image 2 by vertically centering the photo in the middle of the white region.
Then, I'd like to obtain a result like this:
HOWEVER
I am using the following command in ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 in Ubuntu 16.04:
convert \( Image1.png -resize 447x640 \) \( -compose Overlay Image2.png \) -gravity north -composite Image3.png
I've tried countless times but the best result I can get (by using command above) is Image 3. Can anyone help me? Thank you.
Image 3
I think this is what you want using Imagemagick in Unix syntax:
Img:
Mask:
convert \( mask.png -alpha off \) img.jpg \( mask.png -alpha extract -negate \) -compose over -composite result.png
or more simply:
convert mask.png img.jpg -compose dstover -composite result.png

Compositing premultiplied images using ImageMagick

I have two images. One is background with no alpha. The other is a white cloud. The alpha of the cloud image is premultiplied with black. When I composite them the white cloud has black in it, so it looks grey instead of white like it should. I'm doing:
convert -gravity Center bg.tga whitecloud.tga -composite comp.tga
Is there a way to composite premultiplied images in ImageMagick, or does the image have to be non-premultiplied? Can I make a premultiplied image non-premultiplied using ImageMagick?
Update:
Ok, here are the images as TGA for download:
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/bg.tga
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/whitecloud.tga
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/aftereffects.tga
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/imagemagick.tga
and in the same order as jpgs to view in your browser:
I tried all the modes provided, but none of them create the same result as After Effects.
It would be easier if you showed your images, but try adding -compose lighten before -composite in your command, like this:
convert a.tga b.tga -compose lighten -composite out.tga
Basically that will make ImageMagick choose the lighter pixel of the two images at every point.
If that doesn't work, try other blending modes
for b in $(identify -list compose); do
convert -label "$b" bg.tga whitecloud.tga -compose $b -composite miff:-
done | montage - -tile 5x out.png
I am kind of thinking Atop, Dissolve, SrcAtop and SrcOver might be your friends but have a look full-size and see what floats your boat. That would be
convert a.tga b.tga -compose Atop -composite out.tga
Here is an Imagemagick command that does what you want:
convert -gravity Center whitecloud.tga -fx "u/max(u.a, 1/255)" bg.tga +swap -composite -fx "u*u.a" comp.tga
What's happening here?
-fx command #1: Convert whitecloud.tga from premultiplied alpha to "normal". The max() operator is a special case to avoid dividing by zero.
+swap command: Make bg.tga the first image and the revised whitecloud.tga the second.
-composite these two regular, non-premultiplied images.
-fx command #2: take the result, and return to a premultiplied alpha format.
This gives exactly the same result as After Effects.
Note that, as I wrote it, it only works for an opaque bg.tga. You'd need to do some extra work to handle a transparent background image.
If you want to duplicate the After Effects result, then I believe what you want to do in ImageMagick is the following -- composite the background image with a white image using the cloud as a mask:
convert bg.tga \( -clone 0 -fill white -colorize 100 \) whitecloud.tga -compose over -composite cloud_blue.tga
I have posted a JPG result, but my .tga result is the same.

Adaptive background after rotation/deskew in Imagemagick

After I rotate or deskew an image with Imagemagick there's a white background in the corners, where the rotation took place. Example:
convert image.png -rotate 10 out.png
Output: http://imgur.com/8bZQ6
Is there a way I could somehow fill up those white corners with some texture or at least a color that blends in with the image? Cropping is not an option.
I've found this great solution for simple rotation:
convert image.png -virtual-pixel Edge +distort SRT 10 out.png
Output: http://imgur.com/edMS0
But unfortunately it doesn't work with the -deskew command...
So, does anyone know how to fill up those corners in a similar way for the -deskew (and -rotate) command? The point is to mask the fact the image was rotated as best as possible.
I'll go ahead and answer my own question, but I'm still hoping for a better solution.
You can set the -background color, which works with rotate and deskew.
Here's a solution that uses the average image color:
convert image.png -background `convert image.png -resize 1x1 txt:- | tail -1 | cut -b 30-50` -rotate 10 out.png
Output: http://imgur.com/YCqkC
And a better solution that takes some border pixels:
convert image.png -background `convert image.png -resize 100x1! \( +clone -crop 1x1+0+0 \) +append -crop 2x1+99+0 -resize 1x1 txt:- | tail -1 | cut -b 30-50` -rotate 10 out.png
Output: http://imgur.com/4wDPO
There's some bash scripting (inside the backticks), so these solutions are unix only.
Try this:
convert image.png -background transparent -rotate 10 out.png

Resources