- Some more comments

- Removed libmagic, wand can handle it better
pull/199/head
Steve Clement 2018-06-30 00:58:25 +08:00
parent c7c93b53e8
commit ef3837077e
1 changed files with 9 additions and 9 deletions

View File

@ -1,8 +1,9 @@
import json import json
import base64 import base64
from io import BytesIO from io import BytesIO
misperrors = {'error': 'Error'} misperrors = {'error': 'Error'}
userConfig = { }; userConfig = {};
inputSource = ['file'] inputSource = ['file']
@ -15,12 +16,6 @@ moduleconfig = []
def handler(q=False): def handler(q=False):
# try to import modules and return errors if module not found # try to import modules and return errors if module not found
try:
import magic
except ImportError:
misperrors['error'] = "Please pip(3) install magic"
return misperrors
try: try:
from PIL import Image from PIL import Image
except ImportError: except ImportError:
@ -45,13 +40,18 @@ def handler(q=False):
r = {'results': []} r = {'results': []}
request = json.loads(q) request = json.loads(q)
document = base64.b64decode(request["data"]) document = base64.b64decode(request["data"])
if magic.from_buffer(document, mime=True).split("/")[1] == 'pdf': # Eventually this could be replaced with wand.obj.format document = WImage(blob=document)
print("PDF Detected") if document.format == 'PDF':
with WImage(blob=document) as pdf: with WImage(blob=document) as pdf:
# Get number of pages
pages=len(pdf.sequence) pages=len(pdf.sequence)
print(f"PDF with {pages} page(s) detected")
# Create new image object where the height will be the number of pages. With huge PDFs this will overflow, break, consume silly memory etc…
img = WImage(width=pdf.width, height=pdf.height * pages) img = WImage(width=pdf.width, height=pdf.height * pages)
# Cycle through pages and stitch it together to one big file
for p in range(pages): for p in range(pages):
image = img.composite(pdf.sequence[p], top=pdf.height * p, left=0) image = img.composite(pdf.sequence[p], top=pdf.height * p, left=0)
# Create a png blob
image = img.make_blob('png') image = img.make_blob('png')
else: else:
image = document image = document