easy_thumbnails.utils: 89 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/utils.py

Stats: 0 executed, 78 missed, 11 excluded, 52 ignored

  1. import inspect
  2. import math
  3. import datetime
  4. from django.utils.functional import LazyObject
  5. try:
  6. from hashlib import md5 as md5_constructor
  7. except ImportError:
  8. from django.utils.hashcompat import md5_constructor
  9. try:
  10. from PIL import Image
  11. except ImportError:
  12. import Image
  13. try:
  14. from django.utils import timezone
  15. now = timezone.now
  16. def fromtimestamp(timestamp):
  17. dt = datetime.datetime.fromtimestamp(timestamp)
  18. if getattr(settings, 'USE_TZ', False):
  19. default_timezone = timezone.get_default_timezone()
  20. return timezone.make_aware(dt, default_timezone)
  21. return dt
  22. except ImportError:
  23. now = datetime.datetime.now
  24. fromtimestamp = datetime.datetime.fromtimestamp
  25. from easy_thumbnails.conf import settings
  26. def image_entropy(im):
  27. """
  28. Calculate the entropy of an image. Used for "smart cropping".
  29. """
  30. if not isinstance(im, Image.Image):
  31. # Can only deal with PIL images. Fall back to a constant entropy.
  32. return 0
  33. hist = im.histogram()
  34. hist_size = float(sum(hist))
  35. hist = [h / hist_size for h in hist]
  36. return -sum([p * math.log(p, 2) for p in hist if p != 0])
  37. def dynamic_import(import_string):
  38. """
  39. Dynamically import a module or object.
  40. """
  41. # Use rfind rather than rsplit for Python 2.3 compatibility.
  42. lastdot = import_string.rfind('.')
  43. if lastdot == -1:
  44. return __import__(import_string, {}, {}, [])
  45. module_name, attr = import_string[:lastdot], import_string[lastdot + 1:]
  46. parent_module = __import__(module_name, {}, {}, [attr])
  47. return getattr(parent_module, attr)
  48. def valid_processor_options(processors=None):
  49. """
  50. Return a list of unique valid options for a list of image processors
  51. (and/or source generators)
  52. """
  53. if processors is None:
  54. processors = [dynamic_import(p) for p in
  55. settings.THUMBNAIL_PROCESSORS +
  56. settings.THUMBNAIL_SOURCE_GENERATORS]
  57. valid_options = set(['size', 'quality'])
  58. for processor in processors:
  59. args = inspect.getargspec(processor)[0]
  60. # Add all arguments apart from the first (the source image).
  61. valid_options.update(args[1:])
  62. return list(valid_options)
  63. def is_storage_local(storage):
  64. """
  65. Check to see if a file storage is local.
  66. """
  67. try:
  68. storage.path('test')
  69. except NotImplementedError:
  70. return False
  71. return True
  72. def get_storage_hash(storage):
  73. """
  74. Return a hex string hash for a storage object (or string containing
  75. 'full.path.ClassName' referring to a storage object).
  76. """
  77. # If storage is wrapped in a lazy object we need to get the real thing.
  78. if isinstance(storage, LazyObject):
  79. if storage._wrapped is None:
  80. storage._setup()
  81. storage = storage._wrapped
  82. if not isinstance(storage, basestring):
  83. storage_cls = storage.__class__
  84. storage = '%s.%s' % (storage_cls.__module__, storage_cls.__name__)
  85. return md5_constructor(storage).hexdigest()
  86. def is_transparent(image):
  87. """
  88. Check to see if an image is transparent.
  89. """
  90. if not isinstance(image, Image.Image):
  91. # Can only deal with PIL images, fall back to the assumption that that
  92. # it's not transparent.
  93. return False
  94. return (image.mode in ('RGBA', 'LA') or
  95. (image.mode == 'P' and 'transparency' in image.info))
  96. def exif_orientation(im):
  97. """
  98. Rotate and/or flip an image to respect the image's EXIF orientation data.
  99. """
  100. try:
  101. exif = im._getexif()
  102. except (AttributeError, IndexError, KeyError, IOError):
  103. exif = None
  104. if exif:
  105. orientation = exif.get(0x0112)
  106. if orientation == 2:
  107. im = im.transpose(Image.FLIP_LEFT_RIGHT)
  108. elif orientation == 3:
  109. im = im.rotate(180)
  110. elif orientation == 4:
  111. im = im.transpose(Image.FLIP_TOP_BOTTOM)
  112. elif orientation == 5:
  113. im = im.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
  114. elif orientation == 6:
  115. im = im.rotate(-90)
  116. elif orientation == 7:
  117. im = im.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
  118. elif orientation == 8:
  119. im = im.rotate(90)
  120. return im