Skip to content

Commit

Permalink
python3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingdong Huang committed Oct 27, 2019
1 parent eac8073 commit 2cfeb04
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# linedraw
Convert images to vectorized line drawings for plotters.
![Alt text](/screenshots/1.png?raw=true "")
![Alt text](./screenshots/1.png?raw=true "")

- Exports polyline-only svg file with optimized stroke order for plotters;
- Sketchy style powered by Perlin noise;
- Contour-only or hatch-only modes.

## Dependencies
Python 2, PIL/Pillow, numpy, OpenCV (Optional for better performance)
Python 2 or 3, PIL/Pillow, numpy, OpenCV (Optional for better performance)

## Usage
Convert an image to line drawing and export .SVG format:

```shell
$ python linedraw.py -i input.jpg -o output.svg
```
Command specs:

```
usage: linedraw.py [-h] [-i [INPUT_PATH]] [-o [OUTPUT_PATH]] [-b] [-nc] [-nh]
[--no_cv] [--hatch_size [HATCH_SIZE]]
Expand All @@ -38,6 +40,7 @@ optional arguments:
Level of contour simplification. eg. 1, 2, 3
```
Python:

```python
import linedraw
lines = linedraw.sketch("path/to/img.jpg") # return list of polylines, eg.
Expand Down
24 changes: 12 additions & 12 deletions linedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import numpy as np
import cv2
except:
print "Cannot import numpy/openCV. Switching to NO_CV mode."
print("Cannot import numpy/openCV. Switching to NO_CV mode.")
no_cv = True

def find_edges(IM):
print "finding edges..."
print("finding edges...")
if no_cv:
#appmask(IM,[F_Blur])
appmask(IM,[F_SobelX,F_SobelY])
Expand All @@ -39,7 +39,7 @@ def find_edges(IM):


def getdots(IM):
print "getting contour points..."
print("getting contour points...")
PX = IM.load()
dots = []
w,h = IM.size
Expand All @@ -58,7 +58,7 @@ def getdots(IM):
return dots

def connectdots(dots):
print "connecting contour points..."
print("connecting contour points...")
contours = []
for y in range(len(dots)):
for x,v in dots[y]:
Expand Down Expand Up @@ -91,7 +91,7 @@ def connectdots(dots):


def getcontours(IM,sc=2):
print "generating contours..."
print("generating contours...")
IM = find_edges(IM)
IM1 = IM.copy()
IM2 = IM.rotate(-90,expand=True).transpose(Image.FLIP_LEFT_RIGHT)
Expand Down Expand Up @@ -128,7 +128,7 @@ def getcontours(IM,sc=2):


def hatch(IM,sc=16):
print "hatching..."
print("hatching...")
PX = IM.load()
w,h = IM.size
lg1 = []
Expand Down Expand Up @@ -184,13 +184,13 @@ def sketch(path):

lines = []
if draw_contours:
lines += getcontours(IM.resize((resolution/contour_simplify,resolution/contour_simplify*h/w)),contour_simplify)
lines += getcontours(IM.resize((resolution//contour_simplify,resolution//contour_simplify*h//w)),contour_simplify)
if draw_hatch:
lines += hatch(IM.resize((resolution/hatch_size,resolution/hatch_size*h/w)),hatch_size)
lines += hatch(IM.resize((resolution//hatch_size,resolution//hatch_size*h//w)),hatch_size)

lines = sortlines(lines)
if show_bitmap:
disp = Image.new("RGB",(resolution,resolution*h/w),(255,255,255))
disp = Image.new("RGB",(resolution,resolution*h//w),(255,255,255))
draw = ImageDraw.Draw(disp)
for l in lines:
draw.line(l,(0,0,0),5)
Expand All @@ -199,13 +199,13 @@ def sketch(path):
f = open(export_path,'w')
f.write(makesvg(lines))
f.close()
print len(lines), "strokes."
print "done."
print("strokes.")
print("done.")
return lines


def makesvg(lines):
print "generating svg file..."
print("generating svg file...")
out = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">'
for l in lines:
l = ",".join([str(p[0]*0.5)+","+str(p[1]*0.5) for p in l])
Expand Down
2 changes: 1 addition & 1 deletion strokesort.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def sortlines(lines):
print "optimizing stroke sequence..."
print("optimizing stroke sequence...")
clines = lines[:]
slines = [clines.pop(0)]
while clines != []:
Expand Down

0 comments on commit 2cfeb04

Please sign in to comment.