Skip to content

Instantly share code, notes, and snippets.

@sfan5
Created February 15, 2014 16:02
Show Gist options
  • Save sfan5/9021327 to your computer and use it in GitHub Desktop.
Save sfan5/9021327 to your computer and use it in GitHub Desktop.
Adds a bumpmap-like effect to images (works best on low-res [~16x16] textures).
#!/usr/bin/env python
import sys
from PIL import Image
FCT = 8
def diff2(a, b):
return abs(a-b)
def avg3(a, b, c, d=0):
return (a + b + c) / 3.0
def pxlmlower(ar, x, y, n):
v = list(ar[x, y])
v[0] = int((v[0] / 256.0) * float(256-n))
v[1] = int((v[1] / 256.0) * float(256-n))
v[2] = int((v[2] / 256.0) * float(256-n))
ar[x, y] = tuple(v)
if len(sys.argv) <= 2:
print("Usage: %s <input> <output>" % sys.argv[0])
else:
im = Image.open(sys.argv[1])
#if im.mode == 'P':
# im = im.convert(im.palette.mode)
#if not im.mode.startswith("RGB"):
im = im.convert("RGBA")
ims = im.size
imp = im.load()
out = im.resize((ims[0]*FCT, ims[1]*FCT))
outp = out.load()
for x in range(ims[0]):
for y in range(ims[1]):
pxl = imp[x, y]
if x > 0:
opx = imp[x-1, y]
n = diff2(avg3(*pxl), avg3(*opx))
xx = x*FCT
for yy in range(y*FCT, y*FCT+FCT):
pxlmlower(outp, xx, yy, n)
if y > 0:
opx = imp[x, y-1]
n = diff2(avg3(*pxl), avg3(*opx))
yy = y*FCT
for xx in range(x*FCT, x*FCT+FCT):
pxlmlower(outp, xx, yy, n)
if x < ims[0]-1:
opx = imp[x+1, y]
n = diff2(avg3(*pxl), avg3(*opx))
xx = x*FCT+FCT
for yy in range(y*FCT, y*FCT+FCT):
pxlmlower(outp, xx, yy, n)
if y < ims[1]-1:
opx = imp[x, y+1]
n = diff2(avg3(*pxl), avg3(*opx))
yy = y*FCT+FCT
for xx in range(x*FCT, x*FCT+FCT):
pxlmlower(outp, xx, yy, n)
out.save(sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment