2023-08-17 20:51:59 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import gi, cairo, math
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk, Gdk, GdkPixbuf
|
|
|
|
|
|
|
|
|
|
|
|
class ScaleImage(Gtk.Image):
|
|
|
|
def __init__(self, filepath):
|
|
|
|
Gtk.Image.__init__(self)
|
2023-08-18 19:04:51 +08:00
|
|
|
self.width = None
|
|
|
|
self.height = None
|
|
|
|
self.reset(filepath)
|
2023-08-17 20:51:59 +08:00
|
|
|
|
2023-08-18 19:04:51 +08:00
|
|
|
|
2023-08-25 11:19:40 +08:00
|
|
|
def reset(self, filepath, crop = False):
|
2023-08-22 16:19:20 +08:00
|
|
|
if type(filepath) == str:
|
|
|
|
self.origin = GdkPixbuf.Pixbuf.new_from_file(filepath)
|
|
|
|
else:
|
|
|
|
self.origin = filepath
|
2023-08-17 20:51:59 +08:00
|
|
|
|
2023-08-18 19:04:51 +08:00
|
|
|
if self.width is None:
|
|
|
|
self.pixbuf = self.origin
|
2023-08-17 20:51:59 +08:00
|
|
|
|
2023-08-18 19:04:51 +08:00
|
|
|
self.width = self.origin.get_width()
|
|
|
|
self.height = self.origin.get_height()
|
2023-08-25 11:47:38 +08:00
|
|
|
|
2023-08-25 11:19:40 +08:00
|
|
|
self.set_from_pixbuf(self.pixbuf)
|
2023-08-18 19:04:51 +08:00
|
|
|
else:
|
2023-08-25 11:19:40 +08:00
|
|
|
w = self.origin.get_width()
|
|
|
|
h = self.origin.get_height()
|
|
|
|
if crop and w != h:
|
|
|
|
size = min(self.width, self.height)
|
|
|
|
self.clip_resize(size)
|
|
|
|
else:
|
|
|
|
self.pixbuf = self.origin.scale_simple(self.width, self.height, GdkPixbuf.InterpType.BILINEAR)
|
2023-08-25 11:47:38 +08:00
|
|
|
self.set_from_pixbuf(self.pixbuf)
|
|
|
|
|
|
|
|
return self
|
2023-08-17 20:51:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def resize(self, width, height):
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.pixbuf = self.origin.scale_simple(width, height, GdkPixbuf.InterpType.BILINEAR)
|
|
|
|
self.set_from_pixbuf(self.pixbuf)
|
2023-08-18 19:04:51 +08:00
|
|
|
return self
|
2023-08-17 20:51:59 +08:00
|
|
|
|
2023-08-25 11:19:40 +08:00
|
|
|
def clip_resize(self, size):
|
|
|
|
w = self.origin.get_width()
|
|
|
|
h = self.origin.get_height()
|
|
|
|
_size = min(w, h) # 按小边裁剪成正方形
|
|
|
|
size = min(size, _size) # 缩放按最小值来
|
|
|
|
self.width = size
|
|
|
|
self.height = size
|
|
|
|
|
|
|
|
surface = cairo.ImageSurface(cairo.Format.ARGB32, w, h)
|
|
|
|
ctx = cairo.Context(surface)
|
|
|
|
Gdk.cairo_set_source_pixbuf(ctx, self.origin, 0, 0)
|
|
|
|
|
|
|
|
ctx.line_to(0, 0)
|
|
|
|
ctx.line_to(_size, 0)
|
|
|
|
ctx.line_to(_size, _size)
|
|
|
|
ctx.line_to(0, _size)
|
|
|
|
|
|
|
|
ctx.close_path()
|
|
|
|
|
|
|
|
ctx.clip()
|
|
|
|
ctx.paint()
|
|
|
|
|
|
|
|
pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, _size, _size)
|
|
|
|
|
|
|
|
self.pixbuf = pixbuf.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR)
|
|
|
|
self.set_from_pixbuf(self.pixbuf)
|
|
|
|
|
|
|
|
return self
|
2023-08-17 20:51:59 +08:00
|
|
|
|
|
|
|
def set_radius(self, radius = 0):
|
|
|
|
w = self.width
|
|
|
|
h = self.height
|
|
|
|
surface = cairo.ImageSurface(cairo.Format.ARGB32, w, h)
|
|
|
|
ctx = cairo.Context(surface)
|
|
|
|
Gdk.cairo_set_source_pixbuf(ctx, self.pixbuf, 0, 0)
|
|
|
|
|
|
|
|
# 左上角 圆角
|
2023-08-18 19:04:51 +08:00
|
|
|
ctx.arc(radius, radius, radius, -math.pi, -math.pi / 2)
|
2023-08-17 20:51:59 +08:00
|
|
|
ctx.line_to(w - radius, 0)
|
|
|
|
|
|
|
|
# 右上角 圆角
|
2023-08-18 19:04:51 +08:00
|
|
|
ctx.arc(w - radius, radius, radius, -math.pi / 2, 0)
|
2023-08-17 20:51:59 +08:00
|
|
|
ctx.line_to(w, -radius)
|
|
|
|
|
|
|
|
# 右下角 圆角
|
2023-08-18 19:04:51 +08:00
|
|
|
ctx.arc(w - radius, h - radius, radius, 0, math.pi / 2)
|
2023-08-17 20:51:59 +08:00
|
|
|
ctx.line_to(radius, h)
|
|
|
|
|
|
|
|
# 左下角 圆角
|
2023-08-18 19:04:51 +08:00
|
|
|
ctx.arc(radius, h - radius, radius, math.pi / 2, math.pi)
|
2023-08-17 20:51:59 +08:00
|
|
|
ctx.close_path()
|
|
|
|
|
|
|
|
ctx.clip()
|
|
|
|
ctx.paint()
|
|
|
|
|
|
|
|
pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h)
|
|
|
|
self.set_from_pixbuf(pixbuf)
|
2023-08-18 19:04:51 +08:00
|
|
|
return self
|
2023-08-17 20:51:59 +08:00
|
|
|
|
|
|
|
|
2023-08-18 19:04:51 +08:00
|
|
|
# 旋转不生效
|
|
|
|
def rotate(self, deg = 0, point = None):
|
|
|
|
w = self.width
|
|
|
|
h = self.height
|
|
|
|
surface = cairo.ImageSurface(cairo.Format.ARGB32, w, h)
|
|
|
|
ctx = cairo.Context(surface)
|
|
|
|
Gdk.cairo_set_source_pixbuf(ctx, self.pixbuf, 0, 0)
|
|
|
|
|
|
|
|
# 圆心坐标
|
|
|
|
if point is None:
|
|
|
|
point = (w / 2, h / 2)
|
|
|
|
|
|
|
|
ctx.translate(point[0], point[1])
|
|
|
|
ctx.rotate((deg / 180) * math.pi)
|
|
|
|
|
|
|
|
ctx.paint()
|
|
|
|
|
|
|
|
pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h)
|
|
|
|
|
|
|
|
self.set_from_pixbuf(pixbuf)
|
|
|
|
return self
|