- added wand requirement

- fixed missing return png byte-stream
- move module import to handler to catch and  report errorz
pull/199/head
Steve Clement 2018-06-28 23:20:38 +08:00
parent 7885017981
commit 60a3fbe282
2 changed files with 32 additions and 10 deletions

View File

@ -14,6 +14,7 @@ git+https://github.com/MISP/PyMISP.git#egg=pymisp
git+https://github.com/sebdraven/pyonyphe#egg=pyonyphe git+https://github.com/sebdraven/pyonyphe#egg=pyonyphe
pillow pillow
pytesseract pytesseract
wand
SPARQLWrapper SPARQLWrapper
domaintools_api domaintools_api
pygeoip pygeoip

View File

@ -1,12 +1,5 @@
import json import json
import base64 import base64
import magic
from PIL import Image
from wand.image import Image as WImage
from pytesseract import image_to_string
from io import BytesIO from io import BytesIO
misperrors = {'error': 'Error'} misperrors = {'error': 'Error'}
userConfig = { }; userConfig = { };
@ -21,6 +14,32 @@ moduleconfig = []
def handler(q=False): def handler(q=False):
# 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:
from PIL import Image
except ImportError:
misperrors['error'] = "Please pip(3) install pillow"
return misperrors
try:
# Official ImageMagick module
from wand.image import Image as WImage
except ImportError:
misperrors['error'] = "Please pip(3) install wand"
return misperrors
try:
from pytesseract import image_to_string
except ImportError:
misperrors['error'] = "Please pip(3) install pytesseract"
return misperrors
if q is False: if q is False:
return False return False
r = {'results': []} r = {'results': []}
@ -32,14 +51,16 @@ def handler(q=False):
pages=len(pdf.sequence) pages=len(pdf.sequence)
img = WImage(width=pdf.width, height=pdf.height * pages) img = WImage(width=pdf.width, height=pdf.height * pages)
for p in range(pages): for p in range(pages):
img.composite(pdf.sequence[p], top=pdf.height * p, left=0) image = img.composite(pdf.sequence[p], top=pdf.height * p, left=0)
image = document image = img.make_blob('png')
else:
image = document
image_file = BytesIO(image) image_file = BytesIO(image)
image_file.seek(0) image_file.seek(0)
try: try:
im = WImage(blob=image_file) im = Image.open(image_file)
except IOError: except IOError:
misperrors['error'] = "Corrupt or not an image file." misperrors['error'] = "Corrupt or not an image file."
return misperrors return misperrors