From b9bb98966cfcf170b5bda6067c0b28118e4744cc Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 24 Aug 2023 19:32:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=86=E8=A7=92=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/lib/xshot/xshot.py | 50 +++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/usr/lib/xshot/xshot.py b/usr/lib/xshot/xshot.py index ed961df..2a7de48 100755 --- a/usr/lib/xshot/xshot.py +++ b/usr/lib/xshot/xshot.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -import gi, sys, os +import gi, sys, os, cairo, math import dbus import dbus.service, dbus.mainloop.glib @@ -10,7 +10,7 @@ gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GLib, GdkPixbuf from PIL import Image, ImageFilter, ImageChops -app_id = 'fun.wkit.shotit' +app_id = 'fun.wkit.xshot' def pixbuf_to_pil(pixbuf): @@ -34,22 +34,55 @@ def pil_to_pixbuf(img): return GdkPixbuf.Pixbuf.new_from_bytes(GLib.Bytes.new(data), GdkPixbuf.Colorspace.RGB, alpha, 8, w, h, rowstride) +def set_radius(pixbuf, radius = 4): + w = pixbuf.get_width() + h = pixbuf.get_height() + + surface = cairo.ImageSurface(cairo.Format.ARGB32, w, h) + ctx = cairo.Context(surface) + Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0) + + # 左上角 圆角 + ctx.arc(radius, radius, radius, -math.pi, -math.pi / 2) + ctx.line_to(w - radius, 0) + + # 右上角 圆角 + ctx.arc(w - radius, radius, radius, -math.pi / 2, 0) + ctx.line_to(w, -radius) + + # 右下角 圆角 + ctx.arc(w - radius, h - radius, radius, 0, math.pi / 2) + ctx.line_to(radius, h) + + # 左下角 圆角 + ctx.arc(radius, h - radius, radius, math.pi / 2, math.pi) + ctx.close_path() + + ctx.clip() + ctx.paint() + + return Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h) + + + def add_shadow(img): # 加载图片并确认该图片为RGBA模式,保证透明度 + img = set_radius(img) img = pixbuf_to_pil(img).convert('RGBA') w,h = img.size w += 64 h += 64 - shadow = Image.new('RGBA', img.size, (80,80,80,100)) + shadow = Image.new('RGBA', img.size, (80,80,80,223)) + shadow = pixbuf_to_pil(set_radius(pil_to_pixbuf(shadow))) - new_img = Image.new('RGBA', (w,h), (0,0,0,0)) - # new_img = Image.new('RGBA', (w,h), (255,255,255,255)) - new_img.paste(shadow, (32, 32)) + #new_img = Image.new('RGBA', (w,h), (0,0,0,0)) + new_img = Image.new('RGBA', (w,h), (255,255,255,255)) + new_img.paste(shadow, (32, 32), shadow) new_img = new_img.filter(ImageFilter.GaussianBlur(radius = 12)) - new_img.paste(img, (32, 32)) + new_img.paste(img, (32, 32), img) return pil_to_pixbuf(new_img) @@ -88,9 +121,6 @@ class Application(Gtk.Application): self.copy(image) - # print('截图成功...') - - class ApplicationService(dbus.service.Object): def __init__(self, app): self.app = app