Created
February 3, 2021 17:09
-
-
Save villares/4679adf5a786528fc2bf9eb579d95b55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import listdir | |
from os.path import isfile, join | |
def get_image_names(base, folder, word=None): | |
""" | |
Return a list of image names from a directory | |
named folder at base/folder by default only | |
if name contains the folder name. | |
""" | |
word = word or folder | |
contents = listdir(join(base, folder)) | |
image_files = [f for f in contents | |
if is_img_ext(f) | |
and word in f] | |
return image_files | |
def is_img_ext(file_name): | |
""" | |
Return True if file_name ends with | |
one of the valid_ext extensions. | |
""" | |
ext = file_name.split('.')[-1] | |
valid_ext = ('jpg', | |
'png', | |
'jpeg', | |
'gif', | |
'tif', | |
'tga', | |
#'svg', | |
) | |
return ext.lower() in valid_ext | |
def remove_transparency(im, bg_colour=(255, 255, 255)): | |
from PIL import Image | |
# Only process if image has transparency (http://stackoverflow.com/a/1963146) | |
if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info): | |
# Need to convert to RGBA if LA format due to a bug in PIL (http://stackoverflow.com/a/1963146) | |
alpha = im.convert('RGBA').split()[-1] | |
# Create a new background image of our matt color. | |
# Must be RGBA because paste requires both images have the same format | |
# (http://stackoverflow.com/a/8720632 and http://stackoverflow.com/a/9459208) | |
bg = Image.new("RGBA", im.size, bg_colour + (255,)) | |
bg.paste(im, mask=alpha) | |
return bg | |
else: | |
return im |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
from os import listdir | |
from os.path import isfile, join | |
from PIL import Image, GifImagePlugin | |
from helpers import get_image_names, remove_transparency | |
YEAR = "2021" | |
base_path = "/home/villares/GitHub/sketch-a-day/" | |
year_path = join(base_path,YEAR) | |
folders = listdir(year_path) | |
images = [] | |
for folder in sorted(folders): | |
folder_path = join(year_path, folder) | |
f_images = get_image_names(year_path, folder) | |
if f_images: | |
img_path = join(folder_path, f_images[0]) | |
images.append(Image.open(img_path)) | |
thumb_size = (800, 400) | |
n = None # degub with 5 | |
thumbs = [] | |
for img in images[:n]: | |
if img.format == 'GIF' and img.is_animated: | |
p = img.getpalette() | |
img.seek(img.n_frames // 2) | |
if not img.getpalette(): | |
im.putpalette(p) | |
img = img.convert('RGBA') | |
print('gif') | |
img.thumbnail(thumb_size, Image.BICUBIC) | |
img = remove_transparency(img) | |
bg = Image.new('RGB', thumb_size, (0, 0, 0)) | |
print(img.size) | |
iw, bw = img.size[0], bg.size[0] | |
if iw < bw: | |
x = (bw - iw) // 2 | |
else: | |
x = 0 | |
ih, bh = img.size[1], bg.size[1] | |
if ih < bh: | |
y = (bh - ih) // 2 | |
else: | |
y = 0 | |
bg.paste(img, (x, y)) | |
thumbs.append(bg) | |
thumbs[0].save('export{}.gif'.format(YEAR), | |
save_all=True, append_images=thumbs, | |
optimize=True, | |
duration=500, | |
loop=0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment