Ellipse cropping from GIMP parameters

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
filippogambarota
Posts: 2
Joined: 2019-09-19T02:02:12-07:00
Authentication code: 1152

Ellipse cropping from GIMP parameters

Post by filippogambarota »

Hi,
I'm trying to batch crop images with a particular ellipse region of interest (ROI). Usually, I do it with rectangular ROI so I go to GIMP, I select my region, I copy my x,y,w,h parameters anf then use the command line tool for batch cropping. In GIMP, even the oval shape is defined by x,y,w,h but Imagemagick accept other parameters for drawing an oval shape. Is there any method to use x,y,w,h values for oval cropping?

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Ellipse cropping from GIMP parameters

Post by snibgo »

What version on IM, on what platform?

From x,y,w,h you can calculate x0,y0,rx,ry for an ellipse. In IM v7, you can do this in fx expressions within the draw. For example (Windows BAT syntax):

Code: Select all

magick ^
  in.png ^
  -draw "ellipse %%[fx:%XX%+%WW%/2-0.5],%%[fx:%YY%+%HH%/2-0.5] %%[fx:%WW%/2-0.5],%%[fx:%HH%/2-0.5] 0,360" ^
  out.png
Or you could do the calculations before the IM command, in a script.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Ellipse cropping from GIMP parameters

Post by fmw42 »

If the oval is defined by x,y,w,h, then just do -crop WxH+X+Y +repage. That is assuming that x,y are the upper left corner of the rectangular crop region of the ROI for the oval. This will get the bounding box about the ellipse for the output. If you want the oval with transparent background, then draw an white oval (ellipse) on black background mask as per what snibgo suggested and put the mask into the alpha channel of your image. The optionally trim to the bounding box (ROI) of the oval.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Ellipse cropping from GIMP parameters

Post by GeeMack »

filippogambarota wrote: 2019-09-19T02:07:47-07:00Is there any method to use x,y,w,h values for oval cropping?
The example provided above by snibgo uses "-draw" with "ellipse" to create an ellipse. That's a good simple solution that requires you to define an X and Y center point, and a width and height radius from that point.

As an alternative, you can also use "-draw" with "arc" to create an ellipse. It requires you to start with the X and Y offsets for the top and left of the ellipse rather than using center points, and it uses X and Y offsets for the bottom and right rather than using horizontal and vertical radius dimensions.

Using IM v7 on Windows, this command should produce exactly the same result as snibgo's suggestion above, while using a slightly different formula to calculate the location.

Code: Select all

magick ^
  in.png ^
  -draw "arc %XX%,%YY% %[fx:%XX%+%WW%-1],%[fx:%YY%+%HH%-1] 0,360" ^
  out_arc.png
If you're using IM v6 you'll have to calculate those dimensions in the script before the ImageMagick command.

To use this in a BAT script you'll need to double the percent signs "%%" at the beginning of the FX expressions.
Post Reply