Use Python / PIL or similar to reducing spaces

Any ideas on how to use Python with a PIL module for compression, select all? I know this can be achieved with gimp. I try to pack my application as little as possible; installing GIMP is not an option for the EU.

Let's say you have 2 images, one is 400x500, the other is 200x100. Both are white with a 100x100 text block somewhere inside the borders of each image. What I'm trying to do automatically breaks the spaces around this text, loads this 100x100 image text block into a variable for further text extraction.

Obviously, this is not so simple, so just starting the selection of text on the whole image will not work! I just wanted to ask about the main process. There is not much available on this topic in Google. If this is allowed, perhaps it will also help someone else ...

Thanks for reading!

+4
source share
3 answers

If you put the image in a numpy array, just find the edges that you can use PIL to crop. Here, I assume that the space is the color (255,255,255) , you can adapt to your needs:

 from PIL import Image import numpy as np im = Image.open("test.png") pix = np.asarray(im) pix = pix[:,:,0:3] # Drop the alpha channel idx = np.where(pix-255)[0:2] # Drop the color when finding edges box = map(min,idx)[::-1] + map(max,idx)[::-1] region = im.crop(box) region_pix = np.asarray(region) 

To show what the results look like, I left the axis labels so you can see the size of the box :

 from pylab import * subplot(121) imshow(pix) subplot(122) imshow(region_pix) show() 

enter image description here

+8
source

The general algorithm n would be to find the color of the upper left pixel, and then do a spiral scan inward until you find a pixel of that color. This will define one edge of your bounding box. Continue scanning until you hit one more from each edge.

+1
source

http://blog.damiles.com/2008/11/basic-ocr-in-opencv/

may I help. You can use the simple bounding box method described in this tutorial or the @Tyler Eaves spiral sentence, which works the same way.

0
source

Source: https://habr.com/ru/post/1397823/


All Articles