使用 Pillow 9.5.0、Python 3.11.4、Tk 8.6 和 Windows 11(版本 22H2)
在我正在开发的程序中,我有一个 tkinter Canvas ,用于保存 postscript 文件,然后将其导入到 PIL 图像中并显示。问题是它始终具有白色背景,即使我更改了 Canvas 背景颜色
这是我的示例的摘录
self.canvas.update()
self.canvas.postscript(file='temp_{insert_garbage_here}.eps')
img = Image.open('temp_{insert_garbage_here}.eps')
img.show()
有什么办法可以解决这个问题,使背景透明吗?
顺便说一句,我尝试使用
mask = Image.new('L', img.size, color=255)
img.putalpha(mask)
但这并没有改变任何事情
这是 postscript
导出的文件:
https://www.dropbox.com/scl/fi/fz0xt6is5aicgnqvkcskd/temp_-insert_garbage_here.eps?dl=0&rlkey=yd2pjb61t1rmydm9ky704zcfl
最佳答案
打开图像后,可以通过 transparency=True
调用 load()
方法来告诉 ghostscript
加载 EPS(封装的PostScript)具有透明背景的图像:
from PIL import Image
im = Image.open('a.eps')
im.load(transparency=True)
# Check we now have RGBA inage
print(im.mode) # prints "RGBA"
关于python - 如何将PostScript文件导入到PIL中并具有透明背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76576074/