Python中的图像创建和字体 [英] Image creation and fonts in Python

查看:37
本文介绍了Python中的图像创建和字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PIL 在 python 中创建了一组图像.除此之外,我还实现了 textwrap 以便将文本放在我创建的图像上,但是,它们并不完美.首先,下面是我创建的三个图像示例.

这三个图像具有不同的宽度,但我希望它们都具有相同的宽度,而高度无关紧要,可以比彼此更高或更小;宽度是唯一必须保持一致的东西.除此之外,我还使用了 utf-8 编码来在图像上获取此文本,但我希望字体看起来更像以下内容

上图中还显示了这些盒子的堆叠方式——这就是我想要的最终产品的方式.我想要一张包含这些带边框文本框的图像,而不是三张单独的带边框文本图像.这是我当前输出的代码

 for match in find_matches(text=fullText):ct += 1match_words = match.split(" ")匹配 = "".join(match_words[:-1])打印(匹配)宽, 高 = 300, 300base = Image.new(RGB", (W, H), (255, 255, 255))draw = ImageDraw.Draw(base)font = ImageFont.load_default()current_h, 垫 = 50, 5对于 textwrap.wrap(match, width=50) 中的键:line = key.encode("ascii")w, h = draw.textsize(line, font=font)draw.text(((W - w)/2, current_h), line, (0, 0, 0), font=font)current_h += h + 垫draw.text((W/2, current_h), str(ct).encode("utf-8"), (0, 0, 0), font=font)对于计数,在 enumerate(match) 中匹配:base.save(f"{ct}C.png")bbox = ImageOps.invert(base).getbbox()修剪 = base.crop(bbox)专利 = ImageOps.expand(trim, border=5, fill=(255, 255, 255))专利 = ImageOps.expand(专利,边框=3,填充=(0, 0, 0))专利.保存(f{ct}C.png")p_w, p_h = 专利大小Image.open(result_fpath,r")结果.粘贴(专利)结果.保存(result_fpath)

最后,这必须是一个自动化过程.我认为可以将堆叠的框转换为单个图像是一个 for 循环,它接收创建的图像,然后将它们粘贴到与第一个粘贴图像大小相同的图像中,该图像为每个后续图像适当调整大小带边框的文本框.我将不胜感激.

解决方案

我发现使用 ImageMagick 可以更轻松地完成这类事情,

以下是一次性完成的方法:

magick -background white -gravity center -pointsize 24 -size 400x -bordercolor black \\( 标题:组件检测到已在传输中添加了替换组件\n246C" -bordercolor black -border 5 -bordercolor white -border 5 \) \\( 标题:组件检测到另一个组件已被删除\n246D" -bordercolor black -border 5 -bordercolor white -border 5 \) \\( 标题:以任何方式检测到另一个组件已被替换\n247K" -bordercolor black -border 5 -bordercolor white -border 5 \) \-附加结果.png

当然,您可以更改字体、更改颜色、从文件中读取标题、使用 Unicode、使用不同的空格和/或在 Python 中使用非常相似的代码完成所有操作 - 此处是一个答案的链接,该答案显示了 Pythonwand 中的近似技术.

I have created a set of images in python utilizing PIL. In addition to this, I've implemented textwrap in order to put text onto the images I've created, however, they're not quite perfect. First, below are three examples of images I've created.

These three images have different widths, but I'd like them all to have the same width, whereas height isn't of concern and can be taller or smaller than each other; the width is the only thing that must remain consistent. In addition to this, I've used utf-8 encoding in order to get this text on the images, but I would like the font to look something more like the following

Also shown in the above image is how those boxes are stacked--That is how I'd like to have my final product. Rather than three separate images of bordered text, I'd like to have one single image containing those bordered boxes of text. Here is my current code for what I've output

for match in find_matches(text=fullText):
ct += 1
match_words = match.split(" ")
match = " ".join(match_words[:-1])
print(match)
W, H = 300, 300
base = Image.new("RGB", (W, H), (255, 255, 255))
draw = ImageDraw.Draw(base)
font = ImageFont.load_default()

current_h, pad = 50, 5

for key in textwrap.wrap(match, width=50):
    line = key.encode("ascii")
    w, h = draw.textsize(line, font=font)
    draw.text(((W - w) / 2, current_h), line, (0, 0, 0), font=font)
    current_h += h + pad
draw.text((W / 2, current_h), str(ct).encode("utf-8"), (0, 0, 0), font=font)
for count, matches in enumerate(match):
    base.save(f"{ct}C.png")
    bbox = ImageOps.invert(base).getbbox()
    trim = base.crop(bbox)
    patent = ImageOps.expand(trim, border=5, fill=(255, 255, 255))
    patent = ImageOps.expand(patent, border=3, fill=(0, 0, 0))
    patent.save(f"{ct}C.png")
    p_w, p_h = patent.size
    Image.open(result_fpath, "r")
    result.paste(patent)
    result.save(result_fpath)

Finally, this has to be an automated process. What I was thinking that could be done for the stacked boxes into a single image would be a for-loop that takes in the created images and then pastes them into an image of the same size as the first pasted image which resizes appropriately for each subsequent bordered box of text. I'd appreciate any help on this greatly.

解决方案

I find this sort of thing much easier with ImageMagick, for which there are decent bindings available with wand.

Here's how you can do one image, just at the command-line in Terminal, showing the various parts in different colours so you can see what affects what:

magick -background yellow -gravity center -pointsize 24 -size 400x caption:"Detecting, by the component, that a replacement component has been added in the transport\n246C" -bordercolor magenta -border 10 -bordercolor cyan -border 5 result.png

And here's how you can do a few in one go:

magick -background white -gravity center -pointsize 24 -size 400x -bordercolor black \
 \( caption:"Detecting, by the component, that a replacement component has been added in the transport\n246C" -bordercolor black -border 5 -bordercolor white -border 5 \) \
 \( caption:"Detecting, by the component, that another component has been removed\n246D" -bordercolor black -border 5 -bordercolor white -border 5  \)  \
 \( caption:"Detecting, by any means, that another component has been replaced\n247K" -bordercolor black -border 5 -bordercolor white -border 5 \)  \
 -append result.png

Of course you can change the fonts, change the colours, read the captions from a file, use Unicode, space differently and/or do it all in Python with very similar-looking code - here is a link to an answer showing the approximate technique in wand in Python.

这篇关于Python中的图像创建和字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆