Skip to content

Instantly share code, notes, and snippets.

@tai2
Created February 20, 2013 19:45
Show Gist options
  • Save tai2/4998712 to your computer and use it in GitHub Desktop.
Save tai2/4998712 to your computer and use it in GitHub Desktop.
drop shadow using scaled rectangles.corner area representation is poor.
import sys
import os
import math
import Image
def create_shadow_image(image_size, shadow_size):
mergin = (shadow_size, shadow_size)
new_size = map(sum, zip(image_size, mergin, mergin))
im = Image.new('RGBA', new_size, 0xFFFFFFFF)
buf = im.load()
for i in range(1, shadow_size + 1):
offset = shadow_size - i
for y in range(image_size[1] + i * 2):
for x in range(image_size[0] + i * 2):
r, g, b, a = buf[x + offset, y + offset]
r = max(r - 0x08, 0)
g = max(g - 0x08, 0)
b = max(b - 0x08, 0)
buf[x + offset, y + offset] = (r, g, b, a)
print '\033[2K\033[30D%3.2f percent processed'%(float(i) * 100 / (shadow_size)),
sys.stdout.flush()
return im
def draw_shadowed_image(filename):
SHADOW_SIZE = 15
im = Image.open(filename)
new_im = create_shadow_image(im.size, SHADOW_SIZE)
offset = (SHADOW_SIZE,) * 4
base = (0, 0) + im.size
new_im.paste(im, map(sum, zip(base, offset)))
root, ext = os.path.splitext(filename)
new_im.save(root + '_shadowed' + ext)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.stderr.write('filename please!\n')
sys.exit(1)
draw_shadowed_image(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment