filer.thumbnail_processors: 68 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/filer/thumbnail_processors.py

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

  1. #-*- coding: utf-8 -*-
  2. import re
  3. try:
  4. from PIL import Image
  5. from PIL import ImageDraw
  6. except ImportError:
  7. try:
  8. import Image
  9. import ImageDraw
  10. except ImportError:
  11. raise ImportError("The Python Imaging Library was not found.")
  12. from easy_thumbnails import processors
  13. from filer.settings import FILER_SUBJECT_LOCATION_IMAGE_DEBUG
  14. RE_SUBJECT_LOCATION = re.compile(r'^(\d+),(\d+)$')
  15. def normalize_subject_location(subject_location):
  16. if subject_location:
  17. if isinstance(subject_location, basestring):
  18. m = RE_SUBJECT_LOCATION.match(subject_location)
  19. if m:
  20. return (int(m.group(1)), int(m.group(2)))
  21. else:
  22. try:
  23. return (int(subject_location[0]), int(subject_location[1]))
  24. except (TypeError, ValueError):
  25. pass
  26. return False
  27. def scale_and_crop_with_subject_location(im, size, subject_location=False,
  28. crop=False, upscale=False, **kwargs):
  29. """
  30. Like ``easy_thumbnails.processors.scale_and_crop``, but will use the
  31. coordinates in ``subject_location`` to make sure that that part of the
  32. image is in the center or at least somewhere on the cropped image.
  33. Please not that this does *not* work correctly if the image has been
  34. resized by a previous processor (e.g ``autocrop``).
  35. ``crop`` needs to be set for this to work, but any special cropping
  36. parameters will be ignored.
  37. """
  38. subject_location = normalize_subject_location(subject_location)
  39. if not (subject_location and crop):
  40. # use the normal scale_and_crop
  41. return processors.scale_and_crop(im, size, crop=crop,
  42. upscale=upscale, **kwargs)
  43. # for here on we have a subject_location and cropping is on
  44. # --snip-- this is a copy and paste of the first few
  45. # lines of ``scale_and_crop``
  46. source_x, source_y = [float(v) for v in im.size]
  47. target_x, target_y = [float(v) for v in size]
  48. if crop or not target_x or not target_y:
  49. scale = max(target_x / source_x, target_y / source_y)
  50. else:
  51. scale = min(target_x / source_x, target_y / source_y)
  52. # Handle one-dimensional targets.
  53. if not target_x:
  54. target_x = source_x * scale
  55. elif not target_y:
  56. target_y = source_y * scale
  57. if scale < 1.0 or (scale > 1.0 and upscale):
  58. im = im.resize((int(source_x * scale), int(source_y * scale)),
  59. resample=Image.ANTIALIAS)
  60. # --endsnip-- begin real code
  61. # ===============================
  62. # subject location aware cropping
  63. # ===============================
  64. # res_x, res_y: the resolution of the possibly already resized image
  65. res_x, res_y = [float(v) for v in im.size]
  66. # subj_x, subj_y: the position of the subject (maybe already re-scaled)
  67. subj_x = res_x * float(subject_location[0]) / source_x
  68. subj_y = res_y * float(subject_location[1]) / source_y
  69. ex = (res_x - min(res_x, target_x)) / 2
  70. ey = (res_y - min(res_y, target_y)) / 2
  71. fx, fy = res_x - ex, res_y - ey
  72. # box_width, box_height: dimensions of the target image
  73. box_width, box_height = fx - ex, fy - ey
  74. # try putting the box in the center around the subject point
  75. # (this will be partially outside of the image in most cases)
  76. tex, tey = subj_x - (box_width / 2), subj_y - (box_height / 2)
  77. tfx, tfy = subj_x + (box_width / 2), subj_y + (box_height / 2)
  78. if tex < 0:
  79. # its out of the img to the left, move both to the right until tex is 0
  80. tfx = tfx - tex # tex is negative!
  81. tex = 0
  82. elif tfx > res_x:
  83. # its out of the img to the right
  84. tex = tex - (tfx - res_x)
  85. tfx = res_x
  86. if tey < 0:
  87. # its out of the img to the top, move both to the bottom until tey is 0
  88. tfy = tfy - tey # tey is negative!)
  89. tey = 0
  90. elif tfy > res_y:
  91. # its out of the img to the bottom
  92. tey = tey - (tfy - res_y)
  93. tfy = res_y
  94. if ex or ey:
  95. crop_box = ((int(tex), int(tey), int(tfx), int(tfy)))
  96. if FILER_SUBJECT_LOCATION_IMAGE_DEBUG:
  97. # draw elipse on focal point for Debugging
  98. draw = ImageDraw.Draw(im)
  99. esize = 10
  100. draw.ellipse(((subj_x - esize, subj_y - esize),
  101. (subj_x + esize, subj_y + esize)), outline="#FF0000")
  102. im = im.crop(crop_box)
  103. return im