1

I am trying to expedite the process of cropping a bunch of images using fu scripts. All the images will get cropped identically. I need to select a circle that is centered on the image and then crop the image to that circle. I would prefer that the extra areas around the circle (the difference between a circle ad a square) are transparent. I would also prefer the the image it size be cropped to just what is selected.

The second half of my question is am I able to run such an operation from commandline? I would ideally like to run this process on a directory of images and crop them all at once.

(I am new to fu scripting and not entirely sure how all this works. If anyone has a different approach then this to solve the same repetition, I would appreciate that as well.)

wesleywmd
  • 605
  • 1
  • 6
  • 20
  • It's doable with Gimp, but likely much easier with ImageMagick. Add that to your tags to attract the attention of the right people. – xenoid Nov 04 '17 at 18:34
  • Added. Thank you for the direction. – wesleywmd Nov 04 '17 at 19:14
  • Can you post an example image, so we can see what you are trying to do? In Imagemagick you can process a whole folder of images using the mogrify command. – fmw42 Nov 04 '17 at 19:42
  • I am not sure if I am allowed to post the images from a game. They are not mine to post. Its basically a map with a scanned heat signature of an area. That area is a circle on the map. I want to cut the rest of the map off so I only have the heat signatures. You can assume that the circle I want to keep is perfectly centered in the original image. – wesleywmd Nov 04 '17 at 19:47
  • Also, I am a php developer and now I am looking at the Imagick library. If anyone has any samples of using this library for this would be appreciated as well. – wesleywmd Nov 04 '17 at 19:49
  • 1
    Sometimes it is worth searching Stackoverflow: https://stackoverflow.com/questions/13795535/circularize-an-image-with-imagick – Bonzo Nov 04 '17 at 20:32
  • Does the image have a delineated circle marking the area you want to keep and you want to automatically crop to that circle? Or do you just want to be able to crop a circle region of an image with an arbitrary radius? – fmw42 Nov 04 '17 at 21:28

4 Answers4

1

I think this is close, if I start with:

enter image description here

#!/bin/bash
# Get x,y coordinates of centre
cx=$(convert bean.jpg -format "%[fx:int(w/2)]" info:)
cy=$(convert bean.jpg -format "%[fx:int(h/2)]" info:)
# Find point on circle circumeference
pt="0,$cy"
[ $cx -gt $cy ] && pt="$cx,0"

# Now create a black and white circle of the right size as transparency
convert bean.jpg                                                                               \
     \( +clone -fill black -colorize 100% -fill white -draw "circle $cx,$cy $pt" -alpha off \) \
     -compose copyopacity -composite                                                           \
     -trim +repage result.png

enter image description here


If you have ImageMagick v7, and like looking at mad things, you can do all that in a one-liner:

magick bean.jpg \
     \( +clone -fill black -colorize 100% -fill white -draw "circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:w>h?int(w/2):0],%[fx:w>h?0:int(h/2)]" -alpha off \) \
     -compose copyopacity -composite \
     -trim +repage result.png
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • This looks good. One question, how would I be able to specify to radius? – wesleywmd Nov 04 '17 at 22:03
  • It automatically selects a radius according to the smaller dimension. If you wanted a radius of 10, you would set `pt=“$cx,$((cy+10))”` though I cannot test that at the moment. – Mark Setchell Nov 04 '17 at 22:21
0

Modifying Mark Setchell's nice code a little, in ImageMagick 7, you can get it to find the center automatically, translate to there and specify a radius=200 in one command line as follows:

magick bean.jpg \
\( -clone 0 -fill black -colorize 100 -fill white \
-draw "translate %[fx:w/2],%[fx:h/2] circle 0,0 0,200" \) \
-alpha off -compose copy_opacity -composite result.png

In ImageMagick 6, you would need another command to get the center:

declare `convert bean.jpg -format "CX=%[fx:w/2]\nCY=%[fx:h/2]\n" info:`

convert bean.jpg \
\( -clone 0 -fill black -colorize 100 -fill white \
-draw "translate $CX,$CY circle 0,0 0,200" \) \
-alpha off -compose copy_opacity -composite result.png

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

An ImageMagick command like this should take any image as input, then output the largest circle possible from the center. The circle will be in a square canvas. The background outside the circle will be transparent. This should work with any IM version 6.7.7 or newer.

convert input.png -gravity center -background black -bordercolor black \
   \( -clone 0 -fill lime -colorize 100 -rotate 90 \) +swap -composite -trim \
   \( -clone 0 -fill white -colorize 100 -crop 2x+0+0 -shave 0x2 -border 0x1 \
   +repage -distort arc 360 \) -compose copyopacity -composite output.png

Edited to add: To crop out a particular sized circle from the center of the image, simply replace that entire second line with "-extent NxN \", where N is the size of your desired crop.

GeeMack
  • 4,486
  • 1
  • 7
  • 10
0

In ImageMagick 7, you can center crop to a circle of diameter equal to the minimum of Width or Height as follows:

magick bean.jpg \
\( -clone 0 -fill black -colorize 100 -fill white \
-draw "translate %[fx:w/2],%[fx:h/2] circle 0,0 0,%[fx:min(w/2,h/2)]" \) \
-alpha off -compose copy_opacity -composite -trim result.png

enter image description here

In IM 6, you can do it by:

declare `convert bean.jpg -format "CX=%[fx:w/2]\nCY=%[fx:h/2]\nRAD=%[fx:min(w/2,h/2)]\n" info:`

convert bean.jpg \
\( -clone 0 -fill black -colorize 100 -fill white \
-draw "translate $CX,$CY circle 0,0 0,$RAD" \) \
-alpha off -compose copy_opacity -composite -trim result3.png

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80