Created
March 1, 2013 16:45
-
-
Save andreasjansson/5065949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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