【问题标题】:How to create & pass Python PNG images without saving them to a file如何创建和传递 Python PNG 图像而不将它们保存到文件中
【发布时间】:2022-01-26 02:26:29
【问题描述】:

目标:在不创建磁盘文件的情况下创建用于程序其他部分的图像。

我正在尝试将标题文本添加到使用 matplotlib 创建的 PNG 绘图图像中。可能有更好的方法来做到这一点,但我目前的方法是创建文本的 PNG 图像(带枕头)并将其与绘图图像合并。因此,该任务有 3 个不同的组件。两个创建单独的图像并将它们保存为 PNG 文件,第三个将它们合并。我想通过创建合并代码可以使用但无法确定要使用的图像类型或图像打开方法的图像来消除前两个文件的创建。示例代码如下:

import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont

v = [106.0, 95.0, 125.0, 128.0, 100.0, 121.0, 97.0, 103.0, 104.0, 101.0]
dt = range(len(v))
cap_text = '     Number of tests: 10    Number of units: 8'
size = 12
color = 'white'
fnt = ImageFont.truetype('arial.ttf', size)
    
############################################################################
# Create the plot & save as tmp1.png    
    
plt.plot(dt, v, 'r-', label='Results')
plt.ylabel('ms')
plt.xlabel('Tests')
plt.grid()
plt.savefig('tmp1.png')
plt.gcf().clear()

############################################################################
# Create the caption text PNG image and save as tmp2.png

image = Image.new(mode = "RGB", size = (int(size/2)*len(cap_text),size+50), color = 'white')
d = ImageDraw.Draw(image)
d.text((12,12), cap_text, font=fnt, fill=(0,0,0))
image.save('tmp2.png')

############################################################################
# Merge the two PNG files, one above the other, to create the desired result

imgs = [r'tmp1.png', r'tmp2.png']

total_height = 0
max_width = 0
ix =[]
for img in imgs:
    im = Image.open(img)
    size = im.size
    w = size[0]
    h = size[1]
    total_height += h 
    max_width = max(max_width, w)
    ix.append(im) 

tmp3 = Image.new('RGB', (max_width, total_height), color='white')
current_h = 0
for img in ix:
    tmp3.paste(img, (0, current_h))
    current_h += img.size[1]
    
tmp3.show()
tmp3.save('tmp3.png', quality=100)

生成以下图像 (tmp3.png)。

在提交之前我又尝试了一件事,它似乎有效。使用 BytesIO,我创建了名为 buffer1 和 buffer2 的变量,如绘图和 PIL 图像的“保存”调用中所示;更新的代码如下(只是存在更改的前半部分)。查找缓冲区变量名称。

import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

v = [106.0, 95.0, 125.0, 128.0, 100.0, 121.0, 97.0, 103.0, 104.0, 101.0]
dt = range(len(v))
cap_text = '     Number of tests: 10    Number of units: 8'
size = 12
color = 'white'
fnt = ImageFont.truetype('arial.ttf', size)
    
############################################################################
# Create the plot & save as tmp1.png    
    
plt.plot(dt, v, 'r-', label='Results')
plt.ylabel('ms')
plt.xlabel('Tests')
plt.grid()
buffer1 = BytesIO()
plt.savefig(buffer1, format='png')
plt.gcf().clear()

############################################################################
# Create the caption text PNG image and save as tmp2.png

image = Image.new(mode = "RGB", size = (int(size/2)*len(cap_text),size+50), color = 'white')
d = ImageDraw.Draw(image)
d.text((12,12), cap_text, font=fnt, fill=(0,0,0))
buffer2 = BytesIO()
image.save(buffer2, format = 'png')

############################################################################
# Merge the two PNG files, one above the other, to create the desired result

imgs = [buffer1, buffer2]

【问题讨论】:

    标签: python image python-imaging-library png


    【解决方案1】:

    我找到了提交中提出的问题的答案。它创建组合文件而不创建两个组件文件。不是用plt.savefig('tmp1.png')image.save('tmp2.png') 将这两个图像保存到硬盘上的PNG 文件,而是将它们保存为BytesIO 图像变量,如下所示:

    from io import BytesIO
    .
    .
    # in the plot section:
    buffer1 = BytesIO()
    plt.savefig(buffer1, format='png')
    .
    .
    # in the text-to-image section:
    buffer2 = BytesIO()
    image.save(buffer2, format = 'png')
    .
    .
    # and the buffer names are used in the last section
    .
    .
    imgs = [buffer1, buffer2]
    .
    .
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2016-04-22
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多