0

So I need to take an image I made in PIL and convert it to a pixmap to be displayed in a drawable.

How do I convert from PIL to pixmap and keep the images alpha?

Currently I have this code written:

    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)

Here are the images I created from im.save("test.png"):

https://i.stack.imgur.com/XxIeP.png

Notice the first picture has an alpha of 255 (full) and the seconds has an alpha of 50.

However When I convert the images to a pixmap with my current code I lose the transparent affect.

Thanks for your help,

Ian

EDIT: I have narrowed it down a little bit with more testing. I am losing the alpha of my image when converting the pixbuf to a pixmap.

still learning
  • 157
  • 2
  • 14

1 Answers1

1

Okay figured it out.

Trick here is to not convert the pixbuf to a pixmap using pixbuf.render_pixmap_and_mask()

Instead I took my self.pixmap that I draw onto my drawable and called draw_pixbuf() on it.

Here is the new code I used.

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)

Hope this helps someone.

still learning
  • 157
  • 2
  • 14