我有这张图 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")
这段代码工作正常,但图像中文本的位置不是静态的。 我希望代码自动检测文本的位置然后裁剪它。
最佳答案
您可以使用getbbox()
来获取边界框:
image=Image.open('text.jpg')
x1,y1,x2,y2=image.getbbox()
print(x1,y1,x2,y2)
输出
16 192 208 216
关于python - 检测图像中的文本位置并在 Python 中裁剪它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58048848/