From a918f952c7ea439cc44eadd7ba1cbe928b10319e Mon Sep 17 00:00:00 2001 From: Lucas Cimon Date: Sun, 3 Dec 2017 13:51:52 +0100 Subject: [PATCH] Adding also images from "Image:" metadata entries --- gen_imgs_from_mds.py | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/gen_imgs_from_mds.py b/gen_imgs_from_mds.py index 861d765..762de98 100755 --- a/gen_imgs_from_mds.py +++ b/gen_imgs_from_mds.py @@ -7,17 +7,13 @@ SMALLEST['gif'] = b'GIF89a\x01\x00\x01\x00\x00\x00\x00!\xf9\x04\x01\n\x00\x01\x0 SMALLEST['jpg'] = SMALLEST['jpeg'] = b'\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13\x0f\x10\x10\x10\xff\xc9\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00\xff\xcc\x00\x06\x00\x10\x10\x05\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xd2\xcf \xff\xd9' SMALLEST['png'] = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDATx\x9cc\xfa\x0f\x00\x01\x05\x01\x02\xcf\xa0.\xcd\x00\x00\x00\x00IEND\xaeB`\x82' -if len(sys.argv) == 1: - print('Checking pelican plugin image_process.scale works OK on those imgs') - from PIL import Image - from image_process import scale - for ext, content in sorted(SMALLEST.items()): - print('- Testing {} img'.format(ext)) - small_img_filename = 'img.{}'.format(ext) - with open(small_img_filename, 'wb') as small_img: - small_img.write(content) - scale(Image.open(small_img_filename), '300', '300', False, False) - sys.exit(0) +def gen_img(img_url): + if os.path.exists(img_url): + return + os.makedirs(os.path.dirname(img_url), exist_ok=True) + ext = img_url.split('.')[-1].lower() + with open(img_url, 'wb') as img_file: + img_file.write(SMALLEST[ext]) for md_file_path in sys.argv[1:]: with open(md_file_path) as md_file: @@ -28,10 +24,20 @@ for md_file_path in sys.argv[1:]: img_url = img.attrib['src'] if img_url.startswith('http'): continue - img_url = 'content/' + img_url - if os.path.exists(img_url): + gen_img('content/' + img_url) + # Adding also images from "Image:" metadata entries + for line in md_content.splitlines()[:6]: + if not line.startswith('Image: '): continue - os.makedirs(os.path.dirname(img_url), exist_ok=True) - ext = img_url.split('.')[-1].lower() - with open(img_url, 'wb') as img_file: - img_file.write(SMALLEST[ext]) + gen_img('content/' + line.replace('Image: ', '').strip()) + +if not sys.argv[1:]: + print('Checking that pelican plugin image_process.scale works OK on those imgs') + from PIL import Image + from image_process import scale + for ext, content in sorted(SMALLEST.items()): + print('- Testing {} img'.format(ext)) + small_img_filename = 'img.{}'.format(ext) + with open(small_img_filename, 'wb') as small_img: + small_img.write(content) + scale(Image.open(small_img_filename), '300', '300', False, False) # raise an exception if img.getbbox() returns None