mirror of
https://github.com/simon987/Simple-Incremental-Search-Tool.git
synced 2025-12-14 07:39:05 +00:00
Added navbar and updated thumbnail generator
This commit is contained in:
69
thumbnail.py
69
thumbnail.py
@@ -1,37 +1,42 @@
|
||||
from PIL import Image
|
||||
import os
|
||||
from parsing import ContentMimeGuesser
|
||||
from parsing import ContentMimeGuesser, ExtensionMimeGuesser
|
||||
from multiprocessing import Value
|
||||
import ffmpeg
|
||||
|
||||
|
||||
class ThumbnailGenerator:
|
||||
|
||||
def __init__(self, size):
|
||||
self.size = (size, size)
|
||||
self.mime_guesser = ContentMimeGuesser
|
||||
self.mime_guesser = ContentMimeGuesser()
|
||||
|
||||
def generate(self, path, dest_path):
|
||||
|
||||
try:
|
||||
with open(path, "rb") as image_file:
|
||||
with Image.open(image_file) as image:
|
||||
mime = self.mime_guesser.guess_mime(path)
|
||||
|
||||
image.thumbnail(self.size, Image.BICUBIC)
|
||||
if mime.startswith("image"):
|
||||
try:
|
||||
self.generate_image(path, dest_path)
|
||||
pass
|
||||
except OSError:
|
||||
print("Not an image " + path)
|
||||
|
||||
canvas = Image.new("RGB", image.size, (255, 0, 255))
|
||||
elif mime.startswith("video"):
|
||||
try:
|
||||
(ffmpeg.
|
||||
input(path)
|
||||
.output("tmp", vframes=1, f="image2", loglevel="error")
|
||||
.run()
|
||||
)
|
||||
self.generate_image("tmp", dest_path)
|
||||
os.remove("tmp")
|
||||
except:
|
||||
print("Couldn't make thumbnail for " + path)
|
||||
|
||||
if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info):
|
||||
canvas.paste(image, mask=image.split()[3]) # 3 is the alpha channel
|
||||
else:
|
||||
canvas.paste(image)
|
||||
# print(dest_path + " - " + str(os.path.getsize(dest_path))) # debug todo remove
|
||||
|
||||
canvas.save(dest_path, "JPEG", quality=50, optimize=True)
|
||||
canvas.close()
|
||||
|
||||
except OSError as e:
|
||||
print(e)
|
||||
print("Not an image " + path)
|
||||
|
||||
def generate_all(self, docs, dest_path):
|
||||
def generate_all(self, docs, dest_path, counter: Value=None):
|
||||
|
||||
os.makedirs(dest_path, exist_ok=True)
|
||||
|
||||
@@ -40,4 +45,28 @@ class ThumbnailGenerator:
|
||||
full_path = os.path.join(doc["_source"]["path"], doc["_source"]["name"])
|
||||
|
||||
if os.path.isfile(full_path):
|
||||
self.generate(full_path, os.path.join(dest_path, doc["_id"]))
|
||||
self.generate(full_path, os.path.join(dest_path, doc["_id"]))
|
||||
|
||||
if counter is not None:
|
||||
counter.value += 1
|
||||
|
||||
def generate_image(self, path, dest_path):
|
||||
with open(path, "rb") as image_file:
|
||||
with Image.open(image_file) as image:
|
||||
|
||||
image.thumbnail(self.size, Image.BICUBIC)
|
||||
|
||||
canvas = Image.new("RGB", image.size, (255, 0, 255)) # todo get from config
|
||||
|
||||
if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info):
|
||||
|
||||
try:
|
||||
canvas.paste(image, mask=image.split()[-1])
|
||||
except ValueError:
|
||||
canvas.paste(image)
|
||||
else:
|
||||
canvas.paste(image)
|
||||
|
||||
canvas.save(dest_path, "JPEG", quality=85, optimize=True) # todo get qual from config
|
||||
canvas.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user