mirror of
https://github.com/simon987/Simple-Incremental-Search-Tool.git
synced 2025-12-14 07:39:05 +00:00
Added thumbnail generator
This commit is contained in:
37
thumbnail.py
37
thumbnail.py
@@ -1,14 +1,43 @@
|
||||
from PIL import Image
|
||||
import os
|
||||
from parsing import ContentMimeGuesser
|
||||
|
||||
|
||||
class ThumbnailGenerator:
|
||||
|
||||
def __init__(self, size):
|
||||
self.size = (size, size)
|
||||
self.mime_guesser = ContentMimeGuesser
|
||||
|
||||
def generate(self, path, dest_path):
|
||||
|
||||
image = Image.open(path)
|
||||
image.thumbnail(self.size, Image.BICUBIC)
|
||||
image.save(dest_path)
|
||||
image.close()
|
||||
try:
|
||||
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))
|
||||
|
||||
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)
|
||||
|
||||
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):
|
||||
|
||||
os.makedirs(dest_path, exist_ok=True)
|
||||
|
||||
for doc in docs:
|
||||
|
||||
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"]))
|
||||
Reference in New Issue
Block a user