# Automatically cropping images

**URL:** <https://discourse.julialang.org/t/automatically-cropping-images/29235>\
**Category:** Visualization\
**Created:** [September 27, 2019, 1:45pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235 "2019-09-27T13:45:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![merckxiaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merckxiaan/32/4697_2.png) [@merckxiaan](https://discourse.julialang.org/u/merckxiaan)\
**Post date:** [September 27, 2019, 1:45pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/1 "2019-09-27T13:45:43Z")

</div>

Hello,

I’ve got a directory filled with a couple of hundred images, similar to something like this: ![found on the internet.](https://global.discourse-cdn.com/julialang/original/3X/e/a/ead0f96c7d82d2ac4ddc4d2aee96b78a5cac8452.png)  
The problem is a lot of these images aren’t centered or don’t take up a lot of the space (e.g. a lot of excess whitespace) so I’d like to crop them.  
I tried to crop these images based at row r and column c where `sum(img)[r, :]` and `sum(img[:, c])` exceed a certain threshold. This works in some cases but fails when the edge of the image has a line of black pixels (the images are scanned so a lot of them have this.)

When the image is a thin line drawing this heuristic fails completely.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/aebf05d9160ddd988d40497362adebd23662fdd9.jpeg) ![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/af2050f92dba718fb3b071d4ed007cb1c147e053.png)  
_Examples of hard cases due to thin lines_

I’m looking for something along the lines of a filter that is invariant to thin lines, black paper edges…  
I’ve already tried using `felzenszwalb` but that also recognizes the long thin lines along the edge as a blob.  
Any help would be appreciated!

Thanks  
Jules

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 27, 2019, 2:25pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/2 "2019-09-27T14:25:17Z")

</div>

Are you familiar with this collection of scripts?

> **[Fred's ImageMagick Scripts: MULTICROP](http://www.fmwconcepts.com/imagemagick/multicrop/index.php)**
>
> Fred's ImageMagick Scripts - multicrop - crops and unrotates multiple images from a scanned image.

I’ve used it to solve similar problems before

---

<div class="post-metadata">

**Author:** ![merckxiaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merckxiaan/32/4697_2.png) [@merckxiaan](https://discourse.julialang.org/u/merckxiaan)\
**Post date:** [September 27, 2019, 2:46pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/3 "2019-09-27T14:46:06Z")

</div>

Wow, that’s definitely something I was not familiar with.  
Thanks a lot!  
I’ll surely have a look through this!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 27, 2019, 3:07pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/4 "2019-09-27T15:07:18Z")

</div>

Could you not just define a small margin where you ignore content, and the use the simple cropping procedure on the rest?

---

<div class="post-metadata">

**Author:** ![merckxiaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merckxiaan/32/4697_2.png) [@merckxiaan](https://discourse.julialang.org/u/merckxiaan)\
**Post date:** [September 27, 2019, 3:12pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/5 "2019-09-27T15:12:51Z")

</div>

That’s a solution for most of the images but some (perhaps too few to spend time on, but that’s another problem…) have an object that touches the edge and I wouldn’t want anything to be cut off.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 27, 2019, 3:40pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/6 "2019-09-27T15:40:41Z")

</div>

But for what I have in mind, if the object is larger than the margin, nothing would be cut.

---

<div class="post-metadata">

**Author:** ![merckxiaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merckxiaan/32/4697_2.png) [@merckxiaan](https://discourse.julialang.org/u/merckxiaan)\
**Post date:** [September 27, 2019, 3:45pm UTC](https://discourse.julialang.org/t/automatically-cropping-images/29235/7 "2019-09-27T15:45:58Z")

</div>

Right, that’s something I tried and it did indeed work quite wel, however it didn’t get rid of the problem with drawings with thin lines.  
The cropping also disregards small protrusions since these do not exceed the threshold.
