diff --git a/README.md b/README.md index dd947bb..5bab217 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,21 @@ A sort of artwork Drag'n'drop the image of your choice on the canvas to generate a hilbert or gosper curve version. +## Usage + +Choose a curve type, drag-and-drop an image, choose some thresholds, and it will generate a curve. You can then export it with "exportJSON" and then use `jsonToSVG.py` to create an SVG drawing. + +Only Gosper curve can be colored. +There is one threshold per iteration / level. The global threshold sets all other thresholds to the same value, it's just a shortcut. + +The lightness parameter enable to reduce the amount of black when in color mode. It does not have any effect in black and white mode. + +You can choose the scale and position of the image, and hide / show the image which will be drawn. + +### Convert JSON to SVG + +Use `python jsonToSVG.py -i path/to/drawing.json` to create the corresponding `drawing.svg`. You can add a small margin around the drawing with the `--margin` option (in percentage of the max side of the drawing bounding box). + ## How does it work? A [Space Filling Curve](https://en.wikipedia.org/wiki/Space-filling_curve) ([Hilbert curve](https://en.wikipedia.org/wiki/Hilbert_curve) or a [Gosper curve](https://en.wikipedia.org/wiki/Gosper_curve)) is computed from a grayscale image, refined where the image is darker than thredhold. diff --git a/jsonToSVG.py b/jsonToSVG.py index f812f1b..bc0b315 100644 --- a/jsonToSVG.py +++ b/jsonToSVG.py @@ -8,7 +8,7 @@ parser = argparse.ArgumentParser('JSON to SVG', description='Converts a json file generated with the space-filling-curve webpage to svg.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-i', '--input', help='Input json', required=True) -parser.add_argument('-s', '--sorted', action='store_true', help='Path is sorted. Use to skip points sorting.') +# parser.add_argument('-s', '--sorted', action='store_true', help='Path is sorted. Use to skip points sorting.') parser.add_argument('-m', '--margin', type=float, help='Margin (defined as a percentage of the max side of the drawing bounding box)', default=0.01) args = parser.parse_args() @@ -16,7 +16,8 @@ with open(args.input, 'r') as f: data = json.load(f) -fdata = data if args.sorted else [p for path in data for p in path] +# fdata = data if args.sorted else [p for path in data for p in path] +fdata = [p for path in data for p in path] xs = [p[0] for p in fdata] ys = [p[1] for p in fdata] @@ -29,36 +30,36 @@ def areClose(p1, p2): return abs(p2[0] - p1[0]) < 1e-6 and abs(p2[1] - p1[1]) < 1e-6 -if not args.sorted: - width = maxX - minX - height = maxY - minY - - # tree = quads.QuadTree((minX+width/2, minY+height/2), 2*width, 2*height) - ps = [[path[0], path[-1]] for path in data] - ps = [p for s in ps for p in s] - - print('Creating tree...') - tree = KDTree(ps) - - # for d in data: - # tree.insert(quads.Point(d[0][0], d[0][1]), data=(d, True)) - # tree.insert(quads.Point(d[-1][0], d[-1][1]), data=(d, False)) - print('Creating path...') - finalData = data[0].copy() - n = 1 - addedIndices = set() - addedIndices.add(0) - while n { let visible = preview.visible; preview.visible = false; let path = [] - if(parameters.sortPath) { - let cs = compoundPath.children.slice() - let firstChild = cs.shift() - let segments = firstChild.segments - path.push([segments[0].point.x, segments[0].point.y]) - while(cs.length>0) { - for(let i=1 ; i { if(value == 'hilbert') { parameters.nIterations = 9 - globalThresholdController.setValue(0.03) + parameters.globalThreshold = 0.03 parameters.margin = 0 } else if(value == 'gosper') { parameters.nIterations = 8 - globalThresholdController.setValue(0.02455) + parameters.globalThreshold = 0.85 parameters.margin = 0.2 } + globalThresholdController.setValue(parameters.globalThreshold) + for(let n=0 ; n { gui.add(parameters, 'color').onFinishChange(()=> { displayGeneratingAndDraw(); }); +gui.add(parameters, 'lightness', 0, 1, 0.01).onFinishChange(()=> { + displayGeneratingAndDraw(); +}); gui.add(parameters, 'nIterations', 1, nThresholds, 1).onFinishChange(()=> { displayGeneratingAndDraw(); @@ -466,5 +409,4 @@ gui.add(parameters, 'showImage').onFinishChange((value)=> { preview.visible = value; } }); -gui.add(parameters, 'sortPath'); gui.add(parameters, 'exportJSON');