如何从 Python 中的图像中删除空格?

问题描述

我有一组从代码输出的图像,我希望能够删除图像中所有多余的空白,以便将图像缩小为仅图像中的文本。 这是相关代码

from PIL import Image,ImageFont,ImageDraw,ImageChops
from docx import Document
import textwrap
import re

doc = Document('Patents.docx')
docText = ''.join(paragraph.text for paragraph in doc.paragraphs)


def trim(im,color):
    bg = Image.new(im.mode,im.size,color)
    diff = ImageChops.difference(im,bg)
    diff = ImageChops.add(diff,diff)
    bBox = diff.getbBox()
    if bBox:
        return im.crop(bBox)


for match in find_matches(text=docText,keywords=("responsive","detecting","providing")):
    W,H = 300,300
    body = Image.new('RGB',(W,H),(255,255,255))
    border = Image.new('RGB',(W + 4,H + 4),(0,0))
    border.save('border.png')
    body.save('body.png')
    patent = Image.open('border.png')
    patent.paste(body,(2,2))
    draw = ImageDraw.Draw(patent)
    font = ImageFont.load_default()

    current_h,pad = 100,20

    for key in textwrap.wrap(match,width=45):
        line = key.encode('utf-8')
        # (width,height) = font.getsize(line)
        # patent.resize((width,height),resample=0,Box=None)
        w,h = draw.textsize(line,font=font)
        draw.text(((W - w) / 2,current_h),line,0),font=font)
        current_h += h + pad
    for count,matches in enumerate(match):
        patent.save(f'{match}.png')
        patentCrop = trim(patent,255)
        patentCrop.save(f'{match}_new.png')

这是我构建的代码的 4 个输出中的 2 个(每个框都是它自己的输出):

enter image description here

enter image description here

我想保留边框,但显然我总是不能使用边框然后裁剪图像然后添加边框,但无论如何,我需要帮助删除空格。如我的代码所示,我使用了一个修剪函数,但它似乎无论出于何种原因都不起作用。如果有任何解决方案,无论是修复我的功能还是完全不同的方法,我都非常感谢您的帮助。以下是我想要完成的,当然,每个框都是它自己的输出

Sample Out

解决方法

我认为这就是您想要的 - 一种双垫环绕:

#!/usr/bin/env python3

from PIL import Image,ImageDraw,ImageOps

# Open input image
im = Image.open('zHZB9.png')

# Get rid of existing black border by flood-filling with white from top-left corner
ImageDraw.floodfill(im,xy=(0,0),value=(255,255,255),thresh=10)

# Get bounding box of text and trim to it
bbox = ImageOps.invert(im).getbbox()
trimmed = im.crop(bbox)

# Add new white border,then new black,then new white border
res = ImageOps.expand(trimmed,border=10,fill=(255,255))
res = ImageOps.expand(res,border=5,fill=(0,0))
res = ImageOps.expand(res,255))
res.save('result.png')

enter image description here