python - 如何将PostScript文件导入到PIL中并具有透明背景?

标签 python tkinter python-imaging-library postscript

使用 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"

enter image description here

关于python - 如何将PostScript文件导入到PIL中并具有透明背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76576074/

相关文章:

python - 用PIL保存后图像颜色发生变化

python - PIL - 将图像保存为 .jpg 不起作用

python - 在python中的图像中插入波斯文字,UnicodeEncodeError: 'latin-1'编解码器无法在位置0-4处编码字符:序数不在范围内(256)

python - 使用正则表达式检查并保存电话号码

python - 在 RTSP 设置后接收 RTP 数据包

python - 将文本复制到剪贴板的平台独立工具

windows-7 - Winsound使Tkinter GUI崩溃

python - 解析 json 中的代码 (Python)

python - 使用函数的单例实现

python - 单选按钮 get 在函数内不起作用