Convert PIL Image to Cairo ImageSurface
By : user3593129
Date : March 29 2020, 07:55 AM
hop of those help? Cairo's create_for_data() is wants a writeable buffer object (a string can be used as a buffer object, but it's not writable), and it only supports 32 bits per pixel data (RGBA, or RGB followed by one unused byte). PIL, on the other hand, provides a 24bpp RGB read-only buffer object. I suggest you tell PIL to add an alpha channel, then convert the PIL buffer to a numpy array to get a writable buffer for Cairo. code :
im = Image.open(filename)
im.putalpha(256) # create alpha channel
arr = numpy.array(im)
height, width, channels = arr.shape
surface = cairo.ImageSurface.create_for_data(arr, cairo.FORMAT_RGB24, width, height)
|
Cairo, ImageSurface : Cannot get correct alpha blending
By : InsomniaNY
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have finally found what was wrong with cairo and the bad alpha blending. Cairo supports ARGB32 surface, but only with premultiplied alpha. That is, every component of a pixel is stored premultiplied with the alpha component. I have not found an answer for this over the internet, and I assume premultiplied alpha is not suited for the type of operation I'm doing. I switched to PyQt4, used ARGB32 images without premultiplication, and it worked like a charm.
|
Cairo introspection errors in Eclipse with PyDev `TypeError: Couldn't find conversion for foreign struct 'cairo.Context'
By : Shiyang Zheng US - A
Date : March 29 2020, 07:55 AM
I wish this help you In view of the lack of response I have come up with the following workaround, which uses WebKit instead of weasyprint to do the parsing, rendering and administration of the printing from html: code :
#!/usr/bin/env python3
import os
from gi.repository import Gtk, WebKit
testFile = 'file://' + os.path.join(os.getcwd(), 'printTestHtml.html')
class Example(Gtk.Window):
def __init__(self):
super(Example, self).__init__()
self.init_ui()
def init_ui(self):
self.set_title("Print Html WebKit Test")
self.resize(230, 150)
self.set_position(Gtk.WindowPosition.CENTER)
self.connect("delete-event", Gtk.main_quit)
printButton = Gtk.Button('Press Me')
self.add(printButton)
printButton.connect('clicked', self.on_printButton_clicked)
self.show_all()
def on_printButton_clicked(self, widget):
webView = WebKit.WebView()
webView.load_uri(testFile)
webFrame = webView.get_main_frame()
webFrame.print_full(Gtk.PrintOperation(),
Gtk.PrintOperationAction.PRINT_DIALOG)
def main():
app = Example()
Gtk.main()
if __name__ == "__main__":
main()
|
Cairo.ImageSurface to Gdk.Pixbuf?
By : Agnes Lydia A
Date : March 29 2020, 07:55 AM
wish help you to fix your issue It turns out that the code above is exactly correct... I just wasn't drawing anything to the surface. Works great!
|
How do I crop an ImageSurface in Cairo?
By : flerris
Date : March 29 2020, 07:55 AM
I hope this helps . I'm using the Cairo bindings for C#, and I wanted to be able to crop an ImageSurface and put it into a new ImageSurface variable, which I will then use in a seperate subroutine. The question is how would I do this properly. , You almost got it. Try this: code :
ImageSurface OutputImage = new ImageSurface (Format.Rgb24, (int)RectangleToCropTo.Width, (int)RectangleToCropTo.Height);
using (Cairo.Context cr = new Cairo.Context(OutputImage)) {
cr.SetSource (originalImage, -RectangleToCropTo.X, -RectangleToCropTo.Y);
cr.Paint ();
}
|