From 1c41f562573d61f73edee84c8be4d8c974f078ab Mon Sep 17 00:00:00 2001 From: Rahul Date: Sat, 22 Jul 2023 14:07:40 +0530 Subject: [PATCH] added save settings option and some minor changes --- linedraw.py | 20 +++++++++++++------- linedraw/default.py | 18 ++++++++++++++++++ linedraw/helper.py | 40 ++++++++++++++++++++++++++++++++++++++-- linedraw/util.py | 13 +++++++++++++ 4 files changed, 82 insertions(+), 9 deletions(-) diff --git a/linedraw.py b/linedraw.py index 7f27b96..8beac0b 100644 --- a/linedraw.py +++ b/linedraw.py @@ -14,32 +14,37 @@ if __name__ == '__main__': help='Output image path') 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') - 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', 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', 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', 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', 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, 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, 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() input_path = args.input_path @@ -51,4 +56,5 @@ if __name__ == '__main__': argument.show_bitmap = args.show_bitmap argument.no_cv = args.no_cv argument.resolution = args.resolution + argument.save_settings = args.save_settings sketch(input_path, export_path) diff --git a/linedraw/default.py b/linedraw/default.py index a69a843..d60a0d3 100644 --- a/linedraw/default.py +++ b/linedraw/default.py @@ -1,3 +1,5 @@ +import json + class Default: export_path = "output/out.svg" show_bitmap = False @@ -7,5 +9,21 @@ class Default: hatch_size = 16 contour_simplify = 2 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() \ No newline at end of file diff --git a/linedraw/helper.py b/linedraw/helper.py index 0baab8d..3866ca1 100644 --- a/linedraw/helper.py +++ b/linedraw/helper.py @@ -1,15 +1,32 @@ from PIL import Image, ImageOps, ImageDraw import linedraw.perlin as perlin +from datetime import datetime +import os from linedraw.filters import appmask, F_SobelX, F_SobelY 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 -def sketch(input_path, output_path): +def sketch(input_path, output_path:str): 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: IMAGE = Image.open(input_path) except FileNotFoundError: @@ -39,10 +56,24 @@ def sketch(input_path, output_path): draw.line(l, (0, 0, 0), 5) 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.write(make_svg(lines)) 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.") + if argument.save_settings: + argument.save(os.path.dirname(output_path) + '/settings.json') print("done.") return lines @@ -197,3 +228,8 @@ def make_svg(lines): out += '\n' out += '' return out + + +def rasterise_image(svg_image, raster_image): + print("Converting image....") + # to be implemented diff --git a/linedraw/util.py b/linedraw/util.py index 235c3ed..8867080 100644 --- a/linedraw/util.py +++ b/linedraw/util.py @@ -1,3 +1,4 @@ +import os def midpt(*args): xs,ys = 0,0 for p in args: @@ -7,3 +8,15 @@ def midpt(*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))]) + + +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 \ No newline at end of file