tianyi-wp-theme/manifest/manifest.py

72 lines
1.9 KiB
Python
Raw Normal View History

2019-11-29 04:11:37 +00:00
# coding=utf-8
2019-11-28 09:41:44 +00:00
'''
Created on Apr 23, 2018
Desc: Webp convertor
@author: Mashiro https://2heng.xin
'''
import os
import sys
import json
import hashlib
from PIL import Image
class Single(object):
def __init__(self, file, manifest):
self.file = file
self.mani = manifest
def hash(self):
hasher = hashlib.md5()
with open('gallary/' + self.file, 'rb') as afile:
buf = afile.read()
hasher.update(buf)
self.hash = hasher.hexdigest()
self.jpeg = 'jpeg/' + self.hash + '.jpeg'
self.webp = 'webp/' + self.hash + '.webp'
2019-11-29 04:11:37 +00:00
self.th_jpeg = 'jpeg/' + self.hash + '.th.jpeg'
self.th_webp = 'webp/' + self.hash + '.th.webp'
2019-11-28 09:41:44 +00:00
def optimize(self):
im = Image.open('gallary/' + self.file).convert('RGB')
im.save(self.jpeg, 'JPEG') # todo: TinyPNG API
im.save(self.webp, 'WEBP')
2019-11-29 04:11:37 +00:00
def thumbnail(self):
im = Image.open('gallary/' + self.file).convert('RGB')
im.thumbnail((450, 300))
im.save(self.th_jpeg, 'JPEG') # todo: TinyPNG API
im.save(self.th_webp, 'WEBP')
2019-11-28 09:41:44 +00:00
def manifest(self):
self.mani[self.hash] = {
'source': self.file,
2019-11-29 04:11:37 +00:00
'jpeg': ['jpeg/' + self.hash + '.jpeg', 'jpeg/' + self.hash + '.th.jpeg'],
'webp': ['webp/' + self.hash + '.webp', 'webp/' + self.hash + '.th.webp']
2019-11-28 09:41:44 +00:00
}
def main(self):
self.hash()
# if os.path.exists(self.jpeg) and os.path.exists(self.webp):
self.optimize()
2019-11-29 04:11:37 +00:00
self.thumbnail()
2019-11-28 09:41:44 +00:00
self.manifest()
return self.mani
def main():
onlyfiles = [f for f in os.listdir('gallary') if os.path.isfile(os.path.join('gallary', f))]
id = 1
Manifest = {}
for f in onlyfiles:
worker = Single(f, Manifest)
Manifest = worker.main()
print(str(id) + '/' + str(len(onlyfiles)))
id += 1
with open('manifest.json', 'w+') as json_file:
json.dump(Manifest, json_file)
if __name__ == '__main__':
main()
key = input('`manifest.json` saved. Press any key to quit.')
quit()