如何在PIL中导入一个带有透明背景的PostScript文件?

huangapple go评论70阅读模式
英文:

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"

如何在PIL中导入一个带有透明背景的PostScript文件?

英文:

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"

如何在PIL中导入一个带有透明背景的PostScript文件?

huangapple
  • 本文由 发表于 2023年6月29日 03:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定