|
|
@ -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)
|
|
|
|
|
|
|
|
if self.image.endswith('.gif'):
|
|
|
|
image = Image.open(i)
|
|
|
|
image = Image.open(i)
|
|
|
|
if is_gif:
|
|
|
|
|
|
|
|
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,7 +129,19 @@ 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))
|
|
|
|