- draw img helper function

This commit is contained in:
trigg 2020-10-05 16:26:11 +01:00
parent 3e766aff66
commit c1d7447db1
2 changed files with 73 additions and 28 deletions

View file

@ -63,11 +63,16 @@ class Surface_Getter():
def get_url(self):
im = Image.open(requests.get(self.url, stream=True, headers={
'Referer': 'https://streamkit.discord.com/overlay/voice', 'User-Agent': 'Mozilla/5.0'}).raw)
surf = self.from_pil(im)
try:
resp = requests.get(self.url, stream=True, headers={
'Referer': 'https://streamkit.discord.com/overlay/voice', 'User-Agent': 'Mozilla/5.0'})
raw = resp.raw
im = Image.open(raw)
surf = self.from_pil(im)
self.func(self.id, surf)
self.func(self.id, surf)
except:
logging.error("Unable to open %s" % (self.url))
def from_pil(self, im, alpha=1.0, format=cairo.FORMAT_ARGB32):
"""
@ -95,3 +100,53 @@ def get_surface(func, id, ava, size):
image_getter = Surface_Getter(func, id, ava, size)
t = threading.Thread(target=image_getter.get_url, args=())
t.start()
def get_aspected_size(img, w, h, anchor=0, hanchor=0):
px = img.get_width()
py = img.get_height()
if py < 1 or h < 1:
return (0, 0)
img_aspect = px / py
rect_aspect = w / h
y = 0
x = 0
if img_aspect > rect_aspect:
oh = h
h = w / img_aspect
if anchor == 0:
y = y + (oh - h)
if anchor == 1:
y = y + ((oh - h) / 2)
elif img_aspect < rect_aspect:
ow = w
w = h * img_aspect
if hanchor == 2:
x = x + (ow - w)
if hanchor == 1:
x = x + ((ow - w) / 2)
return (x, y, w, h)
def draw_img_to_rect(img, ctx, x, y, w, h, path=False, aspect=False, anchor=0, hanchor=0):
# Path - only add the path do not fill : True/False
# Aspect - keep aspect ratio : True/False
# Anchor - with aspect : 0=left 1=middle 2=right
# HAnchor - with apect : 0=bottom 1=middle 2=top
ctx.save()
px = img.get_width()
py = img.get_height()
(x_off, y_off, w, h) = get_aspected_size(
img, w, h, anchor=anchor, hanchor=hanchor)
ctx.translate(x + x_off, y + y_off)
ctx.scale(w, h)
ctx.scale(1 / px, 1 / py)
ctx.set_source_surface(img, 0, 0)
ctx.rectangle(0, 0, px, py)
if not path:
ctx.fill()
ctx.restore()
return (w, h)

View file

@ -8,7 +8,7 @@ import cairo
import logging
import time
import re
from .image_getter import get_surface
from .image_getter import get_surface, draw_img_to_rect, get_aspected_size
class TextOverlayWindow(OverlayWindow):
@ -157,18 +157,21 @@ class TextOverlayWindow(OverlayWindow):
def draw_attach(self, y, url):
(w, h) = self.get_size()
if self.attachment[url]:
if url in self.attachment and self.attachment[url]:
pix = self.attachment[url]
h = pix.get_height()
iw = pix.get_width()
ih = pix.get_height()
iw = min(iw, w)
ih = min(ih, (h * .7))
(ax, ay, aw, ah) = get_aspected_size(pix, iw, ih)
self.col(self.bg_col)
self.context.rectangle(0, y - h, w, h)
self.context.rectangle(0, y - ah, w, ah)
self.context.fill()
self.context.set_operator(cairo.OPERATOR_OVER)
self.col([1, 1, 1, 1])
self.context.set_source_surface(pix, 0, y - h)
self.context.rectangle(0, y - h, w, h)
self.context.fill()
return y - h
new_w, new_h = draw_img_to_rect(
pix, self.context, 0, y - ih, iw, ih, aspect=True)
return y - new_h
return y
def draw_text(self, y, text):
@ -225,21 +228,8 @@ class TextOverlayWindow(OverlayWindow):
return
pix = self.attachment[key]
(x, y) = ctx.get_current_point()
px = pix.get_width()
py = pix.get_height()
ctx.save()
ctx.translate(x, y - self.text_size)
ctx.scale(self.text_size, self.text_size)
ctx.scale(1 / px, 1 / py)
ctx.set_source_surface(pix, 0, 0)
ctx.rectangle(0, 0, px, py)
if not path:
ctx.fill()
ctx.restore()
ctx.move_to(x + self.text_size, y)
draw_img_to_rect(pix, ctx, x, y - self.text_size, self.text_size,
self.text_size, path=path)
return True
def santize_string(self, string):