argos/docsbox-master/docsbox/docs/utils.py

41 lines
1.3 KiB
Python
Raw Normal View History

2018-07-11 15:47:36 +02:00
import os
import zipfile
from wand.image import Image
from docsbox import app
def make_zip_archive(uuid, tmp_dir):
"""
Creates ZIP archive from given @tmp_dir.
"""
zipname = "{0}.zip".format(uuid)
result_path = os.path.join(app.config["MEDIA_PATH"],
zipname)
result_url = os.path.join(app.config["MEDIA_URL"],
zipname)
with zipfile.ZipFile(result_path, "w") as output:
for dirname, subdirs, files in os.walk(tmp_dir):
for filename in files:
path = os.path.join(dirname, filename)
output.write(path, path.split(tmp_dir)[1])
return result_path, result_url
def make_thumbnails(image, tmp_dir, size):
thumbnails_folder = os.path.join(tmp_dir, "thumbnails/")
os.mkdir(thumbnails_folder)
(width, height) = size
for index, page in enumerate(image.sequence):
with Image(page) as page:
filename = os.path.join(thumbnails_folder, "{0}.png".format(index))
page.resize(width, height)
if app.config["THUMBNAILS_QUANTIZE"]:
page.quantize(app.config["THUMBNAILS_QUANTIZE_COLORS"],
app.config["THUMBNAILS_QUANTIZE_COLORSPACE"], 0, True, True)
page.save(filename=filename)
else:
image.close()
return index