6

Any ideas how to use Python with the PIL module to shrink select all? I know this can be achieved with Gimp. I'm trying to package my app as small as possible, a GIMP install is not an option for the EU.

Say you have 2 images, one is 400x500, other is 200x100. They both are white with a 100x100 textblock somewhere within each image's boundaries. What I'm trying to do is automatically strip the whitespace around that text, load that 100x100 image textblock into a variable for further text extraction.

It's obviously not this simple, so just running the text extraction on the whole image won't work! I just wanted to query about the basic process. There is not much available on Google about this topic. If solved, perhaps it could help someone else as well...

Thanks for reading!

user1145643
  • 896
  • 5
  • 15
  • 26

3 Answers3

10

If you put the image into a numpy array, it's simple to find the edges which you can use PIL to crop. Here I'm assuming that the whitespace is the color (255,255,255), you can adjust 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've left the axis labels on so you can see the size of the box region:

from pylab import *

subplot(121)
imshow(pix)
subplot(122)
imshow(region_pix)
show()

enter image description here

Hooked
  • 84,485
  • 43
  • 192
  • 261
  • 1
    @kokosnakoks this answer was written 9 years ago for python 2. Maps need to be made explicit in python 3. Feel free to update the answer! – Hooked Feb 26 '21 at 16:29
  • 2
    In python 3 maps need to be added: list(map(min,idx))[::-1] – vondravl Feb 26 '21 at 19:35
1

The general algorithmn would be to find the color of the top left pixel, and then do a spiral scan inwards until you find a pixel not of that color. That will define one edge of your bounding box. Keep scanning until you hit one more of each edge.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • Brilliant! So in essence, I could probably set a range for that expected pixel color and tweak the threshold... – user1145643 Feb 22 '12 at 14:49
0

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

might be of some help. You can use the simple bounding box method described in that tutorial or @Tyler Eaves spiral suggestion which works equally as well

platinummonkey
  • 808
  • 8
  • 19