2023-08-17 20:51:59 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
2023-08-21 19:06:22 +08:00
|
|
|
|
import gi, io, base64
|
2023-08-17 20:51:59 +08:00
|
|
|
|
from gi.repository import Gdk, GLib, GdkPixbuf
|
|
|
|
|
from PIL import Image, ImageFilter
|
|
|
|
|
|
|
|
|
|
def pixbuf_to_pil(pixbuf):
|
|
|
|
|
data = pixbuf.get_pixels()
|
|
|
|
|
w = pixbuf.get_width()
|
|
|
|
|
h = pixbuf.get_height()
|
|
|
|
|
stride = pixbuf.get_rowstride()
|
|
|
|
|
mode = "RGBA" if pixbuf.get_has_alpha() else "RGB"
|
|
|
|
|
img = Image.frombytes(mode, (w, h), data, "raw", mode, stride)
|
|
|
|
|
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pil_to_pixbuf(img):
|
|
|
|
|
data = []
|
|
|
|
|
for p in img.getdata():
|
|
|
|
|
data.extend(p)
|
|
|
|
|
data = bytes(data)
|
|
|
|
|
alpha = img.mode == 'RGBA'
|
|
|
|
|
w, h = img.size
|
|
|
|
|
rowstride = w * 4
|
|
|
|
|
return GdkPixbuf.Pixbuf.new_from_bytes(GLib.Bytes.new(data), GdkPixbuf.Colorspace.RGB, alpha, 8, w, h, rowstride)
|
|
|
|
|
|
|
|
|
|
|
2023-08-21 19:06:22 +08:00
|
|
|
|
def base64_to_pixbuf(base64_str):
|
|
|
|
|
# 解码base64
|
|
|
|
|
decoded = base64.b64decode(base64_str)
|
|
|
|
|
# 创建输入流
|
|
|
|
|
input_stream = io.BytesIO(decoded)
|
|
|
|
|
# 从输入流创建pixbuf
|
|
|
|
|
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(input_stream, None)
|
|
|
|
|
return pixbuf
|
|
|
|
|
|
2023-08-17 20:51:59 +08:00
|
|
|
|
def blur_image(pixbuf):
|
|
|
|
|
# 加载图片并确认该图片为RGBA模式,保证透明度
|
|
|
|
|
img = pixbuf_to_pil(pixbuf).convert('RGBA')
|
2023-08-21 19:06:22 +08:00
|
|
|
|
mask = Image.new('RGBA', img.size, (64, 64, 64, 160))
|
|
|
|
|
img = img.filter(ImageFilter.GaussianBlur(radius = 32))
|
2023-08-17 20:51:59 +08:00
|
|
|
|
img.alpha_composite(mask)
|
|
|
|
|
|
|
|
|
|
return pil_to_pixbuf(img)
|