get videos working for thumbnail and render

master
lza_menace 2 years ago
parent 335365dd24
commit 8208971f76

@ -2,7 +2,9 @@ from os import path
from datetime import datetime from datetime import datetime
from secrets import token_urlsafe from secrets import token_urlsafe
from flask import url_for
from PIL import Image, ImageSequence, ImageFilter from PIL import Image, ImageSequence, ImageFilter
from cv2 import VideoCapture
import peewee as pw import peewee as pw
@ -92,18 +94,29 @@ class Artwork(pw.Model):
@property @property
def thumbnail(self): 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): def generate_thumbnail(self):
is_gif = self.image.endswith('.gif')
_t = self.thumbnail _t = self.thumbnail
i = f'{config.DATA_PATH}/uploads/{self.image}' i = f'{config.DATA_PATH}/uploads/{self.image}'
t = f'{config.DATA_PATH}/uploads/{_t}' t = f'{config.DATA_PATH}/uploads/{_t}'
try: try:
size = (150,150) size = (150,150)
image = Image.open(i) if self.image.endswith('.gif'):
if is_gif: image = Image.open(i)
frames = ImageSequence.Iterator(image) frames = ImageSequence.Iterator(image)
image.close()
def thumbnails(frames): def thumbnails(frames):
for frame in frames: for frame in frames:
thumbnail = frame.copy().convert('RGBA') thumbnail = frame.copy().convert('RGBA')
@ -116,12 +129,24 @@ class Artwork(pw.Model):
_image.info = image.info _image.info = image.info
_image.save(t, save_all=True, append_images=list(_frames), disposal=2) _image.save(t, save_all=True, append_images=list(_frames), disposal=2)
_image.close() _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: else:
image = Image.open(i)
image.thumbnail(size, Image.ANTIALIAS) image.thumbnail(size, Image.ANTIALIAS)
if self.nsfw: if self.nsfw:
image = image.filter(ImageFilter.GaussianBlur(radius = 6)) image = image.filter(ImageFilter.GaussianBlur(radius = 6))
image.save(t, format=image.format) image.save(t, format=image.format)
image.close() image.close()
self.save() self.save()
return True return True
except Exception as e: except Exception as e:

@ -20,6 +20,17 @@ a, a:visited {
margin-top: 4em; margin-top: 4em;
} }
.inline {
display: inline;
}
.nsfw {
color: red;
border: 1px solid red;
border-radius: 4px;
padding: 4px;
}
.button { .button {
color: white; color: white;
border: 1px solid white; border: 1px solid white;

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

@ -6,9 +6,12 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<h2 class="no-margin"><strong>{{ artwork.title }}</strong></h2> <h2 class="no-margin"><strong>{{ artwork.title }}</strong></h2>
<h6 class="no-margin"> <span>
posted by <a href="{{ url_for('user.show', handle=artwork.user.handle) }}">{{ artwork.user.handle }}</a> - {{ artwork.upload_date | humanize }} <p class="inline nsfw">NSFW</p>
</h6> <h6 class="no-margin inline">
posted by <a href="{{ url_for('user.show', handle=artwork.user.handle) }}">{{ artwork.user.handle }}</a> - {{ artwork.upload_date | humanize }}
</h6>
</span>
<p class="artworkDescription">{{ artwork.description }}</p> <p class="artworkDescription">{{ artwork.description }}</p>
<div class="row"> <div class="row">
{% if current_user.is_authenticated and current_user.is_admin %} {% if current_user.is_authenticated and current_user.is_admin %}
@ -28,7 +31,13 @@
<div class="row"> <div class="row">
<div class="column one-half"> <div class="column one-half">
<a href="{{ img }}"> <a href="{{ img }}">
<img src="{{ img }}" width="100%" style="padding-bottom: 2em;"> {% if artwork.is_video %}
<video controls autoplay loop preload disablepictureinpicture muted playsinline width="100%">
<source src="{{ img }}" type="video/{{ artwork.image.split('.')[-1]}}">
</video>
{% else %}
<img src="{{ img }}" width="100%">
{% endif %}
</a> </a>
</div> </div>
<div class="column one-half"> <div class="column one-half">

@ -18,6 +18,8 @@ Jinja2==3.1.2
MarkupSafe==2.1.1 MarkupSafe==2.1.1
mccabe==0.7.0 mccabe==0.7.0
monero==1.1.1 monero==1.1.1
numpy==1.23.5
opencv-python-headless==4.6.0.66
packaging==21.3 packaging==21.3
peewee==3.15.4 peewee==3.15.4
Pillow==9.3.0 Pillow==9.3.0

Loading…
Cancel
Save