├── .gitignore ├── LICENSE ├── README.md ├── data ├── example.gif └── test.jpg └── linerizer.pde /.gitignore: -------------------------------------------------------------------------------- 1 | data/test* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 David Koch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | linerizer 2 | ========= 3 | 4 | This Processing script takes a picture and redraws it in a single line. 5 | 6 | ![example](data/example.gif) 7 | 8 | To try it out, just drag and drop your image into the sketch window. 9 | You can redraw the image by clicking on it. If you click on the upper half of the image, the algorithm draws the darker parts of the image, if you click on the lower half, the lighter parts are drawn. 10 | Depending on where you click in x-direction, the drawing will look more or less dense. 11 | 12 | Requires the [drop library](http://transfluxus.github.io/drop/). 13 | -------------------------------------------------------------------------------- /data/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davekch/linerizer/b23636c411dabd27f40352caa3a9e5294138733a/data/example.gif -------------------------------------------------------------------------------- /data/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davekch/linerizer/b23636c411dabd27f40352caa3a9e5294138733a/data/test.jpg -------------------------------------------------------------------------------- /linerizer.pde: -------------------------------------------------------------------------------- 1 | import drop.*; 2 | import java.io.File; 3 | 4 | SDrop drop; 5 | PImage input; 6 | PImage prepared; 7 | PImage output; 8 | int width, height; 9 | int pixelsize = 3; 10 | ArrayList path; 11 | // stuff for animation 12 | int step = 1; 13 | int speed = 120; 14 | boolean inverted = false; 15 | boolean readyToDraw = false; 16 | boolean killThreads = true; 17 | 18 | void setup () { 19 | size(500, 100); 20 | surface.setResizable(true); 21 | drop = new SDrop(this); 22 | textSize(32); 23 | } 24 | 25 | void draw () { 26 | if (path == null) { 27 | fill(20); 28 | text("Drop an image here ...", 10, 60); 29 | return; 30 | } 31 | if (step < path.size() - 1){ 32 | if (input != null) image(input, 0,0); 33 | } else { 34 | if (inverted) { 35 | background(20); 36 | } else { 37 | background(235); 38 | } 39 | } 40 | if (readyToDraw) { 41 | animatedPath(path, pixelsize, speed); 42 | } 43 | // displayPath(path, pixelsize); 44 | } 45 | 46 | void mouseClicked () { 47 | readyToDraw = false; 48 | // kill other path-calculating threads if still open 49 | killThreads = true; 50 | inverted = mouseY > input.height/2; 51 | step = 1; 52 | int threshold = (int) map(mouseX, 0, input.width, 0, 255); 53 | output = floydSteinberg(prepared, threshold); 54 | thread("neighborPathFromImage"); 55 | } 56 | 57 | void dropEvent (DropEvent event) { 58 | if (event.isFile() && event.isImage()) { 59 | // don't use event.loadImage() because it runs in a seperate thread :( 60 | File f = event.file(); 61 | input = loadImage(f.getAbsolutePath()); 62 | // make a copy of input 63 | prepared = input.get(0, 0, input.width, input.height); 64 | // determine size 65 | if (input.height <= input.width) { 66 | width = 300; 67 | prepared.resize(width, 0); 68 | height = prepared.height; 69 | } else { 70 | height = 300; 71 | prepared.resize(0, height); 72 | width = prepared.width; 73 | } 74 | surface.setSize(width*pixelsize, height*pixelsize); 75 | prepared.filter(GRAY); 76 | output = floydSteinberg(prepared, 128); 77 | neighborPathFromImage(); 78 | // scale input 79 | input.resize(width*pixelsize, 0); 80 | } 81 | } 82 | 83 | 84 | void displayPixels (PImage image, int pixelwidth) { 85 | noStroke(); 86 | for (int y=0; y points, float scale, int speed) { 96 | if (inverted) { 97 | stroke(235); 98 | } else { 99 | stroke(20); 100 | } 101 | for (int i=0; i path.size()-1)) { 113 | step += speed; 114 | } else { 115 | step = path.size() - 1; 116 | } 117 | } 118 | 119 | void displayPath (ArrayList points, float scale) { 120 | background(255); 121 | stroke(0); 122 | for (int i=0; i threshold) { 138 | out.pixels[i] = color(255); 139 | err = brightness(in.pixels[i]) - 255; 140 | } else { 141 | out.pixels[i] = color(0); 142 | err = 255 - brightness(in.pixels[i]); 143 | } 144 | } 145 | return out; 146 | } 147 | 148 | PImage floydSteinberg (PImage in, int threshold) { 149 | // the cooler errorDiffusion 150 | // copy input image 151 | PImage out = in.get(0, 0, in.width, in.height); 152 | out.loadPixels(); 153 | float tmp; 154 | float err; 155 | color newcolor; 156 | for (int y=0; y threshold) { 160 | newcolor = color(255); 161 | } else { 162 | newcolor = color(0); 163 | } 164 | err = tmp - brightness(newcolor); 165 | out.pixels[x + y * out.width] = newcolor; 166 | out.pixels[x+1 + y * out.width] += err * 7/16; 167 | out.pixels[x-1 + (y+1) * out.width] += err * 3/16; 168 | out.pixels[x + (y+1) * out.width] += err * 5/16; 169 | out.pixels[x+1 + (y+1) * out.width] += err * 1/16; 170 | } 171 | } 172 | return out; 173 | } 174 | 175 | void neighborPathFromImage () { 176 | int col; 177 | if (inverted) { 178 | col = 255; 179 | } else { 180 | col = 0; 181 | } 182 | PImage img = output; 183 | // get all black points 184 | ArrayList points = new ArrayList(); 185 | for (int y=0; y pointList) { 197 | // writes the list of points folling the nearest neighbor 198 | // to global path (ArrayList) variable 199 | // using linear search 200 | 201 | if (killThreads) killThreads = false; 202 | // don't start the animated draw, because path is empty right now 203 | path = new ArrayList(); 204 | Point current = pointList.get(0); 205 | pointList.remove(0); 206 | while (pointList.size() > 0) { 207 | int min = current.square_distance(pointList.get(0)); 208 | int neighborIndex = 0; 209 | for (int i=0; i speed) && !readyToDraw) readyToDraw = true; 224 | } 225 | } 226 | 227 | class Point { 228 | int x, y; 229 | 230 | Point (int x, int y) { 231 | this.x = x; 232 | this.y = y; 233 | } 234 | 235 | int square_distance (Point p) { 236 | return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y); 237 | } 238 | } 239 | --------------------------------------------------------------------------------