增加圆角处理

master
yutent 2023-08-24 19:32:35 +08:00
parent 9a63a0ad51
commit b9bb98966c
1 changed files with 40 additions and 10 deletions

View File

@ -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