OSError: exception: access violation reading 0x0000000C

0

I have been trying to use rsvg and cairo to insert a SVG image in a tkinter frame. I got the rsvg wrapper from here and took some code to insert it into a tkinter screen from another StackOverflow post. For some reason, I keep getting the error:

OSError: exception: access violation reading 0x0000000C

It seems that there is an issue with the get_dimension_data function. Below is my script:

#some code to give rsvg.render_cairo(ctx) ability
#on windows.
import os
try:
    print("Warning, could not import 'rsvg'")
    if os.name == 'nt':
        print("Detected windows, creating rsvg.")
        #some workarounds for windows

        from ctypes import *

        l=CDLL('librsvg-2-2.dll')
        g=CDLL('libgobject-2.0-0.dll')
        g.g_type_init()

        class rsvgHandle():
            class RsvgDimensionData(Structure):
                _fields_ = [("width", c_int),
                            ("height", c_int),
                            ("em",c_double),
                            ("ex",c_double)]

            class PycairoContext(Structure):
                _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                            ("ctx", c_void_p),
                            ("base", c_void_p)]

            def __init__(self, path):
                self.path = path
                error = ''
                self.handle = l.rsvg_handle_new_from_file(self.path,error)


            def get_dimension_data(self):
                svgDim = self.RsvgDimensionData()
                l.rsvg_handle_get_dimensions(self.handle,byref(svgDim))
                return (svgDim.width,svgDim.height)

            def render_cairo(self, ctx):
                ctx.save()
                z = self.PycairoContext.from_address(id(ctx))
                l.rsvg_handle_render_cairo(self.handle, z.ctx)
                ctx.restore()



        class rsvgClass():
            def Handle(self,file):
                return rsvgHandle(file)

        rsvghandler = rsvgClass()
        import tkinter as tk
        t=tk.Tk()
        sss=tk.Frame(t)
        def svgPhotoImage(self,file_path_name):
                from PIL import Image, ImageTk
                import cairo
                svg = rsvghandler.Handle(file=file_path_name)
                width, height = svg.get_dimension_data()[:2]
                surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
                context = cairo.Context(surface)
                svg.render_cairo(context)
                tk_image=ImageTk.PhotoImage('RGBA')
                image=Image.frombuffer('RGBA',(width,height),surface.get_data(),'raw','BGRA',0,1)
                tk_image.paste(image)
                return(tk_image)
        tk_image=svgPhotoImage(sss, 'C:/Users/whoever/example.svg')
        frame.configure(image=tk_image)
except Exception as exception:
    print(exception)

I can't figure out how to fix this. Any help would be greatly appreciated.

python
python-3.x
python-3.6
cairo
rsvg
asked on Stack Overflow Jun 3, 2019 by Andoo • edited Jun 3, 2019 by Andoo

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0