英文:
How to import a PostScript file into PIL with a transparent background?
问题
使用Pillow 9.5.0,Python 3.11.4,Tk 8.6和Windows 11(版本22H2)
在我正在开发的程序中,我有一个tkinter画布,它保存一个后置文件,然后导入到PIL图像中并显示出来。问题是它总是有一个白色背景,即使我更改了画布的背景颜色。
这是我的示例摘录:
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
导出的文件:
链接到文件
英文:
Using Pillow 9.5.0, Python 3.11.4, Tk 8.6, and Windows 11 (version 22H2)
On the program i'm working on, I have a tkinter canvas that saves a postscript file, which is then imported into a PIL Image and shown. The problem is that it always has a white background, even when I changed the canvas background color
Here's an excerpt of my example
self.canvas.update()
self.canvas.postscript(file='temp_{insert_garbage_here}.eps')
img = Image.open('temp_{insert_garbage_here}.eps')
img.show()
Is there any way to fix this, so that the background is transparent?
Btw, I tried using
mask = Image.new('L', img.size, color=255)
img.putalpha(mask)
but that didn't change anything
And this is the file the postscript
exported:
https://www.dropbox.com/scl/fi/fz0xt6is5aicgnqvkcskd/temp_-insert_garbage_here.eps?dl=0&rlkey=yd2pjb61t1rmydm9ky704zcfl
答案1
得分: 3
打开图像后,您可以调用load()
方法并使用transparency=True
参数来告诉ghostscript
以透明背景加载EPS(Encapsulated PostScript)图像:
from PIL import Image
im = Image.open('a.eps')
im.load(transparency=True)
# 检查现在是否具有RGBA图像
print(im.mode) # 打印 "RGBA"
英文:
After opening the image, you can call the load()
method with transparency=True
to tell ghostscript
to load the EPS (Encapsulated PostScript) image with a transparent background:
from PIL import Image
im = Image.open('a.eps')
im.load(transparency=True)
# Check we now have RGBA inage
print(im.mode) # prints "RGBA"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论