108 lines
3.9 KiB
Python
108 lines
3.9 KiB
Python
import os, sys
|
|
import logging
|
|
try:
|
|
from PIL import Image
|
|
import hitherdither
|
|
enabled = True
|
|
except:
|
|
logging.warning("Unable to load PIL or hitherdither, disabling thumbnailer")
|
|
enabled = False
|
|
|
|
DEFAULT_IMAGE_DIR = "images"
|
|
DEFAULT_DITHER_DIR = "dithers"
|
|
DEFAULT_THRESHOLD = [96, 96, 96] # this sets the contrast of the final image, rgb
|
|
DEFAULT_TRANSPARENCY= False
|
|
DEFAULT_TRANSPARENT_COLOR = [(125,125,125)]
|
|
DEFAULT_DITHER_PALETTE = [(25,25,25), (75,75,75),(125,125,125),(175,175,175),(225,225,225),(250,250,250)] # 6 tone palette\
|
|
DEFAULT_RESIZE_OUTPUT = True
|
|
DEFAULT_MAX_SIZE = (800,800)
|
|
|
|
def resize_image(img):
|
|
|
|
width, height = img.size
|
|
outcome = 0
|
|
|
|
if width > 800 or height > 800:
|
|
percentage = 0.5
|
|
resized_dimensions = (int(width * percentage), int(height * percentage))
|
|
img_resized = img.resize(resized_dimensions)
|
|
outcome = 0
|
|
else:
|
|
img_resized = img
|
|
outcome = 1
|
|
|
|
return img_resized, outcome
|
|
|
|
jpg_count = 0
|
|
png_count = 0
|
|
other_count = 0
|
|
|
|
for root, dirs, files in os.walk("/home/mastodon/live/public/system", topdown=False):
|
|
for name in files:
|
|
img_file_dir = os.path.join(root, name)
|
|
logging.info(img_file_dir)
|
|
file_size = int(os.path.getsize(img_file_dir))
|
|
|
|
# if img_file_dir[-3:] == 'jpg' and file_size > 10:
|
|
if file_size > 500 and img_file_dir[-3:] != 'mp4':
|
|
|
|
try:
|
|
|
|
if img_file_dir[-3:] == 'jpg' or img_file_dir[-4:] == 'jpeg':
|
|
img= Image.open(img_file_dir)
|
|
pal = list(img.getextrema())
|
|
logging.info('jpg pal: {}'.format(pal))
|
|
img_resized, outcome = resize_image(img)
|
|
if len(pal) > 2:
|
|
jpg_count = jpg_count + 1
|
|
palette = hitherdither.palette.Palette(DEFAULT_DITHER_PALETTE)
|
|
threshold = DEFAULT_THRESHOLD
|
|
img_dithered = hitherdither.ordered.bayer.bayer_dithering(img_resized, palette, threshold, order=8) #see hither dither documentation for different dithering algos
|
|
new_img = img_dithered.convert('L')
|
|
new_img.save(img_file_dir, optimize=True, quality=30)
|
|
else:
|
|
if outcome == 0:
|
|
img_resized.save(img_file_dir, optimize=True, quality=30)
|
|
else:
|
|
img_resized.save(img_file_dir)
|
|
|
|
else:
|
|
|
|
img= Image.open(img_file_dir).convert('RGB')
|
|
|
|
clrs = img.getcolors()
|
|
if clrs == None:
|
|
logging.info('None')
|
|
num_clrs = 11
|
|
else:
|
|
num_clrs = len(clrs)
|
|
logging.info(num_clrs)
|
|
|
|
if num_clrs > 10:
|
|
png_count = png_count + 1
|
|
palette = hitherdither.palette.Palette(DEFAULT_DITHER_PALETTE)
|
|
threshold = DEFAULT_THRESHOLD
|
|
img_dithered = hitherdither.ordered.bayer.bayer_dithering(img, palette, threshold, order=8) #see hither dither documentation for different dithering algos
|
|
|
|
img_resized, outcome = resize_image(img_dithered)
|
|
|
|
if img_file_dir[-3:] == 'png':
|
|
img_resized.save(img_file_dir, optimize=True)
|
|
|
|
else:
|
|
if img_resized.mode in ("RGBA", "P"):
|
|
img_resized = img_resized.convert("RGB")
|
|
img_resized.save(img_file_dir, optimize=True)
|
|
|
|
else:
|
|
logging.info('Already grayscale')
|
|
img.close()
|
|
|
|
except KeyboardInterrupt:
|
|
logging.info("Keyboard interrupt")
|
|
sys.exit()
|
|
|
|
except Exception as e:
|
|
logging.info("Exception encountered:", e)
|
|
|
|
print('jpg: {} png: {}'.format(jpg_count, png_count)) |