Initial commit of the converter image transformation refactor

This commit is contained in:
Roberto Rosario
2011-07-15 06:16:14 -04:00
parent 358216fac5
commit 743ae0fce0
17 changed files with 303 additions and 114 deletions
+2 -3
View File
@@ -21,9 +21,6 @@ class ConverterBase(object):
def get_available_transformations(self):
raise NotImplementedError("Your %s class has not defined a get_available_transformations() method, which is required." % self.__class__.__name__)
def get_available_transformations_labels(self):
return ([(name, data['label']) for name, data in self.get_available_transformations().items()])
def get_transformation_string(self, transformation_list):
transformations = []
warnings = []
@@ -41,3 +38,5 @@ class ConverterBase(object):
return u' '.join(transformations), warnings
def get_page_count(self):
raise NotImplementedError("Your %s class has not defined a get_page_count() method, which is required." % self.__class__.__name__)
+48 -9
View File
@@ -8,6 +8,10 @@ from converter.conf.settings import GM_SETTINGS
from converter.literals import QUALITY_DEFAULT, QUALITY_SETTINGS
from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
from converter.backends import ConverterBase
from converter.literals import TRANSFORMATION_RESIZE, \
TRANSFORMATION_ROTATE, TRANSFORMATION_DENSITY, \
TRANSFORMATION_ZOOM
from converter.literals import DIMENSION_SEPARATOR
CONVERTER_ERROR_STRING_NO_DECODER = u'No decode delegate for this image format'
CONVERTER_ERROR_STARTS_WITH = u'starts with'
@@ -28,7 +32,29 @@ class ConverterClass(ConverterBase):
return proc.stdout.read()
def convert_file(self, input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
def convert_file(self, input_filepath, output_filepath, transformations=None, quality=QUALITY_DEFAULT):
arguments = []
if transformations:
for transformation in transformations:
if transformation['transformation'] == TRANSFORMATION_RESIZE:
dimensions = []
dimensions.append(unicode(transformation['arguments']['width']))
if 'height' in transformation['arguments']:
dimensions.append(unicode(transformation['arguments']['height']))
arguments.append(u'-resize')
arguments.append(u'%s' % DIMENSION_SEPARATOR.join(dimensions))
elif transformation['transformation'] == TRANSFORMATION_ZOOM:
arguments.append(u'-resize')
arguments.append(u'%d%%' % transformation['arguments']['zoom'])
elif transformation['transformation'] == TRANSFORMATION_ROTATE:
arguments.append(u'-rotate')
arguments.append(u'%s' % transformation['arguments']['degrees'])
print 'arguments: %s' % arguments
#if format == u'jpg':
# extra_options += u' -quality 85'
command = []
command.append(unicode(GM_PATH))
command.append(u'convert')
@@ -36,8 +62,9 @@ class ConverterClass(ConverterBase):
command.extend(unicode(GM_SETTINGS).split())
command.append(unicode(input_filepath))
if arguments:
command.extend(unicode(arguments).split())
command.extend(arguments)
command.append(unicode(output_filepath))
print 'command: %s' % command
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
if return_code != 0:
@@ -76,10 +103,22 @@ class ConverterClass(ConverterBase):
def get_available_transformations(self):
return {
'rotate': {
'label': _(u'Rotate [degrees]'),
'arguments': [{'name': 'degrees'}],
'command_line': u'-rotate %(degrees)d'
}
}
return [
TRANSFORMATION_RESIZE, TRANSFORMATION_ROTATE, \
TRANSFORMATION_DENSITY, TRANSFORMATION_ZOOM
]
def get_page_count(self, input_filepath):
try:
return len(self.identify_file(unicode(input_filepath)).splitlines())
except:
#TODO: send to other page number identifying program
return 1
def _get_transformation_string():
pass
#'command_line': u'-rotate %(degrees)d'
# }
#}
+18 -8
View File
@@ -9,7 +9,10 @@ from converter.api import QUALITY_DEFAULT, QUALITY_SETTINGS
from converter.exceptions import ConvertError, UnknownFormat, \
IdentifyError
from converter.backends import ConverterBase
from converter.literals import TRANSFORMATION_RESIZE, \
TRANSFORMATION_ROTATE, TRANSFORMATION_DENSITY, \
TRANSFORMATION_ZOOM
CONVERTER_ERROR_STRING_NO_DECODER = u'no decode delegate for this image format'
@@ -29,6 +32,8 @@ class ConverterClass(ConverterBase):
def convert_file(self, input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
#if format == u'jpg':
# extra_options += u' -quality 85'
command = []
command.append(unicode(IM_CONVERT_PATH))
command.extend(unicode(QUALITY_SETTINGS[quality]).split())
@@ -73,10 +78,15 @@ class ConverterClass(ConverterBase):
def get_available_transformations(self):
return {
'rotate': {
'label': _(u'Rotate [degrees]'),
'arguments': [{'name': 'degrees'}],
'command_line': u'-rotate %(degrees)d'
}
}
return [
TRANSFORMATION_RESIZE, TRANSFORMATION_ROTATE, \
TRANSFORMATION_DENSITY, TRANSFORMATION_ZOOM
]
def get_page_count(self, input_filepath):
try:
return len(self.identify_file(unicode(input_filepath)).splitlines())
except:
#TODO: send to other page number identifying program
return 1
@@ -0,0 +1,3 @@
from PIL import Image
Image.init()
+80
View File
@@ -0,0 +1,80 @@
from PIL import Image
from django.utils.translation import ugettext_lazy as _
from converter.literals import QUALITY_DEFAULT, QUALITY_SETTINGS
from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
from converter.backends import ConverterBase
from converter.literals import TRANSFORMATION_RESIZE, \
TRANSFORMATION_ROTATE
class ConverterClass(ConverterBase):
def identify_file(self, input_filepath, arguments=None):
pass
def get_page_count(self, input_filepath):
page_count = 1
im = Image.open(input_filepath)
try:
while 1:
im.seek(im.tell()+1)
page_count += 1
# do something to im
except EOFError:
pass # end of sequence
return page_count
def convert_file(self, input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
im = Image.open(input_filepath)
outfile, format = output_filepath.split(u':')
im.save(outfile, format)
'''
command = []
command.append(unicode(GM_PATH))
command.append(u'convert')
command.extend(unicode(QUALITY_SETTINGS[quality]).split())
command.extend(unicode(GM_SETTINGS).split())
command.append(unicode(input_filepath))
if arguments:
command.extend(unicode(arguments).split())
command.append(unicode(output_filepath))
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
if return_code != 0:
#Got an error from convert program
error_line = proc.stderr.readline()
if (CONVERTER_ERROR_STRING_NO_DECODER in error_line) or (CONVERTER_ERROR_STARTS_WITH in error_line):
#Try to determine from error message which class of error is it
raise UnknownFormat
else:
raise ConvertError(error_line)
'''
def get_format_list(self):
"""
Introspect PIL's internal registry to obtain a list of the
supported file types
"""
formats = []
for format_name in Image.ID:
formats.append((format_name, u''))
return formats
def get_available_transformations(self):
return [
TRANSFORMATION_RESIZE, TRANSFORMATION_ROTATE
]
def get_page_count(self, input_filepath):
try:
return len(self.identify_file(unicode(input_filepath)).splitlines())
except:
#TODO: send to other page number identifying program
return 1