Image circle cropping in imagemagick - image

People, I am using imagemagick to crop a circle out of an image. But I am getting nothing when I run this command :
convert input.jpg +clone -threshold -1 -negate -fill white -draw "circle 539,539 539,0" -alpha off -compose copy_opacity -composite output_circ.jpg
I am getting an output like this:
Please let me know if I am missing something.

Fundamentally, you are only missing one aspect - JPEG files cannot store transparency, so you need to use a PNG or GIF, for example.
convert input.jpg -alpha on \( +clone -threshold -1 -negate -fill white -draw "circle 539,539 539,0" \) -compose copy_opacity -composite output_circ.png

Related

Cutting out a mask from a image with ImageMagick

I'm trying to cut out a shape from an image based on a mask I have. I tried a number of ways but I'm really struggling to get the result I need.
I'm using ImageMagick 7.1.0-51 Q16-HDRI on Windows.
The "base" image:
https://imgur.com/a/j1kioWu
The mask i'm using:
https://imgur.com/a/OcArICa
Expected result(photoshoped):
https://imgur.com/a/GjRoxyS
Edit: Reuploaded direct on imgur to maintain transparent background
In Imagemagick, you want to invert (negate) the polarity of black and white, then do a -compose difference -composite, then invert again.
Hair:
Face:
magick hair.png face.png -alpha off -colorspace gray -negate -compose difference -composite -negate result.png
Result:
If you want pure white in the face area, threshold the two images after the conversion to grayscale
magick hair.png face.png -alpha off -colorspace gray -auto-threshold otsu -negate -compose difference -composite -negate result2.png
Result 2:
ADDITION
If your input images have transparent backgrounds, then you have to extract the alpha channels, combine them to do the difference and then put that alpha channel back on the input.
Unix Syntax:
magick hair.webp \
\( +clone face_mask.webp -alpha extract -compose difference -composite \) \
-alpha off -compose copy_opacity -composite result.png
For Windows,remove the \ in front of the parentheses and change the end of line \ to ^.

How to add a background to a transparent PNG file?

I'm trying to add a PNG file as background to an PNG image with transparent regions using ImageMagick like so:
convert input.png -texture bg.png output.png
I'm not so far to find out how to fill my background to 100% width / height, because the above example does not work. I end up with the same file that is transparent and I don't know why.
How can I apply a background that uses full width/height across the whole file? (Preferable using the cover method, meaning to keep aspect ratio of the background and cutting overlaps)
If you want to tile out the background then on Unix, try
convert input.png \( +clone -tile bg.png -draw "color 0,0 reset" \) +swap -compose over -composite output.png
On Windows:
convert input.png ( +clone -tile bg.png -draw "color 0,0 reset" ) +swap -compose over -composite output.png
If you just want to resize the background, then on Unix try
dims=$( convert input.png -format "%wx%h" info: )
convert input.png \( bg.png -resize $dims +repage \) +swap -compose over -composite output.png
I do not know Windows that well to tell you how to do the variable
On ImageMagick 7, in Unix, you can do it easier as:
magick input.png -set option:dims "%wx%h" \( bg.png -resize "%[dims]" \) +swap -compose over -composite output.png
Or in Windows:
magick input.png -set option:dims "%wx%h" ( bg.png -resize "%[dims]" ) +swap -compose over -composite output.png

Reduce size of an image when it is fusioned with other using ImageMagick from terminal in Linux

I am having these two pictures, labeled 1.png and 2.png, respectively:
When I run this code from ImageMagick in my Linux terminal:
composite -blend 75 -gravity West 2.png 1.png prueba.png
I get this result:
However, I would like the logo (2.png) to be half or one-third size reduced and placed in the bottom left corner. How can I do it from the terminal?
You can do that in ImageMagick by using the convert syntax. It is more flexible than the composite syntax.
convert 1.png \( 2.png -resize 50% \) -gravity southwest -define compose:args=75 -compose blend -composite 1_2.png
If you want to move it further left, you can simply chop off some of the left side before compositing using ImageMagick.
convert 1.png \( 2.png -resize 50% -gravity west -chop 20x0 \) -gravity southwest -define compose:args=75 -compose blend -composite 1_2_b.png
Alternately, you can simply trim the excess empty space around the 2.png before blending.
convert 1.png \( 2.png -resize 50% -trim +repage \) -gravity southwest -define compose:args=75 -compose blend -composite 1_2_c.png

imagemagick: -gravity SouthEast -geometry offset did not work

when replacing part of an image,
from
to
I replaced two parts of the image.
My solution is
with convert 1.png \( 1.png -crop 200x200+300+800 +repage \) -gravity SouthWest -geometry +0+0 -compose Copy -composite 2.png,
I got
with convert 2.png \( 2.png -crop 350x450+1252+200 +repage \) -gravity SouthEast -geometry -350-450 -compose Copy -composite 4.png
I got the same image, what is wrong with me?
with convert 2.png \( 2.png -crop 350x450+1252+200 +repage \) -gravity Center -geometry +626+349 -compose Copy -composite 3.png, got it .
not very familiar with the mechanism
Using ImageMagick, in place of:
convert 2.png ( 2.png -crop 350x450+1252+200 +repage ) -gravity
SouthEast -geometry 0X0-350-450 -compose Copy -composite 4.png
Use:
convert 2.png \( +clone -crop 350x450+1252+200 +repage \) -gravity SouthEast -geometry -350-450 -compose over -composite 4.png
Or:
convert 2.png \( +clone -crop 350x450+1252+200 +repage \) -gravity center -geometry -350-450 -compose Copy -composite 4.png
That should make a difference. Do not add WxH (0x0) to your -geometry as that may try to make a W=0 and H=0 image, which will either fail or not show. See https://imagemagick.org/Usage/layers/#convert
The +clone, just copies the previous image in the command line, which is 2.png. Repeating 2.png is OK, but will be slower as it has to read the 2.png image a second time.
Note, it is best not use images to show code. Just paste the code into the form and use the {} button to make it show in gray background.
convert 2.png \( +clone -crop 350x450+1252+200 +repage \) -gravity SouthEast -geometry +0+0 -compose over -composite 99.png
experimented and figured out

Can you help me get rid of an intermediary output in my imagemagick convert commands?

I have the following convert command in bash.
convert "$WALLPAPER1" -resize "${H[0]}"x"${V[0]}"^ -gravity center -crop "${H[0]}"x"${V[0]}"+0+0 "$WALLPAPERS/.temp1.jpg"
convert "$WALLPAPER2" -resize "${H[1]}"x"${V[1]}"^ -gravity center -crop "${H[1]}"x"${V[1]}"+0+0 jpg:- |
convert "$WALLPAPERS/.temp1.jpg" - +append "$WALLPAPERS/.temp.jpg"
Is there a way I can get rid of the "$WALLPAPERS/.temp1.jpg" intermediary? So is there a way to carry over the output of the first convert over to the input of the third convert?
convert "$WALLPAPER1" -resize "${H[0]}"x"${V[0]}"^ -gravity center -crop "${H[0]}"x"${V[0]}"+0+0 -write mpr:temp1 +delete \
"$WALLPAPER2" -resize "${H[1]}"x"${V[1]}"^ -gravity center -crop "${H[1]}"x"${V[1]}"+0+0 -write mpr:temp2 +delete \
-gravity north mpr:temp1 mpr:temp2 +append "$WALLPAPERS/.temp.jpg"
There was no need to have 3 convert calls. Instead of writing to file you can write to memory-program-register (mpr) and then recall later. The +delete deletes the original image.

Resources