easy_thumbnails.engine: 58 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/easy_thumbnails/engine.py

Stats: 0 executed, 51 missed, 7 excluded, 32 ignored

  1. import os
  2. try:
  3. from cStringIO import StringIO
  4. except ImportError:
  5. from StringIO import StringIO
  6. try:
  7. from PIL import Image
  8. except ImportError:
  9. import Image
  10. from easy_thumbnails import utils
  11. from easy_thumbnails.conf import settings
  12. def _use_default_options(options):
  13. if not settings.THUMBNAIL_DEFAULT_OPTIONS:
  14. return options
  15. default_options = settings.THUMBNAIL_DEFAULT_OPTIONS.copy()
  16. default_options.update(options)
  17. return default_options
  18. def process_image(source, processor_options, processors=None):
  19. """
  20. Process a source PIL image through a series of image processors, returning
  21. the (potentially) altered image.
  22. """
  23. processor_options = _use_default_options(processor_options)
  24. if processors is None:
  25. processors = [utils.dynamic_import(name)
  26. for name in settings.THUMBNAIL_PROCESSORS]
  27. image = source
  28. for processor in processors:
  29. image = processor(image, **processor_options)
  30. return image
  31. def save_image(image, destination=None, filename=None, **options):
  32. """
  33. Save a PIL image.
  34. """
  35. if destination is None:
  36. destination = StringIO()
  37. filename = filename or ''
  38. format = Image.EXTENSION.get(os.path.splitext(filename)[1], 'JPEG')
  39. if format == 'JPEG':
  40. options.setdefault('quality', 85)
  41. try:
  42. image.save(destination, format=format, optimize=1, **options)
  43. except IOError:
  44. # Try again, without optimization (PIL can't optimize an image
  45. # larger than ImageFile.MAXBLOCK, which is 64k by default)
  46. pass
  47. image.save(destination, format=format, **options)
  48. if hasattr(destination, 'seek'):
  49. destination.seek(0)
  50. return destination
  51. def generate_source_image(source_file, processor_options, generators=None):
  52. """
  53. Processes a source ``File`` through a series of source generators, stopping
  54. once a generator returns an image.
  55. The return value is this image instance or ``None`` if no generators
  56. return an image.
  57. If the source file cannot be opened, it will be set to ``None`` and still
  58. passed to the generators.
  59. """
  60. processor_options = _use_default_options(processor_options)
  61. was_closed = source_file.closed
  62. if generators is None:
  63. generators = [utils.dynamic_import(name)
  64. for name in settings.THUMBNAIL_SOURCE_GENERATORS]
  65. try:
  66. source = source_file
  67. try:
  68. source.open()
  69. except Exception:
  70. source = None
  71. was_closed = False
  72. for generator in generators:
  73. image = generator(source, **processor_options)
  74. if image:
  75. return image
  76. finally:
  77. if was_closed:
  78. source_file.close()