Skip to content

Instantly share code, notes, and snippets.

@y-a-v-a
Created June 19, 2015 10:57
Show Gist options
  • Save y-a-v-a/6799c67f7b5cebfa4667 to your computer and use it in GitHub Desktop.
Save y-a-v-a/6799c67f7b5cebfa4667 to your computer and use it in GitHub Desktop.
Simple glitch application that glitches images by manipulating the string data.
#!/usr/bin/env node
var gd = require('node-gd');
var fs = require('fs');
var c;
var img = fs.readFileSync('b.jpg', { encoding: 'binary' });
var imageBuffer = new Buffer(img, 'binary');
var newBuffer;
var ch = ['aasd','gwer'];
var replace;
function rand() {
return Math.floor(Math.random() * img.length);
}
for (var j = 0; j < 10; j++) {
newBuffer = new Buffer(img.length);
imageBuffer.copy(newBuffer);
for (var i = 0; i < 100; i++) {
c = rand();
replace = ch[Math.floor(Math.random() * ch.length)];
newBuffer.write(replace, c, 1);
}
try {
var newImg = gd.createFromJpegPtr(newBuffer.toString('binary'));
newImg.saveFile('test'+j+'.jpg');
// fs.writeFileSync('test.txt', b);
} catch(e) {
console.log('erroneous image');
}
}
@y-a-v-a
Copy link
Author

y-a-v-a commented Jun 19, 2015

Instructions:
0. Type in Terminal: brew install gd. If this throws errors, type:

  1. Open a terminal and paste ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" and then repeat step 0.
  2. Copy the above file to an empty directory.
  3. Go with to your Terminal and cd into that directory, like cd Projects/glitch
  4. Type in npm init and press enter many times.
  5. Then type in npm install --save node-gd and press enter.
  6. Copy a simple JPEG image into this directory
  7. Change the name of the file to b.jpg or something, as long as you also change the name of it in line 6.
  8. Type in the Terminal node glitch.js and hopefully you'll get glitched images!

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