Created
February 13, 2022 04:51
-
-
Save Quelklef/da5bfd8037f54e9e5acc9352edf7eed6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, Gdk, GObject, GLib | |
import cairo | |
import math | |
import io | |
import PIL.Image as Image | |
import random | |
window = Gtk.Window() | |
window.set_position(Gtk.WindowPosition.CENTER) | |
window.set_default_size(400, 550) | |
window.set_title("Zongus") | |
window.connect("delete-event", Gtk.main_quit) | |
window.set_decorated(True) | |
# The magic lines | |
window.set_app_paintable(True) | |
window.set_visual(window.get_screen().get_rgba_visual()) | |
draw = None | |
def clear(_w, cr): | |
print("clear") | |
cr.set_source_rgba(0, 0, 0, 0) | |
cr.rectangle(0, 0, 1000, 1000) | |
cr.fill() | |
global draw | |
draw = paint | |
window.queue_draw() | |
def paint(_w, cr): | |
print("paint") | |
win = Gdk.get_default_root_window() | |
x0, y0 = window.get_position() | |
dx, dy = window.get_size() | |
y0 += 37 # title bar -- FIXME | |
pixbuf = Gdk.pixbuf_get_from_window(win, x0, y0, dx, dy) | |
_, bytearr = pixbuf.save_to_bufferv('png', [], []) | |
img = Image.open(io.BytesIO(bytearr)) | |
cr.set_source_rgb(255, 0, 0) | |
cr.set_line_width(1) | |
def px_draw(x, y): | |
cr.move_to(x, y) | |
cr.line_to(x + 1, y + 1) | |
cr.stroke() | |
for idx, (h, s, v) in enumerate(img.convert('HSV').getdata()): | |
x = idx % img.width | |
y = idx // img.width | |
if v / 255 < .5: #.01 | |
px_draw(x, y) | |
global draw | |
draw = clear | |
def redraw(): window.queue_draw(); return False | |
GLib.timeout_add_seconds(4, redraw) | |
draw = clear | |
canv = Gtk.DrawingArea() | |
canv.connect('draw', lambda *args: draw(*args)); | |
window.add(canv) | |
window.show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment