Paintwork on the surface of Cairo

I am trying to draw a decoded jpeg on the surface of Cairo ... However, I am a bit stuck and I have no idea how to move further from

import cairo import Image path_to_jpeg = "/home/seif/Pictures/prw.jpg" surface = cairo.PDFSurface ("out.pdf", 1000, 1000) ctx = cairo.Context (surface) image = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1000, 1000) dt = Image.open(path_to_jpeg) dimage = dt.load() 

Any help would be greatly appreciated ...

+4
source share
2 answers

this should do the trick. First you need to convert the image to png, this is apparently the only format with which you can create surfaces. here is what makes a good piece of code below. I recommend that you look at this question , which helped me in creating the code below.

 import Image, StringIO from cairo import PDFSurface, Context, ImageSurface pdf = PDFSurface("out.pdf", 1000, 1000) cr = Context(pdf) im = Image.open("/home/seif/Pictures/prw.jpg") buffer = StringIO.StringIO() im.save(buffer, format="PNG") buffer.seek(0) cr.save() cr.set_source_surface(ImageSurface.create_from_png(buffer)) cr.paint() 
+3
source

When using cairocffi instead of pycairo (API compatible), the cairocffi.pixbuf module provides integration with GDK-PixBuf to load all kinds of image formats into cairo.

http://pythonhosted.org/cairocffi/pixbuf.html

(Disclaimer, I'm the author.)

+1
source

Source: https://habr.com/ru/post/1446954/


All Articles