├── .gitignore ├── LICENSE ├── README.md ├── filters.py ├── images ├── cameraman.tif ├── lenna.png ├── peppers.png └── test.jpg ├── linedraw.py ├── output └── out.svg ├── perlin.py ├── screenshots └── 1.png ├── strokesort.py └── util.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Lingdong Huang 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 | # linedraw 2 | Convert images to vectorized line drawings for plotters. 3 | ![Alt text](./screenshots/1.png?raw=true "") 4 | 5 | - Exports polyline-only svg file with optimized stroke order for plotters; 6 | - Sketchy style powered by Perlin noise; 7 | - Contour-only or hatch-only modes. 8 | 9 | ## Dependencies 10 | Python 2 or 3, PIL/Pillow, numpy, OpenCV (Optional for better performance) 11 | 12 | ## Usage 13 | Convert an image to line drawing and export .SVG format: 14 | 15 | ```shell 16 | $ python linedraw.py -i input.jpg -o output.svg 17 | ``` 18 | Command specs: 19 | 20 | ``` 21 | usage: linedraw.py [-h] [-i [INPUT_PATH]] [-o [OUTPUT_PATH]] [-b] [-nc] [-nh] 22 | [--no_cv] [--hatch_size [HATCH_SIZE]] 23 | [--contour_simplify [CONTOUR_SIMPLIFY]] 24 | 25 | Convert image to vectorized line drawing for plotters. 26 | 27 | optional arguments: 28 | -h, --help show this help message and exit 29 | -i [INPUT_PATH], --input [INPUT_PATH] 30 | Input path 31 | -o [OUTPUT_PATH], --output [OUTPUT_PATH] 32 | Output path. 33 | -b, --show_bitmap Display bitmap preview. 34 | -nc, --no_contour Don't draw contours. 35 | -nh, --no_hatch Disable hatching. 36 | --no_cv Don't use openCV. 37 | --hatch_size [HATCH_SIZE] 38 | Patch size of hatches. eg. 8, 16, 32 39 | --contour_simplify [CONTOUR_SIMPLIFY] 40 | Level of contour simplification. eg. 1, 2, 3 41 | ``` 42 | Python: 43 | 44 | ```python 45 | import linedraw 46 | lines = linedraw.sketch("path/to/img.jpg") # return list of polylines, eg. 47 | # [[(x,y),(x,y),(x,y)],[(x,y),(x,y),...],...] 48 | 49 | linedraw.visualize(lines) # simulates plotter behavior 50 | # draw the lines in order using turtle graphics. 51 | ``` 52 | 53 | -------------------------------------------------------------------------------- /filters.py: -------------------------------------------------------------------------------- 1 | from PIL import Image, ImageDraw, ImageOps, ImageFilter 2 | from random import * 3 | import math 4 | 5 | F_Blur = { 6 | (-2,-2):2,(-1,-2):4,(0,-2):5,(1,-2):4,(2,-2):2, 7 | (-2,-1):4,(-1,-1):9,(0,-1):12,(1,-1):9,(2,-1):4, 8 | (-2,0):5,(-1,0):12,(0,0):15,(1,0):12,(2,0):5, 9 | (-2,1):4,(-1,1):9,(0,1):12,(1,1):9,(2,1):4, 10 | (-2,2):2,(-1,2):4,(0,2):5,(1,2):4,(2,2):2, 11 | } 12 | F_SobelX = {(-1,-1):1,(0,-1):0,(1,-1):-1,(-1,0):2,(0,0):0,(1,0):-2,(-1,1):1,(0,1):0,(1,1):-1} 13 | F_SobelY = {(-1,-1):1,(0,-1):2,(1,-1):1,(-1,0):0,(0,0):0,(1,0):0,(-1,1):-1,(0,1):-2,(1,1):-1} 14 | 15 | 16 | def appmask(IM,masks): 17 | PX = IM.load() 18 | w,h = IM.size 19 | NPX = {} 20 | for x in range(0,w): 21 | for y in range(0,h): 22 | a = [0]*len(masks) 23 | for i in range(len(masks)): 24 | for p in masks[i].keys(): 25 | if 0 128 and 255) 39 | 40 | 41 | def getdots(IM): 42 | print("getting contour points...") 43 | PX = IM.load() 44 | dots = [] 45 | w,h = IM.size 46 | for y in range(h-1): 47 | row = [] 48 | for x in range(1,w): 49 | if PX[x,y] == 255: 50 | if len(row) > 0: 51 | if x-row[-1][0] == row[-1][-1]+1: 52 | row[-1] = (row[-1][0],row[-1][-1]+1) 53 | else: 54 | row.append((x,0)) 55 | else: 56 | row.append((x,0)) 57 | dots.append(row) 58 | return dots 59 | 60 | def connectdots(dots): 61 | print("connecting contour points...") 62 | contours = [] 63 | for y in range(len(dots)): 64 | for x,v in dots[y]: 65 | if v > -1: 66 | if y == 0: 67 | contours.append([(x,y)]) 68 | else: 69 | closest = -1 70 | cdist = 100 71 | for x0,v0 in dots[y-1]: 72 | if abs(x0-x) < cdist: 73 | cdist = abs(x0-x) 74 | closest = x0 75 | 76 | if cdist > 3: 77 | contours.append([(x,y)]) 78 | else: 79 | found = 0 80 | for i in range(len(contours)): 81 | if contours[i][-1] == (closest,y-1): 82 | contours[i].append((x,y,)) 83 | found = 1 84 | break 85 | if found == 0: 86 | contours.append([(x,y)]) 87 | for c in contours: 88 | if c[-1][1] < y-1 and len(c)<4: 89 | contours.remove(c) 90 | return contours 91 | 92 | 93 | def getcontours(IM,sc=2): 94 | print("generating contours...") 95 | IM = find_edges(IM) 96 | IM1 = IM.copy() 97 | IM2 = IM.rotate(-90,expand=True).transpose(Image.FLIP_LEFT_RIGHT) 98 | dots1 = getdots(IM1) 99 | contours1 = connectdots(dots1) 100 | dots2 = getdots(IM2) 101 | contours2 = connectdots(dots2) 102 | 103 | for i in range(len(contours2)): 104 | contours2[i] = [(c[1],c[0]) for c in contours2[i]] 105 | contours = contours1+contours2 106 | 107 | for i in range(len(contours)): 108 | for j in range(len(contours)): 109 | if len(contours[i]) > 0 and len(contours[j])>0: 110 | if distsum(contours[j][0],contours[i][-1]) < 8: 111 | contours[i] = contours[i]+contours[j] 112 | contours[j] = [] 113 | 114 | for i in range(len(contours)): 115 | contours[i] = [contours[i][j] for j in range(0,len(contours[i]),8)] 116 | 117 | 118 | contours = [c for c in contours if len(c) > 1] 119 | 120 | for i in range(0,len(contours)): 121 | contours[i] = [(v[0]*sc,v[1]*sc) for v in contours[i]] 122 | 123 | for i in range(0,len(contours)): 124 | for j in range(0,len(contours[i])): 125 | contours[i][j] = int(contours[i][j][0]+10*perlin.noise(i*0.5,j*0.1,1)),int(contours[i][j][1]+10*perlin.noise(i*0.5,j*0.1,2)) 126 | 127 | return contours 128 | 129 | 130 | def hatch(IM,sc=16): 131 | print("hatching...") 132 | PX = IM.load() 133 | w,h = IM.size 134 | lg1 = [] 135 | lg2 = [] 136 | for x0 in range(w): 137 | for y0 in range(h): 138 | x = x0*sc 139 | y = y0*sc 140 | if PX[x0,y0] > 144: 141 | pass 142 | 143 | elif PX[x0,y0] > 64: 144 | lg1.append([(x,y+sc/4),(x+sc,y+sc/4)]) 145 | elif PX[x0,y0] > 16: 146 | lg1.append([(x,y+sc/4),(x+sc,y+sc/4)]) 147 | lg2.append([(x+sc,y),(x,y+sc)]) 148 | 149 | else: 150 | lg1.append([(x,y+sc/4),(x+sc,y+sc/4)]) 151 | lg1.append([(x,y+sc/2+sc/4),(x+sc,y+sc/2+sc/4)]) 152 | lg2.append([(x+sc,y),(x,y+sc)]) 153 | 154 | lines = [lg1,lg2] 155 | for k in range(0,len(lines)): 156 | for i in range(0,len(lines[k])): 157 | for j in range(0,len(lines[k])): 158 | if lines[k][i] != [] and lines[k][j] != []: 159 | if lines[k][i][-1] == lines[k][j][0]: 160 | lines[k][i] = lines[k][i]+lines[k][j][1:] 161 | lines[k][j] = [] 162 | lines[k] = [l for l in lines[k] if len(l) > 0] 163 | lines = lines[0]+lines[1] 164 | 165 | for i in range(0,len(lines)): 166 | for j in range(0,len(lines[i])): 167 | lines[i][j] = int(lines[i][j][0]+sc*perlin.noise(i*0.5,j*0.1,1)),int(lines[i][j][1]+sc*perlin.noise(i*0.5,j*0.1,2))-j 168 | return lines 169 | 170 | 171 | def sketch(path): 172 | IM = None 173 | possible = [path,"images/"+path,"images/"+path+".jpg","images/"+path+".png","images/"+path+".tif"] 174 | for p in possible: 175 | try: 176 | IM = Image.open(p) 177 | break 178 | except FileNotFoundError: 179 | print("The Input File wasn't found. Check Path") 180 | exit(0) 181 | pass 182 | w,h = IM.size 183 | 184 | IM = IM.convert("L") 185 | IM=ImageOps.autocontrast(IM,10) 186 | 187 | lines = [] 188 | if draw_contours: 189 | lines += getcontours(IM.resize((resolution//contour_simplify,resolution//contour_simplify*h//w)),contour_simplify) 190 | if draw_hatch: 191 | lines += hatch(IM.resize((resolution//hatch_size,resolution//hatch_size*h//w)),hatch_size) 192 | 193 | lines = sortlines(lines) 194 | if show_bitmap: 195 | disp = Image.new("RGB",(resolution,resolution*h//w),(255,255,255)) 196 | draw = ImageDraw.Draw(disp) 197 | for l in lines: 198 | draw.line(l,(0,0,0),5) 199 | disp.show() 200 | 201 | f = open(export_path,'w') 202 | f.write(makesvg(lines)) 203 | f.close() 204 | print(len(lines),"strokes.") 205 | print("done.") 206 | return lines 207 | 208 | 209 | def makesvg(lines): 210 | print("generating svg file...") 211 | out = '' 212 | for l in lines: 213 | l = ",".join([str(p[0]*0.5)+","+str(p[1]*0.5) for p in l]) 214 | out += '\n' 215 | out += '' 216 | return out 217 | 218 | 219 | 220 | if __name__ == "__main__": 221 | parser = argparse.ArgumentParser(description='Convert image to vectorized line drawing for plotters.') 222 | parser.add_argument('-i','--input',dest='input_path', 223 | default='lenna',action='store',nargs='?',type=str, 224 | help='Input path') 225 | 226 | parser.add_argument('-o','--output',dest='output_path', 227 | default=export_path,action='store',nargs='?',type=str, 228 | help='Output path.') 229 | 230 | parser.add_argument('-b','--show_bitmap',dest='show_bitmap', 231 | const = not show_bitmap,default= show_bitmap,action='store_const', 232 | help="Display bitmap preview.") 233 | 234 | parser.add_argument('-nc','--no_contour',dest='no_contour', 235 | const = draw_contours,default= not draw_contours,action='store_const', 236 | help="Don't draw contours.") 237 | 238 | parser.add_argument('-nh','--no_hatch',dest='no_hatch', 239 | const = draw_hatch,default= not draw_hatch,action='store_const', 240 | help='Disable hatching.') 241 | 242 | parser.add_argument('--no_cv',dest='no_cv', 243 | const = not no_cv,default= no_cv,action='store_const', 244 | help="Don't use openCV.") 245 | 246 | 247 | parser.add_argument('--hatch_size',dest='hatch_size', 248 | default=hatch_size,action='store',nargs='?',type=int, 249 | help='Patch size of hatches. eg. 8, 16, 32') 250 | parser.add_argument('--contour_simplify',dest='contour_simplify', 251 | default=contour_simplify,action='store',nargs='?',type=int, 252 | help='Level of contour simplification. eg. 1, 2, 3') 253 | 254 | args = parser.parse_args() 255 | 256 | export_path = args.output_path 257 | draw_hatch = not args.no_hatch 258 | draw_contours = not args.no_contour 259 | hatch_size = args.hatch_size 260 | contour_simplify = args.contour_simplify 261 | show_bitmap = args.show_bitmap 262 | no_cv = args.no_cv 263 | sketch(args.input_path) 264 | -------------------------------------------------------------------------------- /output/out.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | -------------------------------------------------------------------------------- /perlin.py: -------------------------------------------------------------------------------- 1 | #Perlin Noise 2 | #Based on Javascript from p5.js (https://github.com/processing/p5.js/blob/master/src/math/noise.js) 3 | 4 | import math 5 | import random 6 | 7 | PERLIN_YWRAPB = 4 8 | PERLIN_YWRAP = 1<=1.0): xi+=1; xf-=1 72 | if (yf>=1.0): yi+=1; yf-=1 73 | if (zf>=1.0): zi+=1; zf-=1 74 | return r 75 | 76 | def noiseDetail(lod, falloff): 77 | if lod>0:perlin_octaves=lod 78 | if falloff>0:perlin_amp_falloff=falloff 79 | 80 | 81 | class LCG(): 82 | def __init__(self): 83 | self.m = 4294967296.0 84 | self.a = 1664525.0 85 | self.c = 1013904223.0 86 | self.seed = self.z = None 87 | def setSeed(self,val=None): 88 | self.z = self.seed = (math.random()*self.m if val == None else val) >> 0 89 | def getSeed(self): 90 | return self.seed 91 | def rand(self): 92 | self.z = (self.a * self.z + self.c) % self.m 93 | return self.z/self.m 94 | 95 | 96 | def noiseSeed(seed): 97 | lcg = LCG() 98 | lcg.setSeed(seed) 99 | perlin = [] 100 | for i in range(0,PERLIN_SIZE+1): 101 | perlin.append(lcg.rand()) 102 | 103 | -------------------------------------------------------------------------------- /screenshots/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LingDong-/linedraw/3aedc2f61d0ef3a3eb2e1ba18b933cbcd95a44ad/screenshots/1.png -------------------------------------------------------------------------------- /strokesort.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | from PIL import Image, ImageDraw, ImageOps 3 | from util import * 4 | 5 | 6 | def sortlines(lines): 7 | print("optimizing stroke sequence...") 8 | clines = lines[:] 9 | slines = [clines.pop(0)] 10 | while clines != []: 11 | x,s,r = None,1000000,False 12 | for l in clines: 13 | d = distsum(l[0],slines[-1][-1]) 14 | dr = distsum(l[-1],slines[-1][-1]) 15 | if d < s: 16 | x,s,r = l[:],d,False 17 | if dr < s: 18 | x,s,r = l[:],s,True 19 | 20 | clines.remove(x) 21 | if r == True: 22 | x = x[::-1] 23 | slines.append(x) 24 | return slines 25 | 26 | def visualize(lines): 27 | import turtle 28 | wn = turtle.Screen() 29 | t = turtle.Turtle() 30 | t.speed(0) 31 | t.pencolor('red') 32 | t.pd() 33 | for i in range(0,len(lines)): 34 | for p in lines[i]: 35 | t.goto(p[0]*640/1024-320,-(p[1]*640/1024-320)) 36 | t.pencolor('black') 37 | t.pencolor('red') 38 | turtle.mainloop() 39 | 40 | if __name__=="__main__": 41 | import linedraw 42 | #linedraw.draw_hatch = False 43 | lines = linedraw.sketch("Lenna") 44 | #lines = sortlines(lines) 45 | visualize(lines) -------------------------------------------------------------------------------- /util.py: -------------------------------------------------------------------------------- 1 | def midpt(*args): 2 | xs,ys = 0,0 3 | for p in args: 4 | xs += p[0] 5 | ys += p[1] 6 | return xs/len(args),ys/len(args) 7 | 8 | def distsum(*args): 9 | return sum([ ((args[i][0]-args[i-1][0])**2 + (args[i][1]-args[i-1][1])**2)**0.5 for i in range(1,len(args))]) 10 | --------------------------------------------------------------------------------