Adding also images from "Image:" metadata entries

This commit is contained in:
Lucas Cimon 2017-12-03 13:51:52 +01:00
parent f4858e4246
commit a918f952c7
No known key found for this signature in database
GPG Key ID: 6AF36E0DE97FE852

View File

@ -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['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' 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: def gen_img(img_url):
print('Checking pelican plugin image_process.scale works OK on those imgs') if os.path.exists(img_url):
from PIL import Image return
from image_process import scale os.makedirs(os.path.dirname(img_url), exist_ok=True)
for ext, content in sorted(SMALLEST.items()): ext = img_url.split('.')[-1].lower()
print('- Testing {} img'.format(ext)) with open(img_url, 'wb') as img_file:
small_img_filename = 'img.{}'.format(ext) img_file.write(SMALLEST[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)
for md_file_path in sys.argv[1:]: for md_file_path in sys.argv[1:]:
with open(md_file_path) as md_file: 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'] img_url = img.attrib['src']
if img_url.startswith('http'): if img_url.startswith('http'):
continue continue
img_url = 'content/' + img_url gen_img('content/' + img_url)
if os.path.exists(img_url): # Adding also images from "Image:" metadata entries
for line in md_content.splitlines()[:6]:
if not line.startswith('Image: '):
continue continue
os.makedirs(os.path.dirname(img_url), exist_ok=True) gen_img('content/' + line.replace('Image: ', '').strip())
ext = img_url.split('.')[-1].lower()
with open(img_url, 'wb') as img_file: if not sys.argv[1:]:
img_file.write(SMALLEST[ext]) 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