2

In I am using cairo with python 3 but am encountering an error where the get_data() function is returning a memoryview object.

In python 2, the get_data() function returns a Python buffer object for the data of the ImageSurface, for direct inspection or modification.

My script below is supposed to take any svg image and sisplay it in a tkinter frame. In python 3 the getdata() function does not seem to work with Image.frombuffer()

from rsvg import *
import tkinter as tk
t=tk.Tk()
frame=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(frame, 'example.svg')
frame.configure(image=tk_image)

After tons of searching I still cannot find the equivalent for python 3. Input would be hugely appreciated.

Andereoo
  • 834
  • 1
  • 8
  • 24
  • 1
    It seems to me that you have already found the equivalent function. It is still called `get_data()` but it returns a memoryview object instead of a buffer object. No surprise there. A Python 3 memoryview is the successor to a buffer object. There is no way to get the old datatype back, so you are going to have to adjust your code in Python 3 to deal with what you do get back. – BoarGules Jun 04 '19 at 00:08
  • How do I get the data from it then? I tried using the tobytes() function but then I got the error 'not enough bytes to create image'. – Andereoo Jun 04 '19 at 12:41
  • I suspect that you don't need a `tobytes()` function at all, because a memoryview is already a bytes representation. What does the old code do with its `to_bytes()` representation? The new code probably needs to do something fairly similar. – BoarGules Jun 04 '19 at 13:54
  • I am using the command `Image.frombuffer('RGBA'(width,height),surface.get_data(),'raw','BGRA',0,1)` to convert the data to an Image object. – Andereoo Jun 04 '19 at 15:01
  • 1
    Understand that your description of what you are doing lies completely in the domain of the problem you re trying to solve. We don't have your data, we don't have your graphics, so expecting us to give you concrete help with a recoding task that needs a knowledge of both is a little unrealistic. – BoarGules Jun 04 '19 at 15:53
  • I added my script to the question, and any SVG graphic works – Andereoo Jun 04 '19 at 17:34
  • Possible duplicate of [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – BoarGules Jun 10 '19 at 15:25
  • This is about an imported function not global variables. – Andereoo Jun 10 '19 at 17:04

0 Answers0