Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine these commands to achieve circular crop in ImageMagick?

Tags:

imagemagick

How can I combine these commands to achieve circular crop in ImageMagick?

So this command works:

convert -size 200x200 xc:none -fill samia.jpg -draw "circle 100,100 100,1" circle_thumb.png

The above will take a picture and make a circular crop out of it BUT the crop will be based on the upper left corner of the picture not the center of the picture.

This command also works for cropping:

convert *.jpg -resize 200x200^ -gravity Center -crop 200x200+0+0 +repage out.png

The above will make a square crop of an image based on the center of the image.

So what I want to do is to combine both commands.

My aim:

A command that takes a picture as input and makes a circular crop of it based on the center of the picture, not based on the upper left corner of the picture.

Anyone with IM skills who can show a dude how to resolve this?

Vesa

Ubuntu 15.10

Update:

I tried Mark Setchell's solution below but got the following error message:

axx@axx-VPCEA3S1E:~/Desktop/circular-crop$ magick samia.png \( +clone -threshold 101% -fill white -draw 'circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:int(w/2)],%[fx:80+int(h/2)]' \) -channel-fx '| gray=>alpha'   circle.png
magick: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/509.
magick: no image to apply a property "%w" @ warning/property.c/GetMagickPropertyLetter/2561.
magick: unknown image property "%w" @ warning/property.c/InterpretImageProperties/3499.
magick: no image to apply a property "%h" @ warning/property.c/GetMagickPropertyLetter/2449.
magick: unknown image property "%h" @ warning/property.c/InterpretImageProperties/3499.
magick: no image to apply a property "%m" @ warning/property.c/GetMagickPropertyLetter/2480.
magick: unknown image property "%m" @ warning/property.c/InterpretImageProperties/3499.
axx@axx-VPCEA3S1E:~/Desktop/circular-crop$ 
like image 868
Vesa Avatar asked Jan 31 '17 13:01

Vesa


2 Answers

This question gets asked a lot.

Given an image larger than circle.

convert -size 300x300 plasma: input.png

input.png

We can draw a shape, convert values to alpha channel, and compose it over the input image.

convert input.png \
        -gravity Center \
        \( -size 200x200 \
           xc:Black \
           -fill White \
           -draw 'circle 100 100 100 1' \
           -alpha Copy \
        \) -compose CopyOpacity -composite \
        -trim output.png

output

Now if your planing to crop many resources, I would highly suggest creating a mask once. The reuse the mask as needed.

convert -size 200x200 xc:Black -fill White -draw 'circle 100 100 100 1' -alpha Copy mask.png

for f in $(ls *.jpg)
do
  convert $f -gravity Center mask.png -compose CopyOpacity -composite -trim ${f}_output.png
done
like image 184
emcconville Avatar answered Sep 29 '22 16:09

emcconville


Not sure about cropping circles, but if you want to make all but a central circle transparent, you can do this...

Start image

enter image description here

Extract a circle with radius 80, using:

magick start.png \( +clone -threshold 101% -fill white -draw 'circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:int(w/2)],%[fx:80+int(h/2)]' \) -channel-fx '| gray=>alpha'   circle.png

enter image description here

like image 36
Mark Setchell Avatar answered Sep 29 '22 16:09

Mark Setchell