gpt4 book ai didi

python - 导出具有透明背景的 matplotlib

转载 作者:行者123 更新时间:2023-12-04 07:19:03 24 4
gpt4 key购买 nike

我正在尝试导出具有透明背景的 matplotlib 图,因为它是一个圆圈,我需要粘贴到另一个 Canvas 上(没有角)。
我已按照此处列出的示例进行操作:
How to export plots from matplotlib with transparent background?
示例代码:

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

path = '...'

SomeCanvas1 = Image.new('RGB', (750, 750), '#36454F')

fig, ax = plt.subplots(figsize=(6, 6))
wedgeprops = {'width':0.3, 'edgecolor':'white', 'linewidth':2}
ax.pie([1-0.33,0.33], wedgeprops=wedgeprops, startangle=90, colors=['#BABABA', '#0087AE'])
plt.text(0, 0, '33%', ha='center', va='center', fontsize=42)
fig.savefig(path+'donut1.png', transparent=True)

imgDonut = Image.open(path+'donut1.png')
w,h = imgDonut.size

SomeCanvas1.paste(imgDonut, (int(0.5*(750-w)),int(0.5*(750-h))))
SomeCanvas1.save(path+'test1.png')
以及此处列出的示例: How to set opacity of background colour of graph with Matplotlib
示例代码:
SomeCanvas2 = Image.new('RGB', (750, 750), '#36454F')

fig, ax = plt.subplots(figsize=(6, 6))
fig.patch.set_facecolor('None')
fig.patch.set_alpha(0)
wedgeprops = {'width':0.3, 'edgecolor':'white', 'linewidth':2}
ax.pie([1-0.33,0.33], wedgeprops=wedgeprops, startangle=90, colors=['#BABABA', '#0087AE'])
plt.text(0, 0, '33%', ha='center', va='center', fontsize=42)
fig.savefig(path+'donut2.png', facecolor=fig.get_facecolor(), edgecolor='none')

imgDonut = Image.open(path+'donut2.png')
w,h = imgDonut.size

SomeCanvas2.paste(imgDonut, (int(0.5*(750-w)),int(0.5*(750-h))))
SomeCanvas2.save(path+'test2.png')
以及此处的示例: Export plot in .png with transparent background
但这对我不起作用。当我粘贴到 Canvas 上时,我最终得到:
enter image description here
我需要它紧紧地围绕着 donut ,没有方角。
我的代码出了什么问题?
编辑:
我在 Windows 10 上并使用 ATOM IDE。我不确定这是否会有所不同...

最佳答案

这对我有用:

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

path = '...'

SomeCanvas1 = Image.new('RGB', (750, 750), '#36454F')

fig, ax = plt.subplots(figsize=(6, 6))
wedgeprops = {'width':0.3, 'edgecolor':'white', 'linewidth':2}
ax.pie([1-0.33,0.33], wedgeprops=wedgeprops, startangle=90, colors=['#BABABA', '#0087AE'])
plt.text(0, 0, '33%', ha='center', va='center', fontsize=42)
fig.savefig(path+'donut1.png', transparent=True)

imgDonut = Image.open(path+'donut1.png')
w,h = imgDonut.size

SomeCanvas1.paste(imgDonut, (int(0.5*(750-w)),int(0.5*(750-h))))
SomeCanvas1.save(path+'test1.png')
fig.patch.set_alpha(0)
输出是(我有黑色背景-> 这是 JupyterLab 的屏幕截图):
enter image description here
这是你的文件 ...donut1.png (透明背景,我的查看器有白色背景——>这是实际的图像文件):
enter image description here
- - 编辑 - -
设法让它变得透明!
import matplotlib.pyplot as plt
from PIL import Image, ImageFont, ImageDraw

path = '...'

SomeCanvas1 = Image.new('RGB', (750, 750), '#36454F')

fig, ax = plt.subplots(figsize=(6, 6))
wedgeprops = {'width':0.3, 'edgecolor':'white', 'linewidth':2}
ax.pie([1-0.33,0.33], wedgeprops=wedgeprops, startangle=90, colors=['#BABABA', '#0087AE'])
plt.text(0, 0, '33%', ha='center', va='center', fontsize=42)
fig.savefig(path+'donut1.png', transparent=True)
fig.patch.set_alpha(0)

SomeCanvas1.save(path+'test1.png')

foreground = path+'donut1.png'
imgfore = Image.open(foreground, 'r')
background = path+'test1.png'
imgback = Image.open(background, 'r')

merged = Image.new('RGBA', (w,h), (0, 0, 0, 0))
merged.paste(imgback, (0,0))
merged.paste(imgfore, (0,0), mask=imgfore)
merged.save((path+"merged.png"), format="png")
在这种情况下,您将生成 3 个图像文件。这是合并的文件:
enter image description here

关于python - 导出具有透明背景的 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68622483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com