sonist-gtk/utils.py

36 lines
984 B
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
import gi
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)
def blur_image(pixbuf):
# 加载图片并确认该图片为RGBA模式保证透明度
img = pixbuf_to_pil(pixbuf).convert('RGBA')
mask = Image.new('RGBA', img.size, (32, 32, 32,160))
img = img.filter(ImageFilter.GaussianBlur(radius = 16))
img.alpha_composite(mask)
return pil_to_pixbuf(img)
python + gtk3开发的基于mpd后端的音乐播放器
Python 99.1%
Shell 0.9%