From 8208971f76113fa129f3adb5bf651b465f8736e0 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sat, 3 Dec 2022 01:00:29 -0800 Subject: [PATCH] get videos working for thumbnail and render --- nerochan/models.py | 35 +++++++++++++++++++++++---- nerochan/static/css/main.css | 11 +++++++++ nerochan/static/images/play.png | Bin 0 -> 3299 bytes nerochan/templates/artwork/show.html | 17 ++++++++++--- requirements.txt | 2 ++ 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 nerochan/static/images/play.png diff --git a/nerochan/models.py b/nerochan/models.py index 7a03eef..d5e43d1 100644 --- a/nerochan/models.py +++ b/nerochan/models.py @@ -2,7 +2,9 @@ from os import path from datetime import datetime from secrets import token_urlsafe +from flask import url_for from PIL import Image, ImageSequence, ImageFilter +from cv2 import VideoCapture import peewee as pw @@ -92,18 +94,29 @@ class Artwork(pw.Model): @property def thumbnail(self): - return f'thumbnail-{self.image}' + res = f'thumbnail-{self.image}' + if self.is_video: + return f'{res[:-3]}.png' + else: + return res + + @property + def is_video(self): + if self.image.endswith('.mp4') or self.image.endswith('.webm'): + return True + else: + return False def generate_thumbnail(self): - is_gif = self.image.endswith('.gif') _t = self.thumbnail i = f'{config.DATA_PATH}/uploads/{self.image}' t = f'{config.DATA_PATH}/uploads/{_t}' try: size = (150,150) - image = Image.open(i) - if is_gif: + if self.image.endswith('.gif'): + image = Image.open(i) frames = ImageSequence.Iterator(image) + image.close() def thumbnails(frames): for frame in frames: thumbnail = frame.copy().convert('RGBA') @@ -116,12 +129,24 @@ class Artwork(pw.Model): _image.info = image.info _image.save(t, save_all=True, append_images=list(_frames), disposal=2) _image.close() + elif self.is_video: + cap = VideoCapture(i) + _, frame = cap.read() + image = Image.fromarray(frame) + if self.nsfw: + image = image.filter(ImageFilter.GaussianBlur(radius = 6)) + pb = Image.open('nerochan/static/images/play.png') + image.paste(pb, (int(image.width / 2) - int(pb.width / 2), int(image.height / 2) - int(pb.height / 2))) + pb.close() + image.save(t, format=image.format) + image.close() else: + image = Image.open(i) image.thumbnail(size, Image.ANTIALIAS) if self.nsfw: image = image.filter(ImageFilter.GaussianBlur(radius = 6)) image.save(t, format=image.format) - image.close() + image.close() self.save() return True except Exception as e: diff --git a/nerochan/static/css/main.css b/nerochan/static/css/main.css index b53f34c..f1d99bd 100644 --- a/nerochan/static/css/main.css +++ b/nerochan/static/css/main.css @@ -20,6 +20,17 @@ a, a:visited { margin-top: 4em; } +.inline { + display: inline; +} + +.nsfw { + color: red; + border: 1px solid red; + border-radius: 4px; + padding: 4px; +} + .button { color: white; border: 1px solid white; diff --git a/nerochan/static/images/play.png b/nerochan/static/images/play.png new file mode 100644 index 0000000000000000000000000000000000000000..9f41dee6426694bca624272e101dcaf16643d0e6 GIT binary patch literal 3299 zcmV<93>@=`P)^1-Z42(%cK~#9!?VVqDqC61B(-iM()813i`v#W#=;FPt z`u)Gkt5RDC`7=o-DZDEedp756<=1Cpm}h1}NZnn0*)a$ym)ln8uf~e%dC2xPV3@&t=Fdy>k{IOix}W8Penlp$ppDv+2xO7 zZ9$G1o}2Fc`g!KN%9@kz`Wj@^NE{DWe^>sRw{6EK;hu+WE7r^=Zv1R`gl8eZxEgIf2`yl_=T$=|a0FcS z|6@)~eDY8yFh|E#&)O?399JtaN5)m%gzt%+Pafv+>y9@ zL6C5XV9rj{hAV&Ib4$>0v+YRUKwSCvZ(4$fOR~}O8;Cplnh{J~q75H#aE}5Pmxx0| z+-E_MakE`K+$Ja3xP-h#!`&D7xRe|s;XczCxCM=adkJh};O1{GxZpgnu`NG zO9IAy7O`;Uha3089nJ@ykyyASdjltR2rzAP6!CE7SciMyGDWNq3s-(vc;Jq|XYp`J zb^zv}2q?=`iMY6A2h2g~a3u#pBI8Pn8F$1TUKNRrD@DFRxIE8eD&5H!MMTvy_Oq1YJ99*6kNpMy2df-+x5iW(`TKhpV>;P9gEbYA{#FinwKG4$O z?(F$fVrySnkz}~TN1GRJBhujx12Yc9W#dpR4K8V!BrgPaEN&{?Y-7t#6LbFg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dY~%YdiEqH)FAJH39oYU?{Fe+Tm6MBwP(II|~d- zR%beCrS5VpTStS zLQmEYNQ-;^m#3m_Te(jwYuoMmqXztV3|wX-&MwG#cb)&*()EY=XIM;JyUfei)9G_v z6zk%2IsyK9T-;@idM#t)E^%q2<1SHYUu zEC|%^z5%#?D{#n-?znf{e;b!CA

{{ artwork.title }}

-
- posted by {{ artwork.user.handle }} - {{ artwork.upload_date | humanize }} -
+ +

NSFW

+
+ posted by {{ artwork.user.handle }} - {{ artwork.upload_date | humanize }} +
+

{{ artwork.description }}

{% if current_user.is_authenticated and current_user.is_admin %} @@ -28,7 +31,13 @@
diff --git a/requirements.txt b/requirements.txt index 61abebd..d61be55 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,6 +18,8 @@ Jinja2==3.1.2 MarkupSafe==2.1.1 mccabe==0.7.0 monero==1.1.1 +numpy==1.23.5 +opencv-python-headless==4.6.0.66 packaging==21.3 peewee==3.15.4 Pillow==9.3.0