# How can I automatically apply smart cropping to hundreds of coin photos?

I am having terrible difficulty. I routinely have to photograph and then edit hundreds of coin pictures. I then by hand crop out the excess background leaving a small border around the coin. I know this is possible to be automated. I can't seem to be able to get it working. I need to auto crop out the background color to a tolerance and with a border from the center image.

• Why not crop in camera with a zoom for the desired margins on the top and bottom (i.e. make the coins the same size and position in the frame). If you make sure the coin(s) are centered in the frame then a crop script or action should take care of the problem.
– BobT
Commented Jan 9, 2016 at 17:24
• You say that you can't "get it working," but you haven't told us anything about what you've tried to do. Explaining what you've tried already and how that fails will prevent us from suggesting something that you know doesn't work. Commented Jan 10, 2016 at 19:28

This will probably require a bit of scripting or programming. Read up on the Circle Hough Transform. Basically, it detects circles in an image. While the maths are quite complicated, you can probably find a decent library in a language that abstracts away a lot of the complexity.

For instance, checkout the OpenCV (Open Computer Vision) library, which has C, C++, Python, and Java interfaces. As an example usage, here is a tutorial for detecting circles using OpenCV in Python. In that example, the circles vector is a list of 3-tuples, each tuple containing the (x-coordinate, y-coordinate, radius) of the found circle. To find the circle with the largest radius, something like:

largest_circle = max(circles, key=lambda c: c[2])


will find the largest circle in the image (which is presumably your coin), as the same (x, y, r) tuple. After that, you can just increase r by a small amount (to create a border), such that you can crop the image at: (xmin = x − r, xmax = x + r), by (ymin = y − r, ymax = y + r).

Rather than using OpenCV, this example uses the scikit-image image processing library with Python. The image in this example is an image of several coins lined up, showing the found circles in the image.

• In retrospect, perhaps this question should be referred to stackoverflow.
– scottbb
Commented Jan 9, 2016 at 1:58
• I think he is looking more for a already existing software solution that can be configured, not developed/scripted, like one could do with Photoshop Actions. Commented Jan 9, 2016 at 11:49
• @dragos You're probably right. I was hoping the OP would respond with example before/after pics like you requested. If there's a wide variation on magnification, coin placement, etc., in the original image, likely the best results would come from a programmatic or algorithmic solution.
– scottbb
Commented Jan 9, 2016 at 15:32

In Photoshop you can create a automation script that does the same thing over and over again .

Now with that said your pictures should be kind of identical so the script works good . You record once what you normally do and then save the action - After that is the easy part just batch execute the action .

Here is how to record it .

Go to the Window menu and choose Actions. A little palette will pop up with Photoshop actions already predefined. To use any predefined action, just select it and press the play button at the bottom. If you want to make your own, click the menu button in the upper righthand corner and choose New Action. Name your action, click OK, and then you'll automatically be in record mode. Anything you do in Photoshop will now be recorded, so perform any actions you want to automate and then click the stop button at the bottom of the actions palette.

• How will this be of use? Surely an action that involves cropping would either crop every image exactly the same, or pause while you manually crop. I don't see how this could possibly work. Commented Jan 11, 2016 at 18:07
• It seems like this will only work if the coin is in the same part of the frame for each photo. Is that right? Commented Jan 11, 2016 at 19:44
• First it's wrong assumtion to wanna crop . The action should be "Select Color Range (the black)" ---> "Feather (maybe 2 px depending on the resolution)"---> Select Inverse ---> Create new black layer ---> Copy selection in the black layer --- > Save . Of course maybe Color adjustments , Contast and sharpness if neeeded. So then on the other hand all of this is kind of overkill . He said he routinely needs to do it so in the best case he should have some kind of a setup where the camera si mounted , a ring flash .manual exposure , focus and just click chahge the coin click . No need for PS Commented Jan 12, 2016 at 10:17

If you have Photoshop or equivalent, you can make an action to open each jpeg in turn,

*add a new background layer in a neutral color, goto the coin layer, *select the the color of the background that the coin was sitting on when photographed, *edit the selection to make it smaller (using 'refine edge'),
*delete the selection.

[That will leave the coin image sitting with a tiny border of the original background color]

Then you can save as a gif with transparency on and set to the new background color. If you have PS and want to do this, I can do a set of images to capture the train of edits.

• Yes I Have ps6. I cannot find refine edge anywhere. Commented Jan 10, 2016 at 4:49
• I posted two pics, they are after crop pics. Commented Jan 10, 2016 at 5:17
• the pre crop is just more of that background color. Commented Jan 10, 2016 at 5:18
• well this is a toughie. Commented Jan 11, 2016 at 15:23
• well this is a toughie. If the programming solution by scottbb isn't feasible, I suggest you look at a routine way of shooting the coins. If they are all in the same holders, make a small shooting holder and standardize the camera position and distance (perhaps using a shooting stand) so that the image of the coin is always centered on the same spot. Then make a PS action that imports each file and changes canvas size to a square that encloses the coin. Commented Jan 11, 2016 at 15:32