This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0
py-gtk-notes/notes/image.py

41 lines
1.0 KiB
Python

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']
)
python + gtk3 + webkit2 学习笔记
Python 60.1%
JavaScript 37.6%
HTML 2.3%