使用Python/PIL或类似方法缩小空白 [英] Use Python / PIL or similar to shrink whitespace

查看:140
本文介绍了使用Python/PIL或类似方法缩小空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么想法如何将Python与PIL模块一起使用以缩小全选?我知道这可以用Gimp来实现.我正在尝试将应用程序打包得尽可能小,对于欧盟来说,GIMP安装不是一个选择.

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.

假设您有2张图片,一张是400x500,另一张是200x100.它们都是白色的,在每个图像的边界内都有100x100的文本块.我想做的是自动去除文本周围的空格,将100x100图像文本块加载到变量中以进一步提取文本.

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.

这显然不是那么简单,所以仅对整个图像进行文本提取是行不通的!我只想查询基本过程. Google上没有太多有关此主题的信息.如果得到解决,也许也可以帮助其他人...

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

感谢阅读!

推荐答案

如果将图像放入numpy数组,则很容易找到可以使用PIL进行裁剪的边缘.在这里,我假设空白是颜色(255,255,255),您可以根据需要进行调整:

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)

为显示结果,我保留了轴标签,以便您可以看到box区域的大小:

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()

这篇关于使用Python/PIL或类似方法缩小空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆