making it pretty

This commit is contained in:
iou1name 2018-03-21 12:00:46 -04:00
parent 64b1b91f21
commit 0d0fd3e638
2 changed files with 106 additions and 100 deletions

View File

@ -5,8 +5,8 @@ machine chooch.
""" """
import os import os
import linedraw
import stream import stream
import linedraw
def draw(rec_filename): def draw(rec_filename):
""" """

View File

@ -1,11 +1,15 @@
#!/usr/bin/env python3
"""
Takes a raster image file and vectorizes it to create line art usable by
a pen plotter.
"""
import os import os
from random import *
import math import math
from random import *
from PIL import Image, ImageDraw, ImageOps from PIL import Image, ImageDraw, ImageOps
no_cv = True no_cv = True
export_path = "output/out.svg"
draw_contours = True draw_contours = True
draw_hatch = False draw_hatch = False
@ -24,6 +28,7 @@ F_Blur = {
(-2,1):4,(-1,1):9,(0,1):12,(1,1):9,(2,1):4, (-2,1):4,(-1,1):9,(0,1):12,(1,1):9,(2,1):4,
(-2,2):2,(-1,2):4,(0,2):5,(1,2):4,(2,2):2, (-2,2):2,(-1,2):4,(0,2):5,(1,2):4,(2,2):2,
} }
F_SobelX = { F_SobelX = {
(-1,-1): 1, (-1,-1): 1,
(0,-1): 0, (0,-1): 0,
@ -33,7 +38,8 @@ F_SobelX = {
(1,0): -2, (1,0): -2,
(-1,1): 1, (-1,1): 1,
(0,1): 0, (0,1): 0,
(1,1): -1} (1,1) -1}
F_SobelY = { F_SobelY = {
(-1,-1): 1, (-1,-1): 1,
(0,-1): 2, (0,-1): 2,
@ -45,9 +51,9 @@ F_SobelY = {
(0,1): -2, (0,1): -2,
(1,1): -1} (1,1): -1}
def appmask(IM,masks): def appmask(image, masks):
PX = IM.load() PX = image.load()
w,h = IM.size w, h = image.size
NPX = {} NPX = {}
for x in range(0, w): for x in range(0, w):
for y in range(0, h): for y in range(0, h):
@ -99,38 +105,37 @@ def sortlines(lines):
return slines return slines
def auto_canny(image, sigma=0.33):
def auto_canny(img, sigma=0.33):
""" """
Automatically determines appropriate upper and lower boundries for the Canny function. Automatically determines appropriate upper and lower boundries for the Canny function.
""" """
med = np.median(img) med = np.median(image)
lower = int(max(0, (1.0 - sigma) * med)) lower = int(max(0, (1.0 - sigma) * med))
upper = int(min(255, (1.0 + sigma) * med)) upper = int(min(255, (1.0 + sigma) * med))
edges = cv2.Canny(img, lower, upper) edges = cv2.Canny(image, lower, upper)
return edges return edges
def find_edges(IM): def find_edges(image):
print("finding edges...") print("finding edges...")
no_cv = True no_cv = True
if no_cv: if no_cv:
#appmask(IM,[F_Blur]) #appmask(image, [F_Blur])
appmask(IM,[F_SobelX,F_SobelY]) appmask(image, [F_SobelX, F_SobelY])
else: else:
im = np.array(IM) image = np.array(image)
im = cv2.GaussianBlur(im,(3,3),0) image = cv2.GaussianBlur(image, (3, 3), 0)
#im = cv2.Canny(im,100,200) #image = cv2.Canny(image,100,200)
im = auto_canny(im) image = auto_canny(image)
IM = Image.fromarray(im) image = Image.fromarray(image)
return IM.point(lambda p: p > 128 and 255) return image.point(lambda p: p > 128 and 255)
def getdots(IM): def getdots(image):
print("getting contour points...") print("getting contour points...")
PX = IM.load() PX = image.load()
dots = [] dots = []
w,h = IM.size w, h = image.size
for y in range(h-1): for y in range(h-1):
row = [] row = []
for x in range(1, w): for x in range(1, w):
@ -179,14 +184,14 @@ def connectdots(dots):
return contours return contours
def getcontours(IM,sc=2): def getcontours(image, sc=2):
print("generating contours...") print("generating contours...")
IM = find_edges(IM) image = find_edges(image)
IM1 = IM.copy() image1 = IM.copy()
IM2 = IM.rotate(-90,expand=True).transpose(Image.FLIP_LEFT_RIGHT) image2 = image.rotate(-90, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
dots1 = getdots(IM1) dots1 = getdots(image1)
contours1 = connectdots(dots1) contours1 = connectdots(dots1)
dots2 = getdots(IM2) dots2 = getdots(image2)
contours2 = connectdots(dots2) contours2 = connectdots(dots2)
for i in range(len(contours2)): for i in range(len(contours2)):
@ -216,10 +221,10 @@ def getcontours(IM,sc=2):
return contours return contours
def hatch(IM,sc=16): def hatch(image, sc=16):
print("hatching...") print("hatching...")
PX = IM.load() PX = image.load()
w,h = IM.size w,h = image.size
lg1 = [] lg1 = []
lg2 = [] lg2 = []
for x0 in range(w): for x0 in range(w):
@ -268,6 +273,7 @@ def sketch(path, export_path=None, resolution=1024, hatch_size=16, contour_simpl
width = int(resolution/contour_simplify) width = int(resolution/contour_simplify)
height = int(resolution/contour_simplify*image.size[0]/image.size[1]) height = int(resolution/contour_simplify*image.size[0]/image.size[1])
lines += getcontours(image.resize((width, height)), contour_simplify) lines += getcontours(image.resize((width, height)), contour_simplify)
if draw_hatch: if draw_hatch:
width = int(resolution/hatch_size) width = int(resolution/hatch_size)
height = int(resolution/hatch_size*image.size[0]/image.size[1]) height = int(resolution/hatch_size*image.size[0]/image.size[1])