Skip to content

Instantly share code, notes, and snippets.

@andrusenn
Last active October 11, 2018 03:34
Show Gist options
  • Save andrusenn/8045f9465c371dd563ada685b09dbfe5 to your computer and use it in GitHub Desktop.
Save andrusenn/8045f9465c371dd563ada685b09dbfe5 to your computer and use it in GitHub Desktop.
Generative and Glitch
/*
En este sketch hay algunas técnicas utilizadas en varios procesos
In this sketch, there are some techniques used in various process
Andrés Senn
2016-2018
*/
int cell = 5;
ArrayList<ImgBlock> imgsb;
PVector pv;
ImageProcess ip;
void setup() {
size(800, 800);
smooth();
background(65);
// Formas / Shapes
fill(255);
noStroke();
ellipse(width*0.5, height*0.5, 800, 800);
// Fractales / Fractals
// https://github.com/shiffman/The-Nature-of-Code-Examples/blob/master/chp08_fractals/NOC_8_06_Tree/NOC_8_06_Tree.pde
for (int i = 0; i < 40; i++) {
pushMatrix();
translate(width/2+int(random(-10, 10)), height/2+int(random(-10, 10)));
rotate(random(PI*2));
rama(int(random(50, 80)), 3);
popMatrix();
}
pv = new PVector(0, 0);
ip = new ImageProcess();
imgsb = new ArrayList<ImgBlock>();
// Corta la imagen / Cut image
for (int x = 0; x < width; x+=random(5, 10)) {
for (int y = 0; y < height; y+=random(5, 10)) {
float w = int(random(5, 50));
float h = int(random(5, 10));
ImgBlock ib = new ImgBlock(get(x, y, int(w), int(h)), x, y);
imgsb.add(ib);
}
}
}
void draw() {
noiseSeed(0);
// Pincel / Brush
// Tecla G aplica Glitch / G key apply glitch
if (mousePressed) {
pv = new PVector(mouseX, mouseY);
int bsx = int(random(2, 100));
int bsy = int(random(2, 100));
for (int i = 0; i < imgsb.size(); i++) {
ImgBlock ib = imgsb.get(i);
if (ib.x > pv.x && ib.y > pv.y && ib.x < pv.x+bsx && ib.y < pv.y+bsy) {
float r1 = random(-4, 4);
float r2 = random(-4, 4);
ib.x += r1;
ib.y += r2;
noStroke();
fill(0, 10);
rect(ib.x+10, ib.y+10, ib.img.width, ib.img.height);
stroke(0, 60*noise(ib.x*0.01, ib.y*0.01));
strokeWeight(0.1);
line(ib.x, ib.y, ib.x, ib.y+(100*noise(ib.x*0.01, ib.y*0.01)));
line(ib.x, ib.y, ib.x+(200*noise(ib.x*0.01, ib.y*0.01)), ib.y);
set(ib.x, ib.y, ib.img);
}
if ( keyPressed && key == 'g') {
if (ib.x > pv.x && ib.y > pv.y && ib.x < pv.x+bsx && ib.y < pv.y+bsy) {
PImage im = ip.invert(get(ib.x, ib.y, bsx, bsy));
set(ib.x, ib.y, im);
}
}
}
}
}
void keyPressed() {
if (key == 's') {
String n = year()+""+month()+""+day()+""+hour()+""+minute()+""+second();
save("save/"+n+".jpg");
exit();
}
}
class ImgBlock {
int x;
int y;
PImage img;
ImgBlock(PImage _img, int _x, int _y) {
img = _img;
x = _x;
y = _y;
}
}
void rama(float len, float s) {
strokeWeight(s);
if (random(1) < 0.4) {
stroke(255);
} else {
stroke(0);
}
line(0, 0, 0, -len);
translate(0, -len);
if (len > 8) {
int numb = int(random(2, 5));
for (int i = 0; i < numb; i++) {
pushMatrix();
rotate(random(-PI/4, PI/4));
rama(len*random(0.3, 0.99), s*random(0.5, 0.9));
popMatrix();
}
}
}
/*
Utilidades de procesamiento de imagen
Image processing utilities
*/
class ImageProcess {
PImage img, edgeImg;
// Con Mode ARGB or RGB
ImageProcess(PImage _img, int _mod) {
img = createImage(_img.width, _img.height, _mod);
edgeImg = createImage(_img.width, _img.height, _mod);
img.copy(_img, 0, 0, _img.width, _img.height, 0, 0, _img.width, _img.height);
}
ImageProcess(PImage _img) {
img = createImage(_img.width, _img.height, RGB);
edgeImg = createImage(_img.width, _img.height, RGB);
img.copy(_img, 0, 0, _img.width, _img.height, 0, 0, _img.width, _img.height);
}
ImageProcess() { }
public void setImg(PImage _img) {
img.copy(_img, 0, 0, _img.width, _img.height, 0, 0, _img.width, _img.height);
}
PImage invert(PImage _img) {
pushStyle();
colorMode(HSB);
_img.loadPixels();
for (int i = 0; i < _img.pixels.length; i++)
{
_img.pixels[i] = color(brightness(_img.pixels[i]));
_img.pixels[i] ^= 0xDDFFFFFF;
}
_img.updatePixels();
popStyle();
return _img;
}
PImage trans(PImage _img) {
pushStyle();
colorMode(HSB);
_img.loadPixels();
for (int i = 0; i < _img.pixels.length; i++)
{
_img.pixels[i] = color(_img.pixels[i], 120);
}
_img.updatePixels();
popStyle();
return _img;
}
PImage detectEdge(int _et) {
pushStyle();
colorMode(RGB);
float[][] kernel = {{ -1, -1, -1},
{ -1, _et, -1},
{ -1, -1, -1}};
img.loadPixels();
for (int y = 1; y < img.height-1; y++) {
for (int x = 1; x < img.width-1; x++) {
float sum = 0;
for (int ky = -1; ky <= 1; ky++) {
for (int kx = -1; kx <= 1; kx++) {
int pos = (y + ky)*img.width + (x + kx);
float val = green(img.pixels[pos]);
sum += kernel[ky+1][kx+1] * val;
}
}
edgeImg.pixels[y*img.width + x] = color(sum, sum, sum);
}
}
edgeImg.updatePixels();
popStyle();
return edgeImg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment