Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created November 5, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/fdc0566c6453574998fcc092a5c6e131 to your computer and use it in GitHub Desktop.
Save KrabCode/fdc0566c6453574998fcc092a5c6e131 to your computer and use it in GitHub Desktop.
Pixelsorts a picture there and back again and records a handy .mp4
import com.hamoid.VideoExport;
import processing.core.PApplet;
import processing.core.PImage;
public class MainApp extends PApplet{
public static void main(String[] args)
{
PApplet.main("MainApp", args);
}
PImage backup, img;
VideoExport vx;
boolean rec;
public void settings()
{
backup = loadImage("C:\\Project\\1.jpg");
backup.resize(backup.width, backup.height-1);
size(backup.width, backup.height);
}
public void setup(){
vx = new VideoExport(this);
vx.setFrameRate(60);
vx.startMovie();
rec = true;
}
public void draw()
{
//make a fresh working copy
try {
img = (PImage) backup.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
img = distort(img);
image(img, 0, 0, width, height);
//record video
if(rec){
vx.saveFrame();
}else{
vx.endMovie();
}
}
int minCutoff = -16777216;
int maxCutoff = -100000/3;
boolean up = false;
int intensity = 0;
private PImage distort(PImage img) {
img = pixelSortImage(img,
map(intensity, 0, width, minCutoff, maxCutoff),
up);
if(up){
intensity--;
}else{
intensity++;
}
if(intensity > width){
up = true;
}else if (intensity < 0){
up = false;
rec = false;
}
return img;
}
private PImage pixelSortImage(PImage img, float cutoff, boolean up) {
int remPix = 0;
if(up){
//sort upwards
for(int x = 0; x < img.width; x++) {
for(int y = 0; y < img.height; y++){
remPix = sortPixel(x, y, cutoff, remPix);
}
}
}else{
//sort downwards
for(int x = 0; x < img.width; x++) {
for (int y = img.height; y > 0; y--) {
remPix = sortPixel(x, y, cutoff, remPix);
}
}
}
return img;
}
private int sortPixel(int x, int y, float cutoff, int remPix){
int curPix = img.get(x, y);
if(curPix > cutoff){
remPix = curPix;
}else{
img.set(x, y, remPix);
}
return remPix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment