import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Gdk from gi.repository.GdkPixbuf import Pixbuf # 读取图片, 返回图片像素数据 filename = 'xxx.png' pixbuf = Pixbuf.new_from_file(filename) # 得到这个对象, 就是可以传给前端的, image = { "width": pixbuf.get_width(), "height": pixbuf.get_height(), "colorspace": pixbuf.get_colorspace(), "has_alpha": pixbuf. get_has_alpha(), "bits_per_sample": pixbuf.get_bits_per_sample(), "rowstride": pixbuf.get_rowstride(), "filepath": filename, "bytes": list(pixbuf.get_pixels()) } # scripts = 'native.$emit("' + callback + '",' + json.dumps(image) + ')' # 倒扣到前端传回来的数据, 调用 Pixbuf.new_from_data 可转回 Pixbuf对象 pixbuf = Pixbuf.new_from_data( data = bytes(image['bytes']), colorspace = image['colorspace'], has_alpha = image['has_alpha'], bits_per_sample = image['bits_per_sample'], width = image['width'], height = image['height'], rowstride = image['rowstride'] )