Zulko / gizeh

Simple Vector Graphics for Python
Other
702 stars 70 forks source link

Loading from PNG and drawing on a surface #24

Open Heroico opened 8 years ago

Heroico commented 8 years ago

Hi there!

After some trial and error I got to load a PNG image, and use it as a fill for a rectangle.

        # load png and convert to numpy array
        image_surface = cairo.ImageSurface.create_from_png("my_img.png")
        im = 0+numpy.frombuffer(image_surface.get_data(), numpy.uint8)
        im.shape = (image_surface.get_height(), image_surface.get_width(), 4)
        im = im[:,:,[2,1,0,3]] # put RGB back in order

        #gizeh image pattern
        gizeh_pattern = gizeh.ImagePattern(im)
        r = gizeh.rectangle(lx=im.shape[0]*2, ly=im.shape[1]*2, xy=(im.shape[0], im.shape[1] ), fill=gizeh_pattern)

        surface = gizeh.Surface(WIDTH, HEIGHT, bg_color=(0, 0.0, 0.0))
        r.draw(surface)

Anyways, I was mostly posting the code for reference. I am not sure if I should formalize this into a PNGImagePattern to be used as fill, of if I should modify gizeh's ImagePattern to include this code, as a variant. Maybe I could add something to gizeh's Surface so that a sufrface can be loaded from a png.

Any suggestions are appreciated, and I can make a pull request if some consensus arises.