Remove irregular rounded corners from scanned images

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
Posts: 1
Joined: 2017-10-05T02:58:17-07:00
Authentication code: 1151

Remove irregular rounded corners from scanned images

Post by michelgalibert » 2017-10-05T03:05:13-07:00


I'm quite new to ImageMagick. I have scanned thousands of old slides. On some of them, I have rounded corners I'd like to automatically remove.
The position of the corners is not always the same so applying exactly the same inner cropping would not work.

I would like to detect automatically and crop only the red area, while respecting the ratio of the picture.

Here are some original pictures if needed for testing:

Do you know how to do this?

Many thanks for your help,


Posts: 10729
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove irregular rounded corners from scanned images

Post by snibgo » 2017-10-05T03:57:20-07:00

Please say what version of IM you use, on what platform (eg Windows, Unix, etc) and your preferred script language (Windows BAT, bash, etc) if any.

The first image has a "CloudApp" logo bottom-left, obscuring that corner. Has that been added by your hoster? If so, I suggest you upload them to somewhere that doesn't alter your images. For example, put them in zips that you upload to or somewhere.

Doing the crop is easy, with "-crop" or "-shave". Detecting whether it is needed, and how much, is more difficult. The photographed subject may be very dark in the corners, leading to false readings.

A possible approach: from each corner, examine the line at 45 degrees, towards the centre of the image. As we examine pixels from the corner inwards, if there is a jump from almost-black to something lighter, that marks a point for a crop. If all four corners have a jump at a similar distance, use the inner-most as a crop for all four.

(This could be done by rotating the image by 45 degrees, and taking horizontal or vertical crops.)

If two adjacent corners have a jump but the other two don't, do you care what happens? We could assume the other two have a jump in the same places.
snibgo's IM pages:

User avatar
Posts: 23904
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove irregular rounded corners from scanned images

Post by fmw42 » 2017-10-13T11:45:53-07:00

I have just add a new unix bash shell script, trim corners, to my collection that should do what you want above.

It uses a similar approach as suggested by snibgo to search along the diagonals of the image.

Post Reply