
In other words, script must select pixel, similar to background of the text, and then translate it to Fred's Magic Wand. Then Magic Wand will colorize background in white color.
ghostmansd wrote:That effects picture in the corner also. In a tiff files, at least.
Code: Select all
from PIL import Image
im = Image.open('D:/pavian.png', 'r')
w, h = im.size
a = [[0]*w for i in range(h)]
b = [[0]*w for i in range(h)]
for i in range(h):
for j in range(w):
a[i][j] = im.getpixel((j, i))
d = [[-1,-1], [-1,0], [-1,1], [0,1], [1,1], [1,0], [1,-1], [0,-1]]
c = 40 # // порог разницы в интенсивностях двух соседей
s = 20 # // сторона квадратиков
def foo(p, q):
cnt = 0
for i in range(p + 1, p + s - 1):
for j in range(q + 1, q + s - 1):
for k in range(8):
if abs(a[i][j] - a[i + d[k][0]][j + d[k][1]] > c):
cnt += 1
return cnt
z = 0
for i in range(0, h - s, s):
for j in range(0, w - s, s):
p = foo(i, j)
for k in range(s):
for l in range(s):
b[i + k][j + l] = p
z = max(z, p)
for i in range(h):
for j in range(w):
if b[i][j] > z / 2:
v = 255
else:
v = 0
im.putpixel((j, i), v)
f_out = open('D:/pavian_out.png', 'wb')
im.save(f_out)
f_out.close()
Anthony, great thanks to you! That's amazingly useful tool! Now all what I've to do:anthony wrote:As Fred said it all comes down to...
define "where combination of 1 and 0 looks like text;"
The only thing I can think of is use combinations of morphology and segmentation so as to locate rows of small segments, which generally makes up characters and words, and thus means 'text'.
For example in your thumbnail image above doing a morphology search for long thin horizontal lines can mean 'text'.
On the other hand: define "what is a image" in the page image may in fact be a lot easier!
Again you would use morphology and segmentation to learn about what makes up the page, but in this case a 'image' would be any segment larger than say 2 or 3 typical text rows.
I have done this using Fred's "Multi Crop" to locate the images on a page. (My own need was for the images not text).
My own modified version is in..
http://www.imagemagick.org/Usage/scripts/multi_crop
This does a sparse grid search for any large segments (defined as NOT the background color). If the segment is too small it gets ignored (small character). A small change will let it output a list of rectangles that it thinks are areas of 'non-text'.
WARNING: whatever you do you will need to do a fast 'preview' to check that it worked fine. In my own use I came across pages with overlapping images, extra lines and boxes, or slight image rotations, or text inserts in larger images, that needed some extra work on those specific pages to deal with. But in general it worked and saved me a LOT of work in manually processing each and every page (about a thousand pages).
Code: Select all
/tmp/image-1.png,20,71,95,100
/tmp/image-2.png,300,150,374,200
/tmp/image-3.png,700,503,729,625