Crop image into a circle

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
YujianPeng
Posts: 2
Joined: 2016-09-15T23:54:35-07:00
Authentication code: 1151

Crop image into a circle

Post by YujianPeng »

Hi,
I want to crop a png image into two png images like
Image
Image
Image

How to get the tow images in command line?
Any help appreciated, thanks a lot !
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Crop image into a circle

Post by GeeMack »

YujianPeng wrote:I want to crop a png image into two png images like [...] How to get the tow images in command line?
IMPORTANT: To get the best help you should always let us know which version of ImageMagick you're using and which OS you're working on. That can make a big difference in the actual commands you'll use.

One way to get a transparent hole is to make a clone of your input image, change that clone to transparent, draw a white circle on it where you want the hole to be, and composite it on your input image using the "DstOut" method. Here is a command using Windows syntax that should work with recent versions of IM6 (convert) or IM7 (magick)...

Code: Select all

convert 3-sample.png -alpha on -background none ^
   ( +clone -channel A -evaluate multiply 0 +channel -fill white -draw "ellipse 153,128 57,57 0,360" ) ^
   -compose DstOut -composite circle_out.png
By changing just the compose method, the same idea can be used to keep only the piece you take out of the hole. Make a clone of the image, make the clone transparent, draw a circle on the part you want to keep, and composite it on the input image using the "DstIn" method like this...

Code: Select all

convert 3-sample.png -alpha on -background none ^
   ( +clone -channel a -evaluate multiply 0 +channel -fill white -draw "ellipse 153,128 57,57 0,360" ) ^
   -compose DstIn -composite circle_in.png
In the "-draw" operation, you need to specify the location of the center of the circle like I did with "153,128", and the radius of the circle like I did with "57,57".

To do both of these operations in a single command, you'd start with the input image, make the transparent clone, and put the circle where you want it. Composite them twice, once using the compose method "DstOut" and another time using "DstIn". At that point you have all four images in the stack, so you "-delete" the first and second images which are the input and the mask. That leaves your two modified images which will be the output. That command would look something like this...

Code: Select all

convert 3-sample.png -alpha on -background none ^
   ( +clone -channel A -evaluate multiply 0 +channel -fill white -draw "ellipse 153,128 57,57 0,360" ) ^
   ( -clone 0,1 -compose DstOut -composite ) ^
   ( -clone 0,1 -compose DstIn -composite ) ^
   -delete 0,1 circle.png
IM will name the output files with sequential numbers, so you'll have "circle-0.png" and "circle-1.png".

To change these commands to *nix shell syntax you'll need to replace the continued line carets "^" with backslashes "\" and use backslashes to escape the parentheses like "\(" and "\)". You may also have to tweak another thing or two like change the double quote marks to single quotes.
YujianPeng
Posts: 2
Joined: 2016-09-15T23:54:35-07:00
Authentication code: 1151

Re: Crop image into a circle

Post by YujianPeng »

GeeMack, Thanks for your reply. Your commands work very good for me!
Thanks a lot!
Post Reply