【发布时间】:2020-01-22 17:18:53
【问题描述】:
我有这张照片 Text in an image
我想检测该文本位置,并裁剪仅聚焦该文本的图像。
这是我的代码:
from PIL import Image
# Opens a image in RGB mode
im = Image.open(r"image.jpg")
# Size of the image in pixels (size of orginal image)
# (This is not mandatory)
width, height = im.size
print(im.size)
# Setting the points for cropped image
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4
# Cropped image of above dimension
# (It will not change orginal image)
im1 = im.crop((left, top, right, bottom))
# Shows the image in image viewer
im1.save("new.jpg")
此代码工作正常,但图像中文本的位置不是静态的。 我希望代码自动检测文本的位置然后裁剪它。
【问题讨论】:
-
到目前为止,您尝试了哪些方法来解决实际问题?
-
我尝试将图像拆分为 (height/30) 部分(注意:30 表示可以读取图像中数字的框的高度)而不是进入“for”循环并全部读取,如果脚本找到数字它会打破循环。但是 Mark Setchell,给了我有史以来最好的解决方案,请阅读下面的正确答案。
标签: python python-3.x image-processing python-imaging-library crop