使用alpha将PIL图像转换为GTK pixmap
所以我需要拍摄一张我在PIL中制作的图像,并将其转换为一个pixmap,以显示在一个可绘制的图形中 如何从PIL转换为pixmap并保持图像alpha 目前我编写了以下代码:使用alpha将PIL图像转换为GTK pixmap,gtk,python-imaging-library,alpha,pixmap,Gtk,Python Imaging Library,Alpha,Pixmap,所以我需要拍摄一张我在PIL中制作的图像,并将其转换为一个pixmap,以显示在一个可绘制的图形中 如何从PIL转换为pixmap并保持图像alpha 目前我编写了以下代码: def gfx_draw_tem2(self, r, x, y): #im = Image.open("TEM/TEM cropped.png") im = Image.new("RGBA", (r*2,r*2), (255, 255, 255, 255)) draw = ImageDraw
def gfx_draw_tem2(self, r, x, y):
#im = Image.open("TEM/TEM cropped.png")
im = Image.new("RGBA", (r*2,r*2), (255, 255, 255, 255))
draw = ImageDraw.Draw(im)
for i in range(0,r*2):
for j in range(0,r*2):
if(self.in_circle(i,j,r)):
draw.point((i,j), fill=(100,50,75,50)) #alpha at 255 for test2.png
im.save("test.png")
im_data = im.tostring()
pixbuf = gdk.pixbuf_new_from_data(im_data, gdk.COLORSPACE_RGB, True, 8, im.size[0], im.size[1], 4*im.size[0])
pixmap2, mask = pixbuf.render_pixmap_and_mask()
self.pixmap.draw_drawable(self.white_gc, pixmap2, 0,0,x-r,y-r,-1,-1)
以下是我从im.save(“test.png”)创建的图像:
请注意,第一张图片的alpha值为255(完整),第二张图片的alpha值为50
但是,当我使用当前代码将图像转换为pixmap时,我失去了透明效果
谢谢你的帮助
伊恩
编辑:我通过更多的测试缩小了范围。将pixbuf转换为pixmap时,我丢失了图像的alpha 好的,我知道了
这里的技巧是不使用pixbuf.render_pixmap_和_mask()将pixbuf转换为pixmap
取而代之的是,我将自己绘制的self.pixmap绘制到绘图表上,并在其上调用draw_pixbuf()
这是我使用的新代码
def gfx_draw_tem2(self, r, x, y):
im = Image.new("RGBA", (r*2,r*2), (1, 1, 1, 0))
draw = ImageDraw.Draw(im)
for i in range(0,r*2):
for j in range(0,r*2):
if(self.in_circle(i,j,r)):
draw.point((i,j), fill=(100,50,75,140))
im_data = im.tostring()
pixbuf = gdk.pixbuf_new_from_data(im_data, gdk.COLORSPACE_RGB, True, 8, im.size[0], im.size[1], 4*im.size[0])
self.pixmap.draw_pixbuf(self.white_gc, pixbuf, 0, 0, x, y, -1, -1, gdk.RGB_DITHER_NORMAL, 0, 0)
希望这对别人有帮助