kopia lustrzana https://github.com/LingDong-/linedraw
added save settings option and some minor changes
rodzic
af7694797c
commit
1c41f56257
20
linedraw.py
20
linedraw.py
|
@ -14,32 +14,37 @@ if __name__ == '__main__':
|
||||||
help='Output image path')
|
help='Output image path')
|
||||||
|
|
||||||
parser.add_argument('-r', '--resolution', dest='resolution',
|
parser.add_argument('-r', '--resolution', dest='resolution',
|
||||||
default=argument.show_bitmap, action='store_const',
|
default=argument.resolution, action='store', nargs='?', type=int,
|
||||||
help='Resolution of the output image')
|
help='Resolution of the output image')
|
||||||
|
|
||||||
parser.add_argument('-b', '--show_bitmap', dest='show_bitmap',
|
parser.add_argument('-b', '--show-bitmap', dest='show_bitmap',
|
||||||
const=not argument.show_bitmap, default=argument.show_bitmap, action='store_const',
|
const=not argument.show_bitmap, default=argument.show_bitmap, action='store_const',
|
||||||
help='Display bitmap preview.')
|
help='Display bitmap preview.')
|
||||||
|
|
||||||
parser.add_argument('-nc', '--no_contour', dest='no_contour',
|
parser.add_argument('-nc', '--no-contour', dest='no_contour',
|
||||||
const=argument.draw_contours, default=not argument.draw_contours, action='store_const',
|
const=argument.draw_contours, default=not argument.draw_contours, action='store_const',
|
||||||
help="Don't draw contours.")
|
help="Don't draw contours.")
|
||||||
|
|
||||||
parser.add_argument('-nh', '--no_hatch', dest='no_hatch',
|
parser.add_argument('-nh', '--no-hatch', dest='no_hatch',
|
||||||
const=argument.draw_hatch, default=not argument.draw_hatch, action='store_const',
|
const=argument.draw_hatch, default=not argument.draw_hatch, action='store_const',
|
||||||
help='Disable hatching.')
|
help='Disable hatching.')
|
||||||
|
|
||||||
parser.add_argument('--no_cv', dest='no_cv',
|
parser.add_argument('--no-cv', dest='no_cv',
|
||||||
const=not argument.no_cv, default=argument.no_cv, action='store_const',
|
const=not argument.no_cv, default=argument.no_cv, action='store_const',
|
||||||
help="Don't use openCV.")
|
help="Don't use openCV.")
|
||||||
|
|
||||||
parser.add_argument('--hatch_size', dest='hatch_size',
|
parser.add_argument('--hatch-size', dest='hatch_size',
|
||||||
default=argument.hatch_size, action='store', nargs='?', type=int,
|
default=argument.hatch_size, action='store', nargs='?', type=int,
|
||||||
help='Patch size of hatches. eg. 8, 16, 32')
|
help='Patch size of hatches. eg. 8, 16, 32')
|
||||||
parser.add_argument('--contour_simplify', dest='contour_simplify',
|
|
||||||
|
parser.add_argument('--contour-simplify', dest='contour_simplify',
|
||||||
default=argument.contour_simplify, action='store', nargs='?', type=int,
|
default=argument.contour_simplify, action='store', nargs='?', type=int,
|
||||||
help='Level of contour simplification. eg. 1, 2, 3')
|
help='Level of contour simplification. eg. 1, 2, 3')
|
||||||
|
|
||||||
|
parser.add_argument('--save-settings', dest='save_settings',
|
||||||
|
const=not argument.save_settings, default=argument.save_settings, action='store_const',
|
||||||
|
help='To Save the settings to a json file')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
input_path = args.input_path
|
input_path = args.input_path
|
||||||
|
@ -51,4 +56,5 @@ if __name__ == '__main__':
|
||||||
argument.show_bitmap = args.show_bitmap
|
argument.show_bitmap = args.show_bitmap
|
||||||
argument.no_cv = args.no_cv
|
argument.no_cv = args.no_cv
|
||||||
argument.resolution = args.resolution
|
argument.resolution = args.resolution
|
||||||
|
argument.save_settings = args.save_settings
|
||||||
sketch(input_path, export_path)
|
sketch(input_path, export_path)
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import json
|
||||||
|
|
||||||
class Default:
|
class Default:
|
||||||
export_path = "output/out.svg"
|
export_path = "output/out.svg"
|
||||||
show_bitmap = False
|
show_bitmap = False
|
||||||
|
@ -7,5 +9,21 @@ class Default:
|
||||||
hatch_size = 16
|
hatch_size = 16
|
||||||
contour_simplify = 2
|
contour_simplify = 2
|
||||||
resolution = 1024
|
resolution = 1024
|
||||||
|
save_settings = False
|
||||||
|
|
||||||
|
def save(self,settings_path):
|
||||||
|
print("Savings settings to a JSON file")
|
||||||
|
file = open(settings_path, 'w')
|
||||||
|
data = {
|
||||||
|
"resolution": self.resolution,
|
||||||
|
"show_bitmap": self.show_bitmap,
|
||||||
|
"draw_contours": self.draw_contours,
|
||||||
|
"draw_hatch": self.draw_hatch,
|
||||||
|
"use_opencv": not self.no_cv,
|
||||||
|
"hatch_size": self.hatch_size,
|
||||||
|
"contour_simplify": self.contour_simplify
|
||||||
|
}
|
||||||
|
json.dump(data,file)
|
||||||
|
file.close()
|
||||||
|
|
||||||
argument = Default()
|
argument = Default()
|
|
@ -1,15 +1,32 @@
|
||||||
from PIL import Image, ImageOps, ImageDraw
|
from PIL import Image, ImageOps, ImageDraw
|
||||||
import linedraw.perlin as perlin
|
import linedraw.perlin as perlin
|
||||||
|
from datetime import datetime
|
||||||
|
import os
|
||||||
|
|
||||||
from linedraw.filters import appmask, F_SobelX, F_SobelY
|
from linedraw.filters import appmask, F_SobelX, F_SobelY
|
||||||
from linedraw.default import argument
|
from linedraw.default import argument
|
||||||
from linedraw.util import distsum
|
from linedraw.util import distsum, is_image_file, extract_file_name_and_extension
|
||||||
from linedraw.strokesort import sortlines
|
from linedraw.strokesort import sortlines
|
||||||
|
|
||||||
|
|
||||||
def sketch(input_path, output_path):
|
def sketch(input_path, output_path:str):
|
||||||
IMAGE = None
|
IMAGE = None
|
||||||
|
|
||||||
|
if not is_image_file(input_path):
|
||||||
|
return print("Please provide the path for an image.")
|
||||||
|
|
||||||
|
out_file, out_ext = extract_file_name_and_extension(output_path)
|
||||||
|
|
||||||
|
if not out_file:
|
||||||
|
in_file, in_ext = extract_file_name_and_extension(input_path)
|
||||||
|
out_ext = '.svg'
|
||||||
|
if not output_path.endswith('/'):
|
||||||
|
output_path += '/'
|
||||||
|
output_path += in_file + out_ext
|
||||||
|
|
||||||
|
if out_ext != '.svg':
|
||||||
|
return print("Currently we can only save as svg file")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
IMAGE = Image.open(input_path)
|
IMAGE = Image.open(input_path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
@ -39,10 +56,24 @@ def sketch(input_path, output_path):
|
||||||
draw.line(l, (0, 0, 0), 5)
|
draw.line(l, (0, 0, 0), 5)
|
||||||
disp.show()
|
disp.show()
|
||||||
|
|
||||||
|
# if out_ext != '.svg':
|
||||||
|
# now = datetime.now()
|
||||||
|
# svg_path = output_path.rsplit('/', 1)[0] + now.strftime("%Y%m%d%H%M%S%f") + '.svg'
|
||||||
|
# else:
|
||||||
|
# svg_path = output_path
|
||||||
|
|
||||||
file = open(output_path, 'w')
|
file = open(output_path, 'w')
|
||||||
file.write(make_svg(lines))
|
file.write(make_svg(lines))
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
# if out_ext != '.svg':
|
||||||
|
# if not is_image_file(output_path):
|
||||||
|
# return "Output path is not an image path"
|
||||||
|
# rasterise_image(svg_path,output_path)
|
||||||
|
# os.remove(svg_path)
|
||||||
print(len(lines), "strokes.")
|
print(len(lines), "strokes.")
|
||||||
|
if argument.save_settings:
|
||||||
|
argument.save(os.path.dirname(output_path) + '/settings.json')
|
||||||
print("done.")
|
print("done.")
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
@ -197,3 +228,8 @@ def make_svg(lines):
|
||||||
out += '<polyline points="' + l + '" stroke="black" stroke-width="2" fill="none" />\n'
|
out += '<polyline points="' + l + '" stroke="black" stroke-width="2" fill="none" />\n'
|
||||||
out += '</svg>'
|
out += '</svg>'
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def rasterise_image(svg_image, raster_image):
|
||||||
|
print("Converting image....")
|
||||||
|
# to be implemented
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import os
|
||||||
def midpt(*args):
|
def midpt(*args):
|
||||||
xs,ys = 0,0
|
xs,ys = 0,0
|
||||||
for p in args:
|
for p in args:
|
||||||
|
@ -7,3 +8,15 @@ def midpt(*args):
|
||||||
|
|
||||||
def distsum(*args):
|
def distsum(*args):
|
||||||
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))])
|
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))])
|
||||||
|
|
||||||
|
|
||||||
|
def is_image_file(file_path):
|
||||||
|
image_extensions = ['.jpg', '.jpeg', '.png']
|
||||||
|
tmp,ext = extract_file_name_and_extension(file_path)
|
||||||
|
return ext in image_extensions
|
||||||
|
|
||||||
|
|
||||||
|
def extract_file_name_and_extension(file_path):
|
||||||
|
file_name_with_extension = os.path.basename(file_path)
|
||||||
|
file_name, file_extension = os.path.splitext(file_name_with_extension)
|
||||||
|
return file_name, file_extension
|
Ładowanie…
Reference in New Issue