Search code examples
pythonpygtkpygobject

PyGI: Create pixbuf from 0


I want to do a image of a cable (line) between two points. I thought on doing the cable with the distance between the two points and then rotating the image the number of degrees that have the two points between.

But I don't know how to create a image from nothing. I mean, doing a image of 5px wide and "x" lenght, and then rotate it "y" degrees.

Also if I could select the color, that would be nice.

Sorry for my bad English and thanks


Solution

  • You can try to create a svg e.g. using the svgwrite module and load it with a PixbufLoader. The rotation can be done by calculating the corresponding coordinates or using a svg transformation.

    import svgwrite
    from gi.repository import GdkPixbuf
    
    drawing = svgwrite.Drawing(size=('100px', '100px'))
    line = drawing.line(start=(50, 0), end=(50, 100), stroke='blue')
    drawing.add(line)
    encoded = drawing.tostring().encode()
    
    loader = GdkPixbuf.PixbufLoader()
    loader.write(encoded)
    loader.close()
    pixbuf = loader.get_pixbuf()