我有一组从代码输出的图像,我希望能够删除图像中所有多余的空白,以便将图像减少为仅图像中的文本。这是相关代码:
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, …Run Code Online (Sandbox Code Playgroud)