Added multipage document support and document page transformation
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import tempfile
|
||||
|
||||
from documents.conf import settings as documents_settings
|
||||
from common.conf import settings as common_settings
|
||||
|
||||
TEMPORARY_DIRECTORY = documents_settings.TEMPORARY_DIRECTORY if documents_settings.TEMPORARY_DIRECTORY else tempfile.mkdtemp()
|
||||
TEMPORARY_DIRECTORY = common_settings.TEMPORARY_DIRECTORY if common_settings.TEMPORARY_DIRECTORY else tempfile.mkdtemp()
|
||||
|
||||
+22
-5
@@ -6,10 +6,8 @@ import shutil
|
||||
|
||||
from django.template.defaultfilters import slugify
|
||||
|
||||
|
||||
from documents.utils import from_descriptor_to_tempfile
|
||||
|
||||
from converter.conf.settings import CONVERT_PATH
|
||||
from converter.conf.settings import IDENTIFY_PATH
|
||||
from converter.conf.settings import OCR_OPTIONS
|
||||
from converter.conf.settings import DEFAULT_OPTIONS
|
||||
from converter.conf.settings import LOW_QUALITY_OPTIONS
|
||||
@@ -18,6 +16,7 @@ from converter.conf.settings import HIGH_QUALITY_OPTIONS
|
||||
#from converter.conf.settings import UNOCONV_PATH
|
||||
|
||||
from converter import TEMPORARY_DIRECTORY
|
||||
from utils import from_descriptor_to_tempfile
|
||||
|
||||
|
||||
QUALITY_DEFAULT = 'quality_default'
|
||||
@@ -73,6 +72,16 @@ def execute_unoconv(input_filepath, output_filepath, arguments=''):
|
||||
return (proc.wait(), proc.stderr.read())
|
||||
|
||||
|
||||
def execute_identify(input_filepath, arguments):
|
||||
command = []
|
||||
command.append(IDENTIFY_PATH)
|
||||
command.extend(shlex.split(str(arguments)))
|
||||
command.append(input_filepath)
|
||||
|
||||
proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return (proc.wait(), proc.stderr.read(), proc.stdout.read())
|
||||
|
||||
|
||||
def cache_cleanup(input_filepath, size, page=0, format='jpg'):
|
||||
filepath = create_image_cache_filename(input_filepath, size, page, format)
|
||||
try:
|
||||
@@ -126,7 +135,6 @@ def convert(input_filepath, size, quality=QUALITY_DEFAULT, cache=True, page=0, f
|
||||
try:
|
||||
input_arg = '%s[%s]' % (input_filepath, page)
|
||||
extra_options += ' -resize %s' % size
|
||||
print 'extra_options', extra_options
|
||||
status, error_string = execute_convert(input_arg, extra_options, '%s:%s' % (format, output_filepath), quality=quality)
|
||||
if status:
|
||||
errors = get_errors(error_string)
|
||||
@@ -136,7 +144,16 @@ def convert(input_filepath, size, quality=QUALITY_DEFAULT, cache=True, page=0, f
|
||||
if unoconv_output:
|
||||
cleanup(unoconv_output)
|
||||
return output_filepath
|
||||
|
||||
|
||||
|
||||
def get_page_count(input_filepath):
|
||||
try:
|
||||
status, error_string, output = execute_identify(input_filepath, '-format %n')
|
||||
if status:
|
||||
errors = get_errors(error_string)
|
||||
raise ConvertError(status, errors)
|
||||
finally:
|
||||
return int(output)
|
||||
|
||||
#TODO: slugify OCR_OPTIONS and add to file name to cache
|
||||
def convert_document_for_ocr(document, page=0, format='tif'):
|
||||
|
||||
@@ -5,6 +5,7 @@ ugettext = lambda s: s
|
||||
|
||||
|
||||
CONVERT_PATH = getattr(settings, 'CONVERTER_CONVERT_PATH', u'/usr/bin/convert')
|
||||
IDENTIFY_PATH = getattr(settings, 'CONVERTER_IDENTIFY_PATH', u'/usr/bin/identify')
|
||||
OCR_OPTIONS = getattr(settings, 'CONVERTER_OCR_OPTIONS', u'-colorspace Gray -depth 8 -resample 200x200')
|
||||
DEFAULT_OPTIONS = getattr(settings, 'CONVERTER_DEFAULT_OPTIONS', u'')
|
||||
LOW_QUALITY_OPTIONS = getattr(settings, 'CONVERTER_LOW_QUALITY_OPTIONS', u'')
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from converter import TEMPORARY_DIRECTORY
|
||||
|
||||
#http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
|
||||
def copyfile(source, dest, buffer_size=1024*1024):
|
||||
"""
|
||||
Copy a file from source to dest. source and dest
|
||||
can either be strings or any object with a read or
|
||||
write method, like StringIO for example.
|
||||
"""
|
||||
if not hasattr(source, 'read'):
|
||||
source = open(source, 'rb')
|
||||
if not hasattr(dest, 'write'):
|
||||
dest = open(dest, 'wb')
|
||||
|
||||
while 1:
|
||||
copy_buffer = source.read(buffer_size)
|
||||
if copy_buffer:
|
||||
dest.write(copy_buffer)
|
||||
else:
|
||||
break
|
||||
|
||||
source.close()
|
||||
dest.close()
|
||||
|
||||
|
||||
def from_descriptor_to_tempfile(input_descriptor, filename, buffer_size=1024*1024):
|
||||
path = os.path.join(TEMPORARY_DIRECTORY, filename)
|
||||
|
||||
output_descriptor = open(path, 'wb')
|
||||
|
||||
while 1:
|
||||
copy_buffer = input_descriptor.read(buffer_size)
|
||||
if copy_buffer:
|
||||
output_descriptor.write(copy_buffer)
|
||||
else:
|
||||
break
|
||||
|
||||
input_descriptor.close()
|
||||
output_descriptor.close()
|
||||
return path
|
||||
|
||||
|
||||
def from_descriptor_to_new_tempfile(input_descriptor, buffer_size=1024*1024):
|
||||
output_descriptor, tmp_filename = tempfile.mkstemp()
|
||||
|
||||
while 1:
|
||||
copy_buffer = input_descriptor.read(buffer_size)
|
||||
if copy_buffer:
|
||||
#output_descriptor.write(copy_buffer)
|
||||
os.write(output_descriptor, copy_buffer)
|
||||
else:
|
||||
break
|
||||
|
||||
input_descriptor.close()
|
||||
os.close(output_descriptor)
|
||||
return tmp_filename
|
||||
Reference in New Issue
Block a user