Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中删除图像中的空白?_Python_Python Imaging Library_Image Resizing - Fatal编程技术网

如何在Python中删除图像中的空白?

如何在Python中删除图像中的空白?,python,python-imaging-library,image-resizing,Python,Python Imaging Library,Image Resizing,我有一组从代码输出的图像,我希望能够删除图像中所有多余的空白,以便将图像缩小为图像中的文本。 这是相关代码: 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.p

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

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, 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, 0, 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个(每个框都是自己的输出):

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


我想这正是您想要的-一种双床垫环绕:

#!/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,255))
res = ImageOps.expand(res, border=5, fill=(0,0,0))
res = ImageOps.expand(res, border=5, fill=(255,255,255))
res.save('result.png')

我不确定我是否看到了问题。您可以调用
bbox()
来获取边界框。然后调用
expand()
添加白色边框,然后添加黑色边框和进一步的白色边框。我知道如何添加边框,我正在尝试删除空白以便我的输出与我显示的示例输出匹配。就是这样,非常感谢。我最终没有使用泛洪填充,因为我自己在代码中添加了边框,所以我只是删除了创建边框的代码段。谢谢!