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
|
|
|
|
|
|
|
def reset(self, filepath):
|
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()
|
|
|
|
else:
|
|
|
|
self.pixbuf = self.origin.scale_simple(self.width, self.height, GdkPixbuf.InterpType.BILINEAR)
|
2023-08-17 20:51:59 +08:00
|
|
|
|
2023-08-18 19:04:51 +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
|
|
|
|
|
|
|
|
|
|
|
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
|