Skip to content

Instantly share code, notes, and snippets.

@eiszfuchs
Last active August 29, 2015 13:57
Show Gist options
  • Save eiszfuchs/9785488 to your computer and use it in GitHub Desktop.
Save eiszfuchs/9785488 to your computer and use it in GitHub Desktop.
function getPixel (x, y) {
var pixels = Picture.getPixel(x, y),
red = pixels[0],
green = pixels[1],
blue = pixels[2];
if (red > 128) {
red /= 2;
red += Math.random() * 0x33;
} else {
red *= 2;
}
if (green < 64 || green > 192) {
green = Math.random() * 0xff;
}
if (green < red) {
blue *= x / Picture.width;
}
return [red, green, blue];
}
#!/bin/bash
apt-get install scons libboost-python-dev libboost-all-dev
svn checkout http://v8.googlecode.com/svn/trunk/ v8
svn checkout http://pyv8.googlecode.com/svn/trunk/ pyv8
cd v8/
export V8_HOME=`pwd`
cd ../pyv8/
python setup.py build
python setup.py install
rm -R v8/
rm -R pyv8/
#!/bin/python
import PyV8
import png
from glob import glob
from math import floor
input_file = open("input.png", "rb")
input_source = png.Reader(file=input_file)
input_data = input_source.read()
width = input_data[0]
height = input_data[1]
pixel_data = list(input_data[2])
input_file.close()
def get_pixel(x, y):
red = pixel_data[y][(x * 3) + 0]
green = pixel_data[y][(x * 3) + 1]
blue = pixel_data[y][(x * 3) + 2]
return [red, green, blue]
def normalize (pixels):
red = pixels[0]
green = pixels[1]
blue = pixels[2]
red = min(max(floor(red), 0x00), 0xff)
green = min(max(floor(green), 0x00), 0xff)
blue = min(max(floor(blue), 0x00), 0xff)
return [int(red), int(green), int(blue)]
class Global(PyV8.JSClass):
def trace(self, message):
print(message)
Picture = {
"width": width,
"height": height,
"getPixel": get_pixel,
}
for plugin_filename in glob("./*.js"):
plugin_file = open(plugin_filename, "r")
plugin_source = plugin_file.read()
plugin_file.close()
filtered_pixel_data = pixel_data[:]
def set_pixel(x, y, pixels):
red, green, blue = normalize(pixels)
filtered_pixel_data[y][(x * 3) + 0] = red
filtered_pixel_data[y][(x * 3) + 1] = green
filtered_pixel_data[y][(x * 3) + 2] = blue
context = PyV8.JSContext(Global())
context.enter()
context.eval(plugin_source)
for y in range(height):
for x in range(width):
set_pixel(x, y, context.locals['getPixel'](x, y))
output_file = open(plugin_filename + ".png", "wb")
output_source = png.Writer(width, height)
output_source.write(output_file, pixel_data)
output_file.close()
@eiszfuchs
Copy link
Author

Dirty! \o/

@eiszfuchs
Copy link
Author

In Zeile 52 musst Du deepcopy benutzen! Oh Mann, Raphael!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment