Skip to content

Instantly share code, notes, and snippets.

@andreasjansson
Created March 1, 2013 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andreasjansson/5065949 to your computer and use it in GitHub Desktop.
Save andreasjansson/5065949 to your computer and use it in GitHub Desktop.
def add_grain(im, depth=14):
width, height = im.size
image = scipy.misc.fromimage(im)
code = '''
#include <stdlib.h>
int x, y, i;
int depth2 = depth * 2;
for(i = 0; i < width * height * 3; i ++) {
int x = image[i];
x += rand() % depth2 - depth;
x = x > 255 ? 255 : (x < 0 ? 0 : x);
image[i] = x;
}
'''
scipy.weave.inline(code, ['depth', 'width', 'height', 'image'])
im = scipy.misc.toimage(image)
return im
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment