from glob import glob import os from shutil import copy2 import frontmatter from pathlib import Path from PIL import Image # from frontmatter import frontmatter for folder in glob('projects/*'): project = folder.replace('projects/', '') with open(os.path.join(folder,project+'.md'), 'r') as f: meta, content = frontmatter.parse(f.read()) if 'cover' in meta: cover = meta.get('cover') file = Path(f'static/img2/{cover}') if file.is_file(): try: with Image.open(file) as img: print(f'Ok im copying {cover} to {project}') f, e = os.path.splitext(cover) img.save(os.path.join(folder, f + '.jpg')) img.convert('RGB') img.thumbnail((256,256)) img.save(os.path.join(folder, 'thumb_' + cover), 'JPEG') except: print(f"Couldn't convert {cover}")