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
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?
and the required image should be like

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
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:

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.
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:

convert img.png -trim +write tmp1.png -trim result.png
Result of first trim (tmp1.png):

Final Result after second trim:

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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With