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 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)
if self.image.endswith('.gif'):
image = Image.open(i)
if is_gif:
frames = ImageSequence.Iterator(image)
image.close()
def thumbnails(frames):
for frame in frames:
thumbnail = frame.copy().convert('RGBA')
@ -116,7 +129,19 @@ 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))

@ -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;

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

@ -6,9 +6,12 @@
<div class="container">
<div class="row">
<h2 class="no-margin"><strong>{{ artwork.title }}</strong></h2>
<h6 class="no-margin">
<span>
<p class="inline nsfw">NSFW</p>
<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>
<div class="row">
{% if current_user.is_authenticated and current_user.is_admin %}
@ -28,7 +31,13 @@
<div class="row">
<div class="column one-half">
<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>
</div>
<div class="column one-half">

@ -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

Loading…
Cancel
Save