reflection.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """ Function for generating web 2.0 style image reflection effects.
  2. Copyright (c) 2007, Justin C. Driscoll
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. Neither the name of reflection.py nor the names of its contributors may be used
  12. to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. """
  25. try:
  26. import Image
  27. import ImageColor
  28. except ImportError:
  29. try:
  30. from PIL import Image
  31. from PIL import ImageColor
  32. except ImportError:
  33. raise ImportError("The Python Imaging Library was not found.")
  34. def add_reflection(im, bgcolor="#00000", amount=0.4, opacity=0.6):
  35. """ Returns the supplied PIL Image (im) with a reflection effect
  36. bgcolor The background color of the reflection gradient
  37. amount The height of the reflection as a percentage of the orignal image
  38. opacity The initial opacity of the reflection gradient
  39. Originally written for the Photologue image management system for Django
  40. and Based on the original concept by Bernd Schlapsi
  41. """
  42. # convert bgcolor string to rgb value
  43. background_color = ImageColor.getrgb(bgcolor)
  44. # copy orignial image and flip the orientation
  45. reflection = im.copy().transpose(Image.FLIP_TOP_BOTTOM)
  46. # create a new image filled with the bgcolor the same size
  47. background = Image.new("RGB", im.size, background_color)
  48. # calculate our alpha mask
  49. start = int(255 - (255 * opacity)) # The start of our gradient
  50. steps = int(255 * amount) # the number of intermedite values
  51. increment = (255 - start) / float(steps)
  52. mask = Image.new("L", (1, 255))
  53. for y in range(255):
  54. if y < steps:
  55. val = int(y * increment + start)
  56. else:
  57. val = 255
  58. mask.putpixel((0, y), val)
  59. alpha_mask = mask.resize(im.size)
  60. # merge the reflection onto our background color using the alpha mask
  61. reflection = Image.composite(background, reflection, alpha_mask)
  62. # crop the reflection
  63. reflection_height = int(im.size[1] * amount)
  64. reflection = reflection.crop((0, 0, im.size[0], reflection_height))
  65. # create new image sized to hold both the original image and the reflection
  66. composite = Image.new(
  67. "RGB", (im.size[0], im.size[1] + reflection_height), background_color
  68. )
  69. # paste the orignal image and the reflection into the composite image
  70. composite.paste(im, (0, 0))
  71. composite.paste(reflection, (0, im.size[1]))
  72. # return the image complete with reflection effect
  73. return composite