mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-06-29 11:36:16 +00:00
Remove Thumbnails table
Daddy PyLint not happy with me
This commit is contained in:
parent
db20ce7919
commit
3331edf24d
7 changed files with 104 additions and 144 deletions
|
@ -1,18 +1,22 @@
|
|||
"""
|
||||
Calculate the contrast between two colors
|
||||
"""
|
||||
|
||||
|
||||
def contrast(background, light, dark, threshold = 0.179):
|
||||
"""
|
||||
Calculate the contrast between two colors
|
||||
background: tuple of (r, g, b) values
|
||||
light: color to use if the background is light
|
||||
dark: color to use if the background is dark
|
||||
threshold: the threshold to use for determining lightness, the default is w3 recommended
|
||||
"""
|
||||
r = background[0]
|
||||
g = background[1]
|
||||
b = background[2]
|
||||
red = background[0]
|
||||
green = background[1]
|
||||
blue = background[2]
|
||||
|
||||
# Calculate contrast
|
||||
uicolors = [r / 255, g / 255, b / 255]
|
||||
c = [col / 12.92 if col <= 0.03928 else ((col + 0.055) / 1.055) ** 2.4 for col in uicolors]
|
||||
l = (0.2126 * c[0]) + (0.7152 * c[1]) + (0.0722 * c[2])
|
||||
|
||||
return light if l > threshold else dark
|
||||
uicolors = [red / 255, green / 255, blue / 255]
|
||||
cont = [col / 12.92 if col <= 0.03928 else ((col + 0.055) / 1.055) ** 2.4 for col in uicolors]
|
||||
lightness = (0.2126 * cont[0]) + (0.7152 * cont[1]) + (0.0722 * cont[2])
|
||||
|
||||
return light if lightness > threshold else dark
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
"""
|
||||
Tools for generating images and thumbnails
|
||||
"""
|
||||
|
||||
import os
|
||||
import platformdirs
|
||||
from PIL import Image, ImageOps #, ImageFilter
|
||||
|
@ -8,67 +12,66 @@ CACHE_PATH = platformdirs.user_config_dir('onlylegs') + '/cache'
|
|||
UPLOAD_PATH = platformdirs.user_config_dir('onlylegs') + '/uploads'
|
||||
|
||||
|
||||
class ImageGenerator:
|
||||
def thumbnail(name, resolution, ext=None):
|
||||
"""
|
||||
Image thumbnail generator
|
||||
Uses PIL to generate a thumbnail of the image and saves it to the cache directory
|
||||
Name is the filename
|
||||
resolution: 400x400 or thumb, or any other resolution
|
||||
ext is the file extension of the image
|
||||
"""
|
||||
# Make image cache directory if it doesn't exist
|
||||
if not os.path.exists(CACHE_PATH):
|
||||
os.makedirs(CACHE_PATH)
|
||||
def generate_thumbnail(file_name, resolution, ext=None):
|
||||
"""
|
||||
Image thumbnail generator
|
||||
Uses PIL to generate a thumbnail of the image and saves it to the cache directory
|
||||
Name is the filename
|
||||
resolution: 400x400 or thumb, or any other resolution
|
||||
ext is the file extension of the image
|
||||
"""
|
||||
# Make image cache directory if it doesn't exist
|
||||
if not os.path.exists(CACHE_PATH):
|
||||
os.makedirs(CACHE_PATH)
|
||||
|
||||
# no sussy business
|
||||
name, name_ext = secure_filename(name).rsplit('.')
|
||||
if not ext:
|
||||
ext = name_ext.strip('.')
|
||||
|
||||
# PIL doesnt like jpg so we convert it to jpeg
|
||||
if ext.lower() == "jpg":
|
||||
ext = "jpeg"
|
||||
|
||||
# Set resolution based on preset resolutions
|
||||
if resolution in ['thumb', 'thumbnail']:
|
||||
res_x, res_y = (400, 400)
|
||||
elif resolution in ['prev', 'preview']:
|
||||
res_x, res_y = (1920, 1080)
|
||||
elif len(resolution.split('x')) == 2:
|
||||
res_x, res_y = resolution.split('x')
|
||||
else:
|
||||
return None
|
||||
|
||||
# If image has been already generated, return it from the cache
|
||||
if os.path.exists(os.path.join(CACHE_PATH, f'{name}_{res_x}x{res_y}.{ext}')):
|
||||
return os.path.join(CACHE_PATH, f'{name}_{res_x}x{res_y}.{ext}')
|
||||
|
||||
# Check if image exists in the uploads directory
|
||||
if not os.path.exists(os.path.join(UPLOAD_PATH, f'{name}.{name_ext}')):
|
||||
return None
|
||||
|
||||
# Open image and rotate it based on EXIF data and get ICC profile so colors are correct
|
||||
image = Image.open(os.path.join(UPLOAD_PATH, f'{name}.{name_ext}'))
|
||||
image_icc = image.info.get("icc_profile")
|
||||
img_x, img_y = image.size
|
||||
|
||||
# Resize image to fit the resolution
|
||||
image = ImageOps.exif_transpose(image)
|
||||
image.thumbnail((min(img_x, int(res_x)), min(img_y, int(res_y))), Image.ANTIALIAS)
|
||||
# no sussy business
|
||||
file_name, file_ext = secure_filename(file_name).rsplit('.')
|
||||
if not ext:
|
||||
ext = file_ext.strip('.')
|
||||
|
||||
# Save image to cache directory
|
||||
try:
|
||||
image.save(os.path.join(CACHE_PATH,f'{name}_{res_x}x{res_y}.{ext}'),
|
||||
icc_profile=image_icc)
|
||||
except OSError:
|
||||
# This usually happens when saving a JPEG with an ICC profile,
|
||||
# so we convert to RGB and try again
|
||||
image = image.convert('RGB')
|
||||
image.save(os.path.join(CACHE_PATH, f'{name}_{res_x}x{res_y}.{ext}'),
|
||||
icc_profile=image_icc)
|
||||
|
||||
# No need to keep the image in memory, learned the hard way
|
||||
image.close()
|
||||
|
||||
return os.path.join(CACHE_PATH, f'{name}_{res_x}x{res_y}.{ext}')
|
||||
# PIL doesnt like jpg so we convert it to jpeg
|
||||
if ext.lower() == "jpg":
|
||||
ext = "jpeg"
|
||||
|
||||
# Set resolution based on preset resolutions
|
||||
if resolution in ['thumb', 'thumbnail']:
|
||||
res_x, res_y = (400, 400)
|
||||
elif resolution in ['prev', 'preview']:
|
||||
res_x, res_y = (1920, 1080)
|
||||
elif len(resolution.split('x')) == 2:
|
||||
res_x, res_y = resolution.split('x')
|
||||
else:
|
||||
return None
|
||||
|
||||
# If image has been already generated, return it from the cache
|
||||
if os.path.exists(os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}')):
|
||||
return os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}')
|
||||
|
||||
# Check if image exists in the uploads directory
|
||||
if not os.path.exists(os.path.join(UPLOAD_PATH, f'{file_name}.{file_ext}')):
|
||||
return None
|
||||
|
||||
# Open image and rotate it based on EXIF data and get ICC profile so colors are correct
|
||||
image = Image.open(os.path.join(UPLOAD_PATH, f'{file_name}.{file_ext}'))
|
||||
image_icc = image.info.get("icc_profile")
|
||||
img_x, img_y = image.size
|
||||
|
||||
# Resize image to fit the resolution
|
||||
image = ImageOps.exif_transpose(image)
|
||||
image.thumbnail((min(img_x, int(res_x)), min(img_y, int(res_y))), Image.ANTIALIAS)
|
||||
|
||||
# Save image to cache directory
|
||||
try:
|
||||
image.save(os.path.join(CACHE_PATH,f'{file_name}_{res_x}x{res_y}.{ext}'),
|
||||
icc_profile=image_icc)
|
||||
except OSError:
|
||||
# This usually happens when saving a JPEG with an ICC profile,
|
||||
# so we convert to RGB and try again
|
||||
image = image.convert('RGB')
|
||||
image.save(os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}'),
|
||||
icc_profile=image_icc)
|
||||
|
||||
# No need to keep the image in memory, learned the hard way
|
||||
image.close()
|
||||
|
||||
return os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue