discuss@lists.openscad.org

OpenSCAD general discussion

View all threads

surface()

JW
Joe Weinpert
Tue, May 10, 2022 8:12 PM

Cannot run surface() as a function to return a vnf array of the data to
manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?

Cannot run surface() as a function to return a vnf array of the data to manipulate. Is there a BOSL function that can do what surface() does? If not, is there another library out there that will do it?
RD
Revar Desmera
Tue, May 10, 2022 8:23 PM

BOSL2 has a function/module called heightfield() that you can feed an array of heights, or a function literal that can calculate the heights dynamically.  However, there is currently no way for an OpenSCAD script to read height values from an image file.  You can only generate 3D geometry using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

Cannot run surface() as a function to return a vnf array of the data to manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

BOSL2 has a function/module called `heightfield()` that you can feed an array of heights, or a function literal that can calculate the heights dynamically. However, there is currently no way for an OpenSCAD script to read height values from an image file. You can only generate 3D geometry using `surface()`. https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield <https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield> Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data. - Revar > On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > Cannot run surface() as a function to return a vnf array of the data to manipulate. > > Is there a BOSL function that can do what surface() does? > > If not, is there another library out there that will do it? > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
JW
Joe Weinpert
Tue, May 10, 2022 8:47 PM

Using surface( file = "Flourish_01b.png" ) on this png file:

[image: Flourish_01b.png]

results in this:

[image: result.png]

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a
problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works
great.  I can then use *vnf_polyhedron() *to draw it or draw copies and
mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you
say it can be ... I've never touched python in my life and never dealt with
the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera revarbat@gmail.com wrote:

BOSL2 has a function/module called heightfield() that you can feed an
array of heights, or a function literal that can calculate the heights
dynamically.  However, there is currently no way for an OpenSCAD script to
read height values from an image file.  You can only generate 3D geometry
using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python script
to read in an image file, and have it write out a .scad source file
containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

Cannot run surface() as a function to return a vnf array of the data to
manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Using *surface( file = "Flourish_01b.png" )* on this png file: [image: Flourish_01b.png] results in this: [image: result.png] Except for the square base, I was surprised it even worked. Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead. When using BOSL, I can create a nice vnf array from a sweep(). Works great. I can then use *vnf_polyhedron() *to draw it or draw copies and mirrors of it ... whatever. This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure. On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com> wrote: > BOSL2 has a function/module called `heightfield()` that you can feed an > array of heights, or a function literal that can calculate the heights > dynamically. However, there is currently no way for an OpenSCAD script to > read height values from an image file. You can only generate 3D geometry > using `surface()`. > > > https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield > > Having said that, it should not be too difficult to write a Python script > to read in an image file, and have it write out a .scad source file > containing the array of height data. > > - Revar > > > On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > Cannot run surface() as a function to return a vnf array of the data to > manipulate. > > Is there a BOSL function that can do what surface() does? > > If not, is there another library out there that will do it? > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
NH
nop head
Tue, May 10, 2022 9:01 PM

Python has libraries for everything so you should be able to read a PNG as
an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert joe.weinpert@gmail.com wrote:

Using surface( file = "Flourish_01b.png" ) on this png file:

[image: Flourish_01b.png]

results in this:

[image: result.png]

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a
problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works
great.  I can then use *vnf_polyhedron() *to draw it or draw copies and
mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you
say it can be ... I've never touched python in my life and never dealt with
the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera revarbat@gmail.com wrote:

BOSL2 has a function/module called heightfield() that you can feed an
array of heights, or a function literal that can calculate the heights
dynamically.  However, there is currently no way for an OpenSCAD script to
read height values from an image file.  You can only generate 3D geometry
using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python script
to read in an image file, and have it write out a .scad source file
containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

Cannot run surface() as a function to return a vnf array of the data to
manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure. On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com> wrote: > Using *surface( file = "Flourish_01b.png" )* on this png file: > > [image: Flourish_01b.png] > > results in this: > > [image: result.png] > > Except for the square base, I was surprised it even worked. > > Attempting to use pre-built STL files in SCAD always seems to cause a > problem, so I wanted to try using grayscale images instead. > > When using BOSL, I can create a nice vnf array from a sweep(). Works > great. I can then use *vnf_polyhedron() *to draw it or draw copies and > mirrors of it ... whatever. > > This is why I was hoping for a function ... however ... as simple as you > say it can be ... I've never touched python in my life and never dealt with > the PNG file structure. > > > On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com> wrote: > >> BOSL2 has a function/module called `heightfield()` that you can feed an >> array of heights, or a function literal that can calculate the heights >> dynamically. However, there is currently no way for an OpenSCAD script to >> read height values from an image file. You can only generate 3D geometry >> using `surface()`. >> >> >> https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield >> >> Having said that, it should not be too difficult to write a Python script >> to read in an image file, and have it write out a .scad source file >> containing the array of height data. >> >> - Revar >> >> >> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: >> >> Cannot run surface() as a function to return a vnf array of the data to >> manipulate. >> >> Is there a BOSL function that can do what surface() does? >> >> If not, is there another library out there that will do it? >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JW
Joe Weinpert
Tue, May 10, 2022 11:02 PM

No doubt ... but  I never used Python.

I think that openSCAD having a way to import and use external STLs by
handling them as data arrays would open up many doors. There are thousands
upon thousands of STL files out there.  Heck, openSCAD is used
for parameter customizing on many within Thingiverse alone.  Currently I
use converter
https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html to
convert an STL to a PNG. Super super super basic ... but it does work until
I can find something else.

I will continue searching for a converter to take files like the grayscale
png picture I posted and make height data maps.  Maybe they will work with
the BOSL heightfield() function ... you never know.

On Tue, May 10, 2022 at 5:03 PM nop head nop.head@gmail.com wrote:

Python has libraries for everything so you should be able to read a PNG as
an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert joe.weinpert@gmail.com wrote:

Using surface( file = "Flourish_01b.png" ) on this png file:

[image: Flourish_01b.png]

results in this:

[image: result.png]

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a
problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works
great.  I can then use *vnf_polyhedron() *to draw it or draw copies and
mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you
say it can be ... I've never touched python in my life and never dealt with
the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera revarbat@gmail.com wrote:

BOSL2 has a function/module called heightfield() that you can feed an
array of heights, or a function literal that can calculate the heights
dynamically.  However, there is currently no way for an OpenSCAD script to
read height values from an image file.  You can only generate 3D geometry
using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python
script to read in an image file, and have it write out a .scad source file
containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert joe.weinpert@gmail.com
wrote:

Cannot run surface() as a function to return a vnf array of the data to
manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

No doubt ... but I never used Python. I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there. Heck, openSCAD is used for parameter customizing on many within Thingiverse alone. Currently I use converter <https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html> to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else. I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps. Maybe they will work with the BOSL heightfield() function ... you never know. On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com> wrote: > Python has libraries for everything so you should be able to read a PNG as > an image and not need to know the file structure. > > On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com> wrote: > >> Using *surface( file = "Flourish_01b.png" )* on this png file: >> >> [image: Flourish_01b.png] >> >> results in this: >> >> [image: result.png] >> >> Except for the square base, I was surprised it even worked. >> >> Attempting to use pre-built STL files in SCAD always seems to cause a >> problem, so I wanted to try using grayscale images instead. >> >> When using BOSL, I can create a nice vnf array from a sweep(). Works >> great. I can then use *vnf_polyhedron() *to draw it or draw copies and >> mirrors of it ... whatever. >> >> This is why I was hoping for a function ... however ... as simple as you >> say it can be ... I've never touched python in my life and never dealt with >> the PNG file structure. >> >> >> On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com> wrote: >> >>> BOSL2 has a function/module called `heightfield()` that you can feed an >>> array of heights, or a function literal that can calculate the heights >>> dynamically. However, there is currently no way for an OpenSCAD script to >>> read height values from an image file. You can only generate 3D geometry >>> using `surface()`. >>> >>> >>> https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield >>> >>> Having said that, it should not be too difficult to write a Python >>> script to read in an image file, and have it write out a .scad source file >>> containing the array of height data. >>> >>> - Revar >>> >>> >>> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com> >>> wrote: >>> >>> Cannot run surface() as a function to return a vnf array of the data to >>> manipulate. >>> >>> Is there a BOSL function that can do what surface() does? >>> >>> If not, is there another library out there that will do it? >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RD
Revar Desmera
Tue, May 10, 2022 11:51 PM

I wrote up a simple Python image-to-scadfile converter.  It’ll read most common image types, convert them to Luminosity grayscale, with optional re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file.

  • Revar

Usage:
img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT

Script:
#!env python3

import re
import os
import sys
import os.path
import argparse

from PIL import Image

def img2scad(filename, varname, resize, outf):
indent = " " * 4
im = Image.open(filename).convert('L')
if resize:
print("Resizing to {}x{}".format(resize[0],resize[1]))
im = im.resize(resize)
pix = im.load()
width, height = im.size
print("// Image {} ({}x{})".format(filename, width, height), file=outf)
print("{} = [".format(varname), file=outf)
line = indent
for x in range(width):
line += "[ "
for y in range(height):
line += "{:d}, ".format(pix[x,y])
if len(line) > 60:
print(line, file=outf)
line = indent * 2
line += " ],"
if line != indent:
print(line, file=outf)
line = indent
print("];", file=outf)
print("", file=outf)

def main():
parser = argparse.ArgumentParser(prog='img2scad')
parser.add_argument('-o', '--outfile',
help='Output .scad file.')
parser.add_argument('-v', '--varname',
help='Variable to use in .scad file.')
parser.add_argument('-r', '--resize',
help='Resample image to WIDTHxHEIGHT.')
parser.add_argument('infile', help='Input image file.')
opts = parser.parse_args()

non_alnum = re.compile(r'[^a-zA-Z0-9_]')
if not opts.varname:
    if opts.outfile:
        opts.varname = os.path.splitext(os.path.basename(opts.outfile))[0]
        opts.varname = non_alnum.sub("", opts.varname)
    else:
        opts.varname = "image_data"
size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$')
if opts.resize:
    m = size_pat.match(opts.resize)
    if not m:
        print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr)
        sys.exit(-1)
    opts.resize = (int(m.group(1)), int(m.group(2)))

if not opts.varname or non_alnum.search(opts.varname):
    print("Bad variable name: {}".format(opts.varname), file=sys.stderr)
    sys.exit(-1)

if opts.outfile:
    with open(opts.outfile, "w") as outf:
        img2scad(opts.infile, opts.varname, opts.resize, outf)
else:
    img2scad(opts.infile, opts.varname, opts.resize, sys.stdout)

sys.exit(0)

if name == "main":
main()

On May 10, 2022, at 4:02 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

No doubt ... but  I never used Python.

I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there.  Heck, openSCAD is used for parameter customizing on many within Thingiverse alone.  Currently I use converter https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else.

I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps.  Maybe they will work with the BOSL heightfield() function ... you never know.

On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com mailto:nop.head@gmail.com> wrote:
Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:
Using surface( file = "Flourish_01b.png" ) on this png file:

<Flourish_01b.png>

results in this:

<result.png>

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works great.  I can then use vnf_polyhedron() to draw it or draw copies and mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com mailto:revarbat@gmail.com> wrote:
BOSL2 has a function/module called heightfield() that you can feed an array of heights, or a function literal that can calculate the heights dynamically.  However, there is currently no way for an OpenSCAD script to read height values from an image file.  You can only generate 3D geometry using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:

Cannot run surface() as a function to return a vnf array of the data to manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I wrote up a simple Python image-to-scadfile converter. It’ll read most common image types, convert them to Luminosity grayscale, with optional re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file. - Revar Usage: img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT Script: #!env python3 import re import os import sys import os.path import argparse from PIL import Image def img2scad(filename, varname, resize, outf): indent = " " * 4 im = Image.open(filename).convert('L') if resize: print("Resizing to {}x{}".format(resize[0],resize[1])) im = im.resize(resize) pix = im.load() width, height = im.size print("// Image {} ({}x{})".format(filename, width, height), file=outf) print("{} = [".format(varname), file=outf) line = indent for x in range(width): line += "[ " for y in range(height): line += "{:d}, ".format(pix[x,y]) if len(line) > 60: print(line, file=outf) line = indent * 2 line += " ]," if line != indent: print(line, file=outf) line = indent print("];", file=outf) print("", file=outf) def main(): parser = argparse.ArgumentParser(prog='img2scad') parser.add_argument('-o', '--outfile', help='Output .scad file.') parser.add_argument('-v', '--varname', help='Variable to use in .scad file.') parser.add_argument('-r', '--resize', help='Resample image to WIDTHxHEIGHT.') parser.add_argument('infile', help='Input image file.') opts = parser.parse_args() non_alnum = re.compile(r'[^a-zA-Z0-9_]') if not opts.varname: if opts.outfile: opts.varname = os.path.splitext(os.path.basename(opts.outfile))[0] opts.varname = non_alnum.sub("", opts.varname) else: opts.varname = "image_data" size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$') if opts.resize: m = size_pat.match(opts.resize) if not m: print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr) sys.exit(-1) opts.resize = (int(m.group(1)), int(m.group(2))) if not opts.varname or non_alnum.search(opts.varname): print("Bad variable name: {}".format(opts.varname), file=sys.stderr) sys.exit(-1) if opts.outfile: with open(opts.outfile, "w") as outf: img2scad(opts.infile, opts.varname, opts.resize, outf) else: img2scad(opts.infile, opts.varname, opts.resize, sys.stdout) sys.exit(0) if __name__ == "__main__": main() > On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > No doubt ... but I never used Python. > > I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there. Heck, openSCAD is used for parameter customizing on many within Thingiverse alone. Currently I use converter <https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html> to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else. > > I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps. Maybe they will work with the BOSL heightfield() function ... you never know. > > > On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com <mailto:nop.head@gmail.com>> wrote: > Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure. > > On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: > Using surface( file = "Flourish_01b.png" ) on this png file: > > <Flourish_01b.png> > > results in this: > > <result.png> > > Except for the square base, I was surprised it even worked. > > Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead. > > When using BOSL, I can create a nice vnf array from a sweep(). Works great. I can then use vnf_polyhedron() to draw it or draw copies and mirrors of it ... whatever. > > This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure. > > > On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com <mailto:revarbat@gmail.com>> wrote: > BOSL2 has a function/module called `heightfield()` that you can feed an array of heights, or a function literal that can calculate the heights dynamically. However, there is currently no way for an OpenSCAD script to read height values from an image file. You can only generate 3D geometry using `surface()`. > > https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield <https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield> > > Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data. > > - Revar > > >> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >> >> Cannot run surface() as a function to return a vnf array of the data to manipulate. >> >> Is there a BOSL function that can do what surface() does? >> >> If not, is there another library out there that will do it? >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
JW
Joe Weinpert
Wed, May 11, 2022 12:08 AM

Holy sh..!  How the heck do you write something THAT fast?

You have my thanks ... and in any way that this can be added to BOSL as
some sort of function would be something indeed!

I assume "mg2scad.py" is the file name to save the Python code as ... and
your usage example is "run command line" orientated.  But what do I need on
my desktop to run python apps?  I've never done a thing with Python that I
am aware of.

On Tue, May 10, 2022 at 7:51 PM Revar Desmera revarbat@gmail.com wrote:

I wrote up a simple Python image-to-scadfile converter.  It’ll read most
common image types, convert them to Luminosity grayscale, with optional
re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file.

  • Revar

Usage:
img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT

Script:

#!env python3

import re
import os
import sys
import os.path
import argparse

from PIL import Image

def img2scad(filename, varname, resize, outf):
indent = " " * 4
im = Image.open(filename).convert('L')
if resize:
print("Resizing to {}x{}".format(resize[0],resize[1]))
im = im.resize(resize)
pix = im.load()
width, height = im.size
print("// Image {} ({}x{})".format(filename, width, height), file=outf)
print("{} = [".format(varname), file=outf)
line = indent
for x in range(width):
line += "[ "
for y in range(height):
line += "{:d}, ".format(pix[x,y])
if len(line) > 60:
print(line, file=outf)
line = indent * 2
line += " ],"
if line != indent:
print(line, file=outf)
line = indent
print("];", file=outf)
print("", file=outf)

def main():
parser = argparse.ArgumentParser(prog='img2scad')
parser.add_argument('-o', '--outfile',
help='Output .scad file.')
parser.add_argument('-v', '--varname',
help='Variable to use in .scad file.')
parser.add_argument('-r', '--resize',
help='Resample image to WIDTHxHEIGHT.')
parser.add_argument('infile', help='Input image file.')
opts = parser.parse_args()

 non_alnum = re.compile(r'[^a-zA-Z0-9_]')
 if not opts.varname:
     if opts.outfile:
         opts.varname =

os.path.splitext(os.path.basename(opts.outfile))[0]
opts.varname = non_alnum.sub("", opts.varname)
else:
opts.varname = "image_data"
size_pat = re.compile(r'^([0-9][0-9])x([0-9][0-9])$')
if opts.resize:
m = size_pat.match(opts.resize)
if not m:
print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr)
sys.exit(-1)
opts.resize = (int(m.group(1)), int(m.group(2)))

 if not opts.varname or non_alnum.search(opts.varname):
     print("Bad variable name: {}".format(opts.varname),

file=sys.stderr)
sys.exit(-1)

 if opts.outfile:
     with open(opts.outfile, "w") as outf:
         img2scad(opts.infile, opts.varname, opts.resize, outf)
 else:
     img2scad(opts.infile, opts.varname, opts.resize, sys.stdout)

 sys.exit(0)

if name == "main":
main()

On May 10, 2022, at 4:02 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

No doubt ... but  I never used Python.

I think that openSCAD having a way to import and use external STLs by
handling them as data arrays would open up many doors. There are thousands
upon thousands of STL files out there.  Heck, openSCAD is used
for parameter customizing on many within Thingiverse alone.  Currently I
use converter
https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html to
convert an STL to a PNG. Super super super basic ... but it does work until
I can find something else.

I will continue searching for a converter to take files like the grayscale
png picture I posted and make height data maps.  Maybe they will work with
the BOSL heightfield() function ... you never know.

On Tue, May 10, 2022 at 5:03 PM nop head nop.head@gmail.com wrote:

Python has libraries for everything so you should be able to read a PNG
as an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert joe.weinpert@gmail.com
wrote:

Using surface( file = "Flourish_01b.png" ) on this png file:

<Flourish_01b.png>

results in this:

<result.png>

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a
problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works
great.  I can then use *vnf_polyhedron() *to draw it or draw copies and
mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you
say it can be ... I've never touched python in my life and never dealt with
the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera revarbat@gmail.com
wrote:

BOSL2 has a function/module called heightfield() that you can feed an
array of heights, or a function literal that can calculate the heights
dynamically.  However, there is currently no way for an OpenSCAD script to
read height values from an image file.  You can only generate 3D geometry
using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python
script to read in an image file, and have it write out a .scad source file
containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert joe.weinpert@gmail.com
wrote:

Cannot run surface() as a function to return a vnf array of the data to
manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Holy sh..! How the heck do you write something THAT fast? You have my thanks ... and in any way that this can be added to BOSL as some sort of function would be something indeed! I assume "mg2scad.py" is the file name to save the Python code as ... and your usage example is "run command line" orientated. But what do I need on my desktop to run python apps? I've never done a thing with Python that I am aware of. On Tue, May 10, 2022 at 7:51 PM Revar Desmera <revarbat@gmail.com> wrote: > I wrote up a simple Python image-to-scadfile converter. It’ll read most > common image types, convert them to Luminosity grayscale, with optional > re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file. > > - Revar > > > Usage: > img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT > > Script: > > #!env python3 > > import re > import os > import sys > import os.path > import argparse > > from PIL import Image > > > def img2scad(filename, varname, resize, outf): > indent = " " * 4 > im = Image.open(filename).convert('L') > if resize: > print("Resizing to {}x{}".format(resize[0],resize[1])) > im = im.resize(resize) > pix = im.load() > width, height = im.size > print("// Image {} ({}x{})".format(filename, width, height), file=outf) > print("{} = [".format(varname), file=outf) > line = indent > for x in range(width): > line += "[ " > for y in range(height): > line += "{:d}, ".format(pix[x,y]) > if len(line) > 60: > print(line, file=outf) > line = indent * 2 > line += " ]," > if line != indent: > print(line, file=outf) > line = indent > print("];", file=outf) > print("", file=outf) > > > def main(): > parser = argparse.ArgumentParser(prog='img2scad') > parser.add_argument('-o', '--outfile', > help='Output .scad file.') > parser.add_argument('-v', '--varname', > help='Variable to use in .scad file.') > parser.add_argument('-r', '--resize', > help='Resample image to WIDTHxHEIGHT.') > parser.add_argument('infile', help='Input image file.') > opts = parser.parse_args() > > non_alnum = re.compile(r'[^a-zA-Z0-9_]') > if not opts.varname: > if opts.outfile: > opts.varname = > os.path.splitext(os.path.basename(opts.outfile))[0] > opts.varname = non_alnum.sub("", opts.varname) > else: > opts.varname = "image_data" > size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$') > if opts.resize: > m = size_pat.match(opts.resize) > if not m: > print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr) > sys.exit(-1) > opts.resize = (int(m.group(1)), int(m.group(2))) > > if not opts.varname or non_alnum.search(opts.varname): > print("Bad variable name: {}".format(opts.varname), > file=sys.stderr) > sys.exit(-1) > > if opts.outfile: > with open(opts.outfile, "w") as outf: > img2scad(opts.infile, opts.varname, opts.resize, outf) > else: > img2scad(opts.infile, opts.varname, opts.resize, sys.stdout) > > sys.exit(0) > > > if __name__ == "__main__": > main() > > > > On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > No doubt ... but I never used Python. > > I think that openSCAD having a way to import and use external STLs by > handling them as data arrays would open up many doors. There are thousands > upon thousands of STL files out there. Heck, openSCAD is used > for parameter customizing on many within Thingiverse alone. Currently I > use converter > <https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html> to > convert an STL to a PNG. Super super super basic ... but it does work until > I can find something else. > > I will continue searching for a converter to take files like the grayscale > png picture I posted and make height data maps. Maybe they will work with > the BOSL heightfield() function ... you never know. > > > On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com> wrote: > >> Python has libraries for everything so you should be able to read a PNG >> as an image and not need to know the file structure. >> >> On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com> >> wrote: >> >>> Using *surface( file = "Flourish_01b.png" )* on this png file: >>> >>> <Flourish_01b.png> >>> >>> results in this: >>> >>> <result.png> >>> >>> Except for the square base, I was surprised it even worked. >>> >>> Attempting to use pre-built STL files in SCAD always seems to cause a >>> problem, so I wanted to try using grayscale images instead. >>> >>> When using BOSL, I can create a nice vnf array from a sweep(). Works >>> great. I can then use *vnf_polyhedron() *to draw it or draw copies and >>> mirrors of it ... whatever. >>> >>> This is why I was hoping for a function ... however ... as simple as you >>> say it can be ... I've never touched python in my life and never dealt with >>> the PNG file structure. >>> >>> >>> On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com> >>> wrote: >>> >>>> BOSL2 has a function/module called `heightfield()` that you can feed an >>>> array of heights, or a function literal that can calculate the heights >>>> dynamically. However, there is currently no way for an OpenSCAD script to >>>> read height values from an image file. You can only generate 3D geometry >>>> using `surface()`. >>>> >>>> >>>> https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield >>>> >>>> Having said that, it should not be too difficult to write a Python >>>> script to read in an image file, and have it write out a .scad source file >>>> containing the array of height data. >>>> >>>> - Revar >>>> >>>> >>>> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com> >>>> wrote: >>>> >>>> Cannot run surface() as a function to return a vnf array of the data to >>>> manipulate. >>>> >>>> Is there a BOSL function that can do what surface() does? >>>> >>>> If not, is there another library out there that will do it? >>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RD
Revar Desmera
Wed, May 11, 2022 12:22 AM

Yes, save the script to the file img2scad.py.  I’m afraid that this is just a command-line tool, and while it’s easy enough to use on Linux or a Mac, I’m not truly sure about how to set up a command-line on Windows.  A quick googling results in https://www.geeksforgeeks.org/how-to-install-python-on-windows/ https://www.geeksforgeeks.org/how-to-install-python-on-windows/ which may be helpful.

This script would probably better fit in a separate BOSL2-helpers repository.

A GUI version is perfectly possible to do, but that may take me a couple weeks to refresh my memory on how to do cross-platform.

  • Revar

On May 10, 2022, at 5:08 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

Holy sh..!  How the heck do you write something THAT fast?

You have my thanks ... and in any way that this can be added to BOSL as some sort of function would be something indeed!

I assume "mg2scad.py" is the file name to save the Python code as ... and your usage example is "run command line" orientated.  But what do I need on my desktop to run python apps?  I've never done a thing with Python that I am aware of.

On Tue, May 10, 2022 at 7:51 PM Revar Desmera <revarbat@gmail.com mailto:revarbat@gmail.com> wrote:
I wrote up a simple Python image-to-scadfile converter.  It’ll read most common image types, convert them to Luminosity grayscale, with optional re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file.

  • Revar

Usage:
img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT

Script:
#!env python3

import re
import os
import sys
import os.path
import argparse

from PIL import Image

def img2scad(filename, varname, resize, outf):
indent = " " * 4
im = Image.open(filename).convert('L')
if resize:
print("Resizing to {}x{}".format(resize[0],resize[1]))
im = im.resize(resize)
pix = im.load()
width, height = im.size
print("// Image {} ({}x{})".format(filename, width, height), file=outf)
print("{} = [".format(varname), file=outf)
line = indent
for x in range(width):
line += "[ "
for y in range(height):
line += "{:d}, ".format(pix[x,y])
if len(line) > 60:
print(line, file=outf)
line = indent * 2
line += " ],"
if line != indent:
print(line, file=outf)
line = indent
print("];", file=outf)
print("", file=outf)

def main():
parser = argparse.ArgumentParser(prog='img2scad')
parser.add_argument('-o', '--outfile',
help='Output .scad file.')
parser.add_argument('-v', '--varname',
help='Variable to use in .scad file.')
parser.add_argument('-r', '--resize',
help='Resample image to WIDTHxHEIGHT.')
parser.add_argument('infile', help='Input image file.')
opts = parser.parse_args()

 non_alnum = re.compile(r'[^a-zA-Z0-9_]')
 if not opts.varname:
     if opts.outfile:
         opts.varname = os.path.splitext(os.path.basename(opts.outfile))[0]
         opts.varname = non_alnum.sub("", opts.varname)
     else:
         opts.varname = "image_data"
 size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$')
 if opts.resize:
     m = size_pat.match(opts.resize)
     if not m:
         print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr)
         sys.exit(-1)
     opts.resize = (int(m.group(1)), int(m.group(2)))

 if not opts.varname or non_alnum.search(opts.varname):
     print("Bad variable name: {}".format(opts.varname), file=sys.stderr)
     sys.exit(-1)

 if opts.outfile:
     with open(opts.outfile, "w") as outf:
         img2scad(opts.infile, opts.varname, opts.resize, outf)
 else:
     img2scad(opts.infile, opts.varname, opts.resize, sys.stdout)

 sys.exit(0)

if name == "main":
main()

On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:

No doubt ... but  I never used Python.

I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there.  Heck, openSCAD is used for parameter customizing on many within Thingiverse alone.  Currently I use converter https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else.

I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps.  Maybe they will work with the BOSL heightfield() function ... you never know.

On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com mailto:nop.head@gmail.com> wrote:
Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:
Using surface( file = "Flourish_01b.png" ) on this png file:

<Flourish_01b.png>

results in this:

<result.png>

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works great.  I can then use vnf_polyhedron() to draw it or draw copies and mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com mailto:revarbat@gmail.com> wrote:
BOSL2 has a function/module called heightfield() that you can feed an array of heights, or a function literal that can calculate the heights dynamically.  However, there is currently no way for an OpenSCAD script to read height values from an image file.  You can only generate 3D geometry using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:

Cannot run surface() as a function to return a vnf array of the data to manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Yes, save the script to the file `img2scad.py`. I’m afraid that this is just a command-line tool, and while it’s easy enough to use on Linux or a Mac, I’m not truly sure about how to set up a command-line on Windows. A quick googling results in https://www.geeksforgeeks.org/how-to-install-python-on-windows/ <https://www.geeksforgeeks.org/how-to-install-python-on-windows/> which may be helpful. This script would probably better fit in a separate BOSL2-helpers repository. A GUI version is perfectly possible to do, but that may take me a couple weeks to refresh my memory on how to do cross-platform. - Revar > On May 10, 2022, at 5:08 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > Holy sh..! How the heck do you write something THAT fast? > > You have my thanks ... and in any way that this can be added to BOSL as some sort of function would be something indeed! > > I assume "mg2scad.py" is the file name to save the Python code as ... and your usage example is "run command line" orientated. But what do I need on my desktop to run python apps? I've never done a thing with Python that I am aware of. > > > > > > On Tue, May 10, 2022 at 7:51 PM Revar Desmera <revarbat@gmail.com <mailto:revarbat@gmail.com>> wrote: > I wrote up a simple Python image-to-scadfile converter. It’ll read most common image types, convert them to Luminosity grayscale, with optional re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file. > > - Revar > > > Usage: > img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT > > Script: > #!env python3 > > import re > import os > import sys > import os.path > import argparse > > from PIL import Image > > > def img2scad(filename, varname, resize, outf): > indent = " " * 4 > im = Image.open(filename).convert('L') > if resize: > print("Resizing to {}x{}".format(resize[0],resize[1])) > im = im.resize(resize) > pix = im.load() > width, height = im.size > print("// Image {} ({}x{})".format(filename, width, height), file=outf) > print("{} = [".format(varname), file=outf) > line = indent > for x in range(width): > line += "[ " > for y in range(height): > line += "{:d}, ".format(pix[x,y]) > if len(line) > 60: > print(line, file=outf) > line = indent * 2 > line += " ]," > if line != indent: > print(line, file=outf) > line = indent > print("];", file=outf) > print("", file=outf) > > > def main(): > parser = argparse.ArgumentParser(prog='img2scad') > parser.add_argument('-o', '--outfile', > help='Output .scad file.') > parser.add_argument('-v', '--varname', > help='Variable to use in .scad file.') > parser.add_argument('-r', '--resize', > help='Resample image to WIDTHxHEIGHT.') > parser.add_argument('infile', help='Input image file.') > opts = parser.parse_args() > > non_alnum = re.compile(r'[^a-zA-Z0-9_]') > if not opts.varname: > if opts.outfile: > opts.varname = os.path.splitext(os.path.basename(opts.outfile))[0] > opts.varname = non_alnum.sub("", opts.varname) > else: > opts.varname = "image_data" > size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$') > if opts.resize: > m = size_pat.match(opts.resize) > if not m: > print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr) > sys.exit(-1) > opts.resize = (int(m.group(1)), int(m.group(2))) > > if not opts.varname or non_alnum.search(opts.varname): > print("Bad variable name: {}".format(opts.varname), file=sys.stderr) > sys.exit(-1) > > if opts.outfile: > with open(opts.outfile, "w") as outf: > img2scad(opts.infile, opts.varname, opts.resize, outf) > else: > img2scad(opts.infile, opts.varname, opts.resize, sys.stdout) > > sys.exit(0) > > > if __name__ == "__main__": > main() > > > >> On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >> >> No doubt ... but I never used Python. >> >> I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there. Heck, openSCAD is used for parameter customizing on many within Thingiverse alone. Currently I use converter <https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html> to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else. >> >> I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps. Maybe they will work with the BOSL heightfield() function ... you never know. >> >> >> On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com <mailto:nop.head@gmail.com>> wrote: >> Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure. >> >> On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >> Using surface( file = "Flourish_01b.png" ) on this png file: >> >> <Flourish_01b.png> >> >> results in this: >> >> <result.png> >> >> Except for the square base, I was surprised it even worked. >> >> Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead. >> >> When using BOSL, I can create a nice vnf array from a sweep(). Works great. I can then use vnf_polyhedron() to draw it or draw copies and mirrors of it ... whatever. >> >> This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure. >> >> >> On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com <mailto:revarbat@gmail.com>> wrote: >> BOSL2 has a function/module called `heightfield()` that you can feed an array of heights, or a function literal that can calculate the heights dynamically. However, there is currently no way for an OpenSCAD script to read height values from an image file. You can only generate 3D geometry using `surface()`. >> >> https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield <https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield> >> >> Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data. >> >> - Revar >> >> >>> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >>> >>> Cannot run surface() as a function to return a vnf array of the data to manipulate. >>> >>> Is there a BOSL function that can do what surface() does? >>> >>> If not, is there another library out there that will do it? >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
JW
Joe Weinpert
Wed, May 11, 2022 1:15 AM

I am receiving this error msg when I run the script:

Line 10: ModuleNotFoundError: No module named 'PIL'

Any ideas?

On Tue, May 10, 2022 at 8:23 PM Revar Desmera revarbat@gmail.com wrote:

Yes, save the script to the file img2scad.py.  I’m afraid that this is
just a command-line tool, and while it’s easy enough to use on Linux or a
Mac, I’m not truly sure about how to set up a command-line on Windows.  A
quick googling results in
https://www.geeksforgeeks.org/how-to-install-python-on-windows/ which may
be helpful.

This script would probably better fit in a separate BOSL2-helpers
repository.

A GUI version is perfectly possible to do, but that may take me a couple
weeks to refresh my memory on how to do cross-platform.

  • Revar

On May 10, 2022, at 5:08 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

Holy sh..!  How the heck do you write something THAT fast?

You have my thanks ... and in any way that this can be added to BOSL as
some sort of function would be something indeed!

I assume "mg2scad.py" is the file name to save the Python code as ... and
your usage example is "run command line" orientated.  But what do I need on
my desktop to run python apps?  I've never done a thing with Python that I
am aware of.

On Tue, May 10, 2022 at 7:51 PM Revar Desmera revarbat@gmail.com wrote:

I wrote up a simple Python image-to-scadfile converter.  It’ll read most
common image types, convert them to Luminosity grayscale, with optional
re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file.

  • Revar

Usage:
img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT

Script:

#!env python3

import re
import os
import sys
import os.path
import argparse

from PIL import Image

def img2scad(filename, varname, resize, outf):
indent = " " * 4
im = Image.open(filename).convert('L')
if resize:
print("Resizing to {}x{}".format(resize[0],resize[1]))
im = im.resize(resize)
pix = im.load()
width, height = im.size
print("// Image {} ({}x{})".format(filename, width, height),
file=outf)
print("{} = [".format(varname), file=outf)
line = indent
for x in range(width):
line += "[ "
for y in range(height):
line += "{:d}, ".format(pix[x,y])
if len(line) > 60:
print(line, file=outf)
line = indent * 2
line += " ],"
if line != indent:
print(line, file=outf)
line = indent
print("];", file=outf)
print("", file=outf)

def main():
parser = argparse.ArgumentParser(prog='img2scad')
parser.add_argument('-o', '--outfile',
help='Output .scad file.')
parser.add_argument('-v', '--varname',
help='Variable to use in .scad file.')
parser.add_argument('-r', '--resize',
help='Resample image to WIDTHxHEIGHT.')
parser.add_argument('infile', help='Input image file.')
opts = parser.parse_args()

 non_alnum = re.compile(r'[^a-zA-Z0-9_]')
 if not opts.varname:
     if opts.outfile:
         opts.varname =

os.path.splitext(os.path.basename(opts.outfile))[0]
opts.varname = non_alnum.sub("", opts.varname)
else:
opts.varname = "image_data"
size_pat = re.compile(r'^([0-9][0-9])x([0-9][0-9])$')
if opts.resize:
m = size_pat.match(opts.resize)
if not m:
print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr)
sys.exit(-1)
opts.resize = (int(m.group(1)), int(m.group(2)))

 if not opts.varname or non_alnum.search(opts.varname):
     print("Bad variable name: {}".format(opts.varname),

file=sys.stderr)
sys.exit(-1)

 if opts.outfile:
     with open(opts.outfile, "w") as outf:
         img2scad(opts.infile, opts.varname, opts.resize, outf)
 else:
     img2scad(opts.infile, opts.varname, opts.resize, sys.stdout)

 sys.exit(0)

if name == "main":
main()

On May 10, 2022, at 4:02 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

No doubt ... but  I never used Python.

I think that openSCAD having a way to import and use external STLs by
handling them as data arrays would open up many doors. There are thousands
upon thousands of STL files out there.  Heck, openSCAD is used
for parameter customizing on many within Thingiverse alone.  Currently I
use converter
https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html to
convert an STL to a PNG. Super super super basic ... but it does work until
I can find something else.

I will continue searching for a converter to take files like the
grayscale png picture I posted and make height data maps.  Maybe they will
work with the BOSL heightfield() function ... you never know.

On Tue, May 10, 2022 at 5:03 PM nop head nop.head@gmail.com wrote:

Python has libraries for everything so you should be able to read a PNG
as an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert joe.weinpert@gmail.com
wrote:

Using surface( file = "Flourish_01b.png" ) on this png file:

<Flourish_01b.png>

results in this:

<result.png>

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a
problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works
great.  I can then use *vnf_polyhedron() *to draw it or draw copies
and mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as
you say it can be ... I've never touched python in my life and never dealt
with the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera revarbat@gmail.com
wrote:

BOSL2 has a function/module called heightfield() that you can feed
an array of heights, or a function literal that can calculate the heights
dynamically.  However, there is currently no way for an OpenSCAD script to
read height values from an image file.  You can only generate 3D geometry
using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python
script to read in an image file, and have it write out a .scad source file
containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert joe.weinpert@gmail.com
wrote:

Cannot run surface() as a function to return a vnf array of the data
to manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I am receiving this error msg when I run the script: Line 10: ModuleNotFoundError: No module named 'PIL' Any ideas? On Tue, May 10, 2022 at 8:23 PM Revar Desmera <revarbat@gmail.com> wrote: > Yes, save the script to the file `img2scad.py`. I’m afraid that this is > just a command-line tool, and while it’s easy enough to use on Linux or a > Mac, I’m not truly sure about how to set up a command-line on Windows. A > quick googling results in > https://www.geeksforgeeks.org/how-to-install-python-on-windows/ which may > be helpful. > > This script would probably better fit in a separate BOSL2-helpers > repository. > > A GUI version is perfectly possible to do, but that may take me a couple > weeks to refresh my memory on how to do cross-platform. > > - Revar > > > On May 10, 2022, at 5:08 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > Holy sh..! How the heck do you write something THAT fast? > > You have my thanks ... and in any way that this can be added to BOSL as > some sort of function would be something indeed! > > I assume "mg2scad.py" is the file name to save the Python code as ... and > your usage example is "run command line" orientated. But what do I need on > my desktop to run python apps? I've never done a thing with Python that I > am aware of. > > > > > > On Tue, May 10, 2022 at 7:51 PM Revar Desmera <revarbat@gmail.com> wrote: > >> I wrote up a simple Python image-to-scadfile converter. It’ll read most >> common image types, convert them to Luminosity grayscale, with optional >> re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file. >> >> - Revar >> >> >> Usage: >> img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT >> >> Script: >> >> #!env python3 >> >> import re >> import os >> import sys >> import os.path >> import argparse >> >> from PIL import Image >> >> >> def img2scad(filename, varname, resize, outf): >> indent = " " * 4 >> im = Image.open(filename).convert('L') >> if resize: >> print("Resizing to {}x{}".format(resize[0],resize[1])) >> im = im.resize(resize) >> pix = im.load() >> width, height = im.size >> print("// Image {} ({}x{})".format(filename, width, height), >> file=outf) >> print("{} = [".format(varname), file=outf) >> line = indent >> for x in range(width): >> line += "[ " >> for y in range(height): >> line += "{:d}, ".format(pix[x,y]) >> if len(line) > 60: >> print(line, file=outf) >> line = indent * 2 >> line += " ]," >> if line != indent: >> print(line, file=outf) >> line = indent >> print("];", file=outf) >> print("", file=outf) >> >> >> def main(): >> parser = argparse.ArgumentParser(prog='img2scad') >> parser.add_argument('-o', '--outfile', >> help='Output .scad file.') >> parser.add_argument('-v', '--varname', >> help='Variable to use in .scad file.') >> parser.add_argument('-r', '--resize', >> help='Resample image to WIDTHxHEIGHT.') >> parser.add_argument('infile', help='Input image file.') >> opts = parser.parse_args() >> >> non_alnum = re.compile(r'[^a-zA-Z0-9_]') >> if not opts.varname: >> if opts.outfile: >> opts.varname = >> os.path.splitext(os.path.basename(opts.outfile))[0] >> opts.varname = non_alnum.sub("", opts.varname) >> else: >> opts.varname = "image_data" >> size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$') >> if opts.resize: >> m = size_pat.match(opts.resize) >> if not m: >> print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr) >> sys.exit(-1) >> opts.resize = (int(m.group(1)), int(m.group(2))) >> >> if not opts.varname or non_alnum.search(opts.varname): >> print("Bad variable name: {}".format(opts.varname), >> file=sys.stderr) >> sys.exit(-1) >> >> if opts.outfile: >> with open(opts.outfile, "w") as outf: >> img2scad(opts.infile, opts.varname, opts.resize, outf) >> else: >> img2scad(opts.infile, opts.varname, opts.resize, sys.stdout) >> >> sys.exit(0) >> >> >> if __name__ == "__main__": >> main() >> >> >> >> On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: >> >> No doubt ... but I never used Python. >> >> I think that openSCAD having a way to import and use external STLs by >> handling them as data arrays would open up many doors. There are thousands >> upon thousands of STL files out there. Heck, openSCAD is used >> for parameter customizing on many within Thingiverse alone. Currently I >> use converter >> <https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html> to >> convert an STL to a PNG. Super super super basic ... but it does work until >> I can find something else. >> >> I will continue searching for a converter to take files like the >> grayscale png picture I posted and make height data maps. Maybe they will >> work with the BOSL heightfield() function ... you never know. >> >> >> On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com> wrote: >> >>> Python has libraries for everything so you should be able to read a PNG >>> as an image and not need to know the file structure. >>> >>> On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com> >>> wrote: >>> >>>> Using *surface( file = "Flourish_01b.png" )* on this png file: >>>> >>>> <Flourish_01b.png> >>>> >>>> results in this: >>>> >>>> <result.png> >>>> >>>> Except for the square base, I was surprised it even worked. >>>> >>>> Attempting to use pre-built STL files in SCAD always seems to cause a >>>> problem, so I wanted to try using grayscale images instead. >>>> >>>> When using BOSL, I can create a nice vnf array from a sweep(). Works >>>> great. I can then use *vnf_polyhedron() *to draw it or draw copies >>>> and mirrors of it ... whatever. >>>> >>>> This is why I was hoping for a function ... however ... as simple as >>>> you say it can be ... I've never touched python in my life and never dealt >>>> with the PNG file structure. >>>> >>>> >>>> On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com> >>>> wrote: >>>> >>>>> BOSL2 has a function/module called `heightfield()` that you can feed >>>>> an array of heights, or a function literal that can calculate the heights >>>>> dynamically. However, there is currently no way for an OpenSCAD script to >>>>> read height values from an image file. You can only generate 3D geometry >>>>> using `surface()`. >>>>> >>>>> >>>>> https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield >>>>> >>>>> Having said that, it should not be too difficult to write a Python >>>>> script to read in an image file, and have it write out a .scad source file >>>>> containing the array of height data. >>>>> >>>>> - Revar >>>>> >>>>> >>>>> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com> >>>>> wrote: >>>>> >>>>> Cannot run surface() as a function to return a vnf array of the data >>>>> to manipulate. >>>>> >>>>> Is there a BOSL function that can do what surface() does? >>>>> >>>>> If not, is there another library out there that will do it? >>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RD
Revar Desmera
Wed, May 11, 2022 1:19 AM

You need to install the Pillow library.  Usually this can be done simplest from the command-line with the command:

pip install —upgrade Pillow

More info on installing Pillow: https://pillow.readthedocs.io/en/latest/installation.html https://pillow.readthedocs.io/en/latest/installation.html

  • Revar

On May 10, 2022, at 6:15 PM, Joe Weinpert joe.weinpert@gmail.com wrote:

I am receiving this error msg when I run the script:

Line 10: ModuleNotFoundError: No module named 'PIL'

Any ideas?

On Tue, May 10, 2022 at 8:23 PM Revar Desmera <revarbat@gmail.com mailto:revarbat@gmail.com> wrote:
Yes, save the script to the file img2scad.py.  I’m afraid that this is just a command-line tool, and while it’s easy enough to use on Linux or a Mac, I’m not truly sure about how to set up a command-line on Windows.  A quick googling results in https://www.geeksforgeeks.org/how-to-install-python-on-windows/ https://www.geeksforgeeks.org/how-to-install-python-on-windows/ which may be helpful.

This script would probably better fit in a separate BOSL2-helpers repository.

A GUI version is perfectly possible to do, but that may take me a couple weeks to refresh my memory on how to do cross-platform.

  • Revar

On May 10, 2022, at 5:08 PM, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:

Holy sh..!  How the heck do you write something THAT fast?

You have my thanks ... and in any way that this can be added to BOSL as some sort of function would be something indeed!

I assume "mg2scad.py" is the file name to save the Python code as ... and your usage example is "run command line" orientated.  But what do I need on my desktop to run python apps?  I've never done a thing with Python that I am aware of.

On Tue, May 10, 2022 at 7:51 PM Revar Desmera <revarbat@gmail.com mailto:revarbat@gmail.com> wrote:
I wrote up a simple Python image-to-scadfile converter.  It’ll read most common image types, convert them to Luminosity grayscale, with optional re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file.

  • Revar

Usage:
img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT

Script:
#!env python3

import re
import os
import sys
import os.path
import argparse

from PIL import Image

def img2scad(filename, varname, resize, outf):
indent = " " * 4
im = Image.open(filename).convert('L')
if resize:
print("Resizing to {}x{}".format(resize[0],resize[1]))
im = im.resize(resize)
pix = im.load()
width, height = im.size
print("// Image {} ({}x{})".format(filename, width, height), file=outf)
print("{} = [".format(varname), file=outf)
line = indent
for x in range(width):
line += "[ "
for y in range(height):
line += "{:d}, ".format(pix[x,y])
if len(line) > 60:
print(line, file=outf)
line = indent * 2
line += " ],"
if line != indent:
print(line, file=outf)
line = indent
print("];", file=outf)
print("", file=outf)

def main():
parser = argparse.ArgumentParser(prog='img2scad')
parser.add_argument('-o', '--outfile',
help='Output .scad file.')
parser.add_argument('-v', '--varname',
help='Variable to use in .scad file.')
parser.add_argument('-r', '--resize',
help='Resample image to WIDTHxHEIGHT.')
parser.add_argument('infile', help='Input image file.')
opts = parser.parse_args()

 non_alnum = re.compile(r'[^a-zA-Z0-9_]')
 if not opts.varname:
     if opts.outfile:
         opts.varname = os.path.splitext(os.path.basename(opts.outfile))[0]
         opts.varname = non_alnum.sub("", opts.varname)
     else:
         opts.varname = "image_data"
 size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$')
 if opts.resize:
     m = size_pat.match(opts.resize)
     if not m:
         print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr)
         sys.exit(-1)
     opts.resize = (int(m.group(1)), int(m.group(2)))

 if not opts.varname or non_alnum.search(opts.varname):
     print("Bad variable name: {}".format(opts.varname), file=sys.stderr)
     sys.exit(-1)

 if opts.outfile:
     with open(opts.outfile, "w") as outf:
         img2scad(opts.infile, opts.varname, opts.resize, outf)
 else:
     img2scad(opts.infile, opts.varname, opts.resize, sys.stdout)

 sys.exit(0)

if name == "main":
main()

On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:

No doubt ... but  I never used Python.

I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there.  Heck, openSCAD is used for parameter customizing on many within Thingiverse alone.  Currently I use converter https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else.

I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps.  Maybe they will work with the BOSL heightfield() function ... you never know.

On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com mailto:nop.head@gmail.com> wrote:
Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure.

On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:
Using surface( file = "Flourish_01b.png" ) on this png file:

<Flourish_01b.png>

results in this:

<result.png>

Except for the square base, I was surprised it even worked.

Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead.

When using BOSL, I can create a nice vnf array from a sweep().  Works great.  I can then use vnf_polyhedron() to draw it or draw copies and mirrors of it ... whatever.

This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure.

On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com mailto:revarbat@gmail.com> wrote:
BOSL2 has a function/module called heightfield() that you can feed an array of heights, or a function literal that can calculate the heights dynamically.  However, there is currently no way for an OpenSCAD script to read height values from an image file.  You can only generate 3D geometry using surface().

https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield

Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data.

  • Revar

On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com mailto:joe.weinpert@gmail.com> wrote:

Cannot run surface() as a function to return a vnf array of the data to manipulate.

Is there a BOSL function that can do what surface() does?

If not, is there another library out there that will do it?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org mailto:discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

You need to install the Pillow library. Usually this can be done simplest from the command-line with the command: pip install —upgrade Pillow More info on installing Pillow: https://pillow.readthedocs.io/en/latest/installation.html <https://pillow.readthedocs.io/en/latest/installation.html> - Revar > On May 10, 2022, at 6:15 PM, Joe Weinpert <joe.weinpert@gmail.com> wrote: > > I am receiving this error msg when I run the script: > > Line 10: ModuleNotFoundError: No module named 'PIL' > > Any ideas? > > > On Tue, May 10, 2022 at 8:23 PM Revar Desmera <revarbat@gmail.com <mailto:revarbat@gmail.com>> wrote: > Yes, save the script to the file `img2scad.py`. I’m afraid that this is just a command-line tool, and while it’s easy enough to use on Linux or a Mac, I’m not truly sure about how to set up a command-line on Windows. A quick googling results in https://www.geeksforgeeks.org/how-to-install-python-on-windows/ <https://www.geeksforgeeks.org/how-to-install-python-on-windows/> which may be helpful. > > This script would probably better fit in a separate BOSL2-helpers repository. > > A GUI version is perfectly possible to do, but that may take me a couple weeks to refresh my memory on how to do cross-platform. > > - Revar > > >> On May 10, 2022, at 5:08 PM, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >> >> Holy sh..! How the heck do you write something THAT fast? >> >> You have my thanks ... and in any way that this can be added to BOSL as some sort of function would be something indeed! >> >> I assume "mg2scad.py" is the file name to save the Python code as ... and your usage example is "run command line" orientated. But what do I need on my desktop to run python apps? I've never done a thing with Python that I am aware of. >> >> >> >> >> >> On Tue, May 10, 2022 at 7:51 PM Revar Desmera <revarbat@gmail.com <mailto:revarbat@gmail.com>> wrote: >> I wrote up a simple Python image-to-scadfile converter. It’ll read most common image types, convert them to Luminosity grayscale, with optional re-sizing, and output it to a 2-dimensional array in a OpenSCAD source file. >> >> - Revar >> >> >> Usage: >> img2scad.py INFILE -o OUTSCADFILE -v VARNAME -r WIDTHxHEIGHT >> >> Script: >> #!env python3 >> >> import re >> import os >> import sys >> import os.path >> import argparse >> >> from PIL import Image >> >> >> def img2scad(filename, varname, resize, outf): >> indent = " " * 4 >> im = Image.open(filename).convert('L') >> if resize: >> print("Resizing to {}x{}".format(resize[0],resize[1])) >> im = im.resize(resize) >> pix = im.load() >> width, height = im.size >> print("// Image {} ({}x{})".format(filename, width, height), file=outf) >> print("{} = [".format(varname), file=outf) >> line = indent >> for x in range(width): >> line += "[ " >> for y in range(height): >> line += "{:d}, ".format(pix[x,y]) >> if len(line) > 60: >> print(line, file=outf) >> line = indent * 2 >> line += " ]," >> if line != indent: >> print(line, file=outf) >> line = indent >> print("];", file=outf) >> print("", file=outf) >> >> >> def main(): >> parser = argparse.ArgumentParser(prog='img2scad') >> parser.add_argument('-o', '--outfile', >> help='Output .scad file.') >> parser.add_argument('-v', '--varname', >> help='Variable to use in .scad file.') >> parser.add_argument('-r', '--resize', >> help='Resample image to WIDTHxHEIGHT.') >> parser.add_argument('infile', help='Input image file.') >> opts = parser.parse_args() >> >> non_alnum = re.compile(r'[^a-zA-Z0-9_]') >> if not opts.varname: >> if opts.outfile: >> opts.varname = os.path.splitext(os.path.basename(opts.outfile))[0] >> opts.varname = non_alnum.sub("", opts.varname) >> else: >> opts.varname = "image_data" >> size_pat = re.compile(r'^([0-9][0-9]*)x([0-9][0-9]*)$') >> if opts.resize: >> m = size_pat.match(opts.resize) >> if not m: >> print("Expected WIDTHxHEIGHT resize format.", file=sys.stderr) >> sys.exit(-1) >> opts.resize = (int(m.group(1)), int(m.group(2))) >> >> if not opts.varname or non_alnum.search(opts.varname): >> print("Bad variable name: {}".format(opts.varname), file=sys.stderr) >> sys.exit(-1) >> >> if opts.outfile: >> with open(opts.outfile, "w") as outf: >> img2scad(opts.infile, opts.varname, opts.resize, outf) >> else: >> img2scad(opts.infile, opts.varname, opts.resize, sys.stdout) >> >> sys.exit(0) >> >> >> if __name__ == "__main__": >> main() >> >> >> >>> On May 10, 2022, at 4:02 PM, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >>> >>> No doubt ... but I never used Python. >>> >>> I think that openSCAD having a way to import and use external STLs by handling them as data arrays would open up many doors. There are thousands upon thousands of STL files out there. Heck, openSCAD is used for parameter customizing on many within Thingiverse alone. Currently I use converter <https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html> to convert an STL to a PNG. Super super super basic ... but it does work until I can find something else. >>> >>> I will continue searching for a converter to take files like the grayscale png picture I posted and make height data maps. Maybe they will work with the BOSL heightfield() function ... you never know. >>> >>> >>> On Tue, May 10, 2022 at 5:03 PM nop head <nop.head@gmail.com <mailto:nop.head@gmail.com>> wrote: >>> Python has libraries for everything so you should be able to read a PNG as an image and not need to know the file structure. >>> >>> On Tue, 10 May 2022 at 21:48, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >>> Using surface( file = "Flourish_01b.png" ) on this png file: >>> >>> <Flourish_01b.png> >>> >>> results in this: >>> >>> <result.png> >>> >>> Except for the square base, I was surprised it even worked. >>> >>> Attempting to use pre-built STL files in SCAD always seems to cause a problem, so I wanted to try using grayscale images instead. >>> >>> When using BOSL, I can create a nice vnf array from a sweep(). Works great. I can then use vnf_polyhedron() to draw it or draw copies and mirrors of it ... whatever. >>> >>> This is why I was hoping for a function ... however ... as simple as you say it can be ... I've never touched python in my life and never dealt with the PNG file structure. >>> >>> >>> On Tue, May 10, 2022 at 4:23 PM Revar Desmera <revarbat@gmail.com <mailto:revarbat@gmail.com>> wrote: >>> BOSL2 has a function/module called `heightfield()` that you can feed an array of heights, or a function literal that can calculate the heights dynamically. However, there is currently no way for an OpenSCAD script to read height values from an image file. You can only generate 3D geometry using `surface()`. >>> >>> https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield <https://github.com/revarbat/BOSL2/wiki/shapes3d.scad#functionmodule-heightfield> >>> >>> Having said that, it should not be too difficult to write a Python script to read in an image file, and have it write out a .scad source file containing the array of height data. >>> >>> - Revar >>> >>> >>>> On May 10, 2022, at 1:12 PM, Joe Weinpert <joe.weinpert@gmail.com <mailto:joe.weinpert@gmail.com>> wrote: >>>> >>>> Cannot run surface() as a function to return a vnf array of the data to manipulate. >>>> >>>> Is there a BOSL function that can do what surface() does? >>>> >>>> If not, is there another library out there that will do it? >>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org <mailto:discuss-leave@lists.openscad.org> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org