3

I am trying to trim parts of the image where a complete row of Image doesn't have anything except white color.

I tried using matplot lib

  1. convert image into matrix and saw if (r,g,b) = (0,0,0) or (1,1,1) and removed entire row in image if every (r,g,b) is of above kind in the row

    matrix looks like [ [ [r,g,b], [r,g,b]....] ],...., [ [r,g,b], [r,g,b]....] ] ]

i achieved my requirement but i am running this for around 500 images and it is taking 30 minutes around. Can i do it in better ways?

actual image

and the required image should be like

required output

Edit-1 : tried with trim method from wand package

with wand_img(filename=path) as i:
# i.trim(color=Color('white'))
# i.trim(color=Color('white'))
i.trim()
i.trim()
i.save(filename='output.png')

but not working for the following type of images

2 Answers 2

3

You could use ImageMagick which is installed on most Linux distros and is available for macOS and Windows.

To trim one image, start a Terminal (or Command Prompt on Windows) and run:

magick input.png -fuzz 20% -trim result.png

That will give you this - though I added a black border so you can make out the extent of it:

enter image description here

If you have lots to do, you can do them in parallel with GNU Parallel like this:

parallel -X magick mogrify -trim ::: *png

I made 1,000 copies of your image and did the whole lot in 4 seconds on a MacBook Pro.

If you don't have GNU Parallel, you can do 1,000 images in 12 seconds like this:

magick mogrify -trim *png

If you want to do it with Python, you could try something like this:

#!/usr/bin/env python3

from PIL  import Image, ImageChops

# Load image and convert to greyscale
im = Image.open('image.png').convert('L') 

# Invert image and find bounding box
bbox = ImageChops.invert(im).getbbox()

# Debug
print(*bbox)

# Crop and save
result = im.crop(bbox)
result.save('result.png')       

It gives the same output as the ImageMagick version. I would suggest you use a threading tool to do lots in parallel for best performance.

The sequential version takes 65 seconds for 1,000 images and the multi-processing version takes 14 seconds for 1,000 images.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Mark, can this be achieved with a python utility, because i need this images in many places, and it's not recommended generally to execute os commands through code.
I added a Python version and some benchmarks - please have another look.
You should be able to use Python Wand, which calls Imagemagick. Look for the trim functionality at docs.wand-py.org/en/0.5.0/wand/image.html
thanks @Mark, i will go through it now and verify with my usecase
@fmw42, even trim is not working for this image imgur.com/s7C1pml
|
2

Using two trims in Imagemagick 6.9.10.25 Q16 Mac OSX Sierra works fine for me. Your image has a black bar on the right hand side. The first trim will remove that. The second trim will remove the remaining excess white. You may need to add some fuzz (tolerance) amount for the trim. But I did not need it.

Input:

enter image description here

convert img.png -trim +write tmp1.png -trim result.png

Result of first trim (tmp1.png):

enter image description here

Final Result after second trim:

enter image description here

ADDITION:

Looking at the docs for Python Wand:

trim(*args, **kwargs)
Remove solid border from image. Uses top left pixel as a guide by default, or you can also specify the color to remove.

Parameters: 
color (Color) – the border color to remove. if it’s omitted top left pixel is used by default
fuzz (numbers.Integral) – Defines how much tolerance is acceptable to consider two colors as the same.


You will need to specify color=black for the first trim, since this version of trim uses the top left corner for trimming. Command line Imagemagick looks at all corners. If that fails, then add some fuzz value.

1 Comment

thanks @fmw42, i made it work with similar workaround with colors yesterday [ in my timeline :) ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.