[SOLVED] How do I replicate Inkscape's bitmap tracer?

General discussions about Inkscape.
ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

[SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ukurereh » Sat Apr 14, 2012 2:06 pm

.
Edit: I made a script to do this from the command line, see post #9.

I want to do command-line or scripted bitmap tracing, but Inkscape's tracer can't be automated. Potrace only does black-and-white images, so there must be a lot more going on under the hood with Inkscape.

So if I were to instead use Potrace (and probably ImageMagick, and some SVG tool?), how would I replicate Inkscape's functionality with color, multiple scans, and stacking scans?
Last edited by ukurereh on Mon May 21, 2012 3:41 pm, edited 4 times in total.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: How do I replicate Inkscape's bitmap tracer with Potrace

Postby brynn » Sat Apr 14, 2012 6:03 pm

Potrace only does black and white? I had the impression that potrace and Trace Bitmap were basically the same thing. The technical side of Inkscape is definitely not my forte though, lol.

You could check out the developers' mailing list, and/or irc channel. I think that not many developers participate here. So if you don't get an answer, you could try those places where they hang out and communicate :D

User avatar
tomh
Posts: 218
Joined: Sat Feb 14, 2009 10:14 pm

Re: How do I replicate Inkscape's bitmap tracer with Potrace

Postby tomh » Sun Apr 15, 2012 1:22 am

Not an expert, but...

Potrace only does black and white? I had the impression that potrace and Trace Bitmap were basically the same thing.


Potrace only does black and white. Inkscape has added several pre-processing steps that enable it to do the fancy traces, basically doing the colour prossessing steps and passing black and white images for potrace to scan.

The code is here: http://bazaar.launchpad.net/~inkscape.d ... src/trace/
Might not be that helpful but might give you some idea of what inkscape is doing (even if you only read the titles and comments in each file and match them to the trace bitmap dialogue.

Basically it makes very clever use of pre-processing.

For example the colour scan option uses colour quantisation: http://www.imagemagick.org/Usage/quantize/ to reduce the number of colours; then extracts only one of those colours and uses a threshold operator to convert the image into black and white. It then passes that to potrace to scan and then combines the results back together.

ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

Re: How do I replicate Inkscape's bitmap tracer with Potrace

Postby ukurereh » Sat Apr 21, 2012 10:51 am

Here it is, a Python script to trace a whole directory of color images at once.
- You need to install Python 3.2 or later, and also the lxml library for that version of Python.
- Unless you get the Windows version, you'll also need to download potrace, pngnq, and ImageMagick.
- Then you need to add those programs' directories to your PATH variable so they can be accessed directly from the command line. (Or if you don't want to do that, you can edit the executable paths near the top of color_trace.py)
- Once you've got all that, run color_trace.py from the command line using Python 3.2.

Source & external programs (i.e. Potrace and such already included):
color_trace-win-0.02.zip

Source only (other platforms, must download external programs):
color_trace-0.02.zip
(10.66 KiB) Downloaded 659 times

ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ukurereh » Mon Apr 23, 2012 2:03 am

Bump. Here are some pictures traced with 256 colors. They took hours each, nearly overheated my laptop, and made svg files about 20 megs each. So don't trace 256 colors unless you're feeling adventurous, m'kay?

Image

Image

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby brynn » Mon Apr 23, 2012 5:37 am

Well that's awesome! Those are vector images?? Because that's better than what Trace Bitmap can do, in my experience!

If you like you can post a new topic in Inkscape Resources subforum, with links to the file, etc. Then people can find it quickly, and also ask related questions in that topic.

Or I could move this one for you? Let me know :D

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: How do I replicate Inkscape's bitmap tracer with Potrace

Postby ~suv » Mon Apr 23, 2012 1:26 pm

ukurereh wrote:Here it is, a Python script to trace a whole directory of color images at once.
- You need to install Python 3.2 or later, and also the lxml library for that version of Python.
(…)

You won't offer a version compatible with Python 2.7? I would have been interested in testing the script on OS X 10.7.2 - even wrote a MacPorts portfile for pngnq - only to realize later-on that it would force me to install a (potientally conflicting) Python version.

(Note: Inkscape's extensions haven't been migrated to fully support Python 3.x - whoever (on linux) [1] changes the system-wide default python version to 3 without taking care that Inkscape still spawns python-2.x for the bundled extensions, might have issues with failing extensions (input/output as well as effects)).


[1] Edit: I don't know what happens to Inkscape users on Windows who redefine the global %PATH% variable to point to a system-wide custom Python 3.x installation (AFAIK untested with Inkscape 0.48.x, which includes its own python 2.6.5 executable as well as the modules lxml 2.2.4 and numpy 1.4.1).

ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

Re: How do I replicate Inkscape's bitmap tracer with Potrace

Postby ukurereh » Thu Apr 26, 2012 7:21 am

~suv wrote:You won't offer a version compatible with Python 2.7?

I can do that, just give me the next day or so.

BTW this has nothing to do with Inkscape's extensions. And doesn't Inkscape use its own installation of Python? (or maybe that's just on Windows)

Edit: I'm sorry, real life is getting in the way >_<

Edit edit: I'm just going to forget doing a Python 2.7 backport and instead focus on making standalone executables that don't require Python at all.
Last edited by ukurereh on Mon May 28, 2012 1:06 pm, edited 2 times in total.

ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ukurereh » Mon May 21, 2012 3:37 pm

Version 1.00a is out! This program uses potrace to trace color images.
This version runs faster, has more color quantization options (median-cut, adaptive spatial subdivision, and neuquant), supports dithering*, prescales the image for better quality, makes available several of potrace's options, and can use an external palette instead of color quantizing.
*Dithering will make the output svg much larger and more complex, and may or may not produce desired results.


Now in executable form, with no need to install Python and such! (Windows only for now, Mac version in the future)
Windows executable & external programs (i.e. Potrace and such already included):
color_trace_multi-win32-exe-1.00a.zip
- You might need to install the Microsoft Visual C++ 2008 Redistributable Package (free download, for x86 (32 bit) Windows or for x64 (64 bit) Windows).

And then...
- Once you've got that, run color_trace.exe from the command line.
- Use the switch -h or /? for help, or read the README.txt.

---------------------------------------------------

Requirements for source versions:
- You need to install Python 3.2 or later, and also the lxml library for that version of Python.
- To use this program, you will need to know how to use the command line/shell.

This next part is unnecessary if you get the Windows version.
- Unless you get the Windows version, you'll also need to download potrace, pngquant, pngnq, and ImageMagick.
- Then you need to add those programs' directories to your PATH variable so they can be accessed directly from the command line. (Or if you don't want to do that, you can edit the executable paths near the top of color_trace_multi.py)

And then...
- Once you've got all that, run color_trace.py from the command line using Python 3.2.
- Use the switch -h or /? for help, or read the README.txt.

Windows version with source & external programs (i.e. Potrace and such already included):
color_trace_multi-win32-1.00a.zip

Source only (other platforms, must download external programs):
color_trace_multi-1.00.zip
(23.61 KiB) Downloaded 672 times


Edit: :oops: Oops, it gives a very unhelpful error message when a file is missing, I will fix that in the next release. Also, -s/--stack will be the default and be disabled by --nostack. Coming soon! (But not that soon because I will be out of state.)
Last edited by ukurereh on Mon May 28, 2012 1:43 pm, edited 2 times in total.

ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ukurereh » Mon May 28, 2012 1:24 pm

Bump for Windows executable release and slight bugfix (1.00a). See the above post for more information.

Note: I think I'll eventually re-post this in the Inkscape Resources subforum (as suggested in brynn's post which I totally missed :| ), once I get a new version and Mac executable out.

And also: Can anyone think of a better, catchier name for this program?
- I considered "rainbowtrace", but it's already taken by a Japanese band or something. Could I still use it?
- And "rainbowtracer" is an LED project of some sort.
- And of course "colortrace" is taken by half a dozen things at least.
- Huh, maybe "rainbotrace" (as in rainbow+potrace), but that's prone to misspelling... how about "rainpotrace"?

Lars_Bylund
Posts: 7
Joined: Mon Nov 28, 2011 9:35 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby Lars_Bylund » Tue May 29, 2012 11:45 pm

I know that this is a solved one, and I havent really read every post, but anyway.

I use :

for i in *.png; do autotrace "$i" -despeckle-level 14 --output-file "$(echo $i | sed 's/.png/.svg/')"; done

in the map with my (png) pictures.

I used this as an 'effect' on a video you can see here (please view in full hd!)
http://www.youtube.com/watch?v=R8zW2MHhgUE

This is how it's done, even if it's a bit of topic.

I first convert avi to pngs:

ffmpeg -i fro.avi image%d.png

png's to svg:

for i in *.png; do autotrace "$i" -despeckle-level 14 --output-file "$(echo $i | sed 's/.png/.svg/')"; done

svg to png:

for i in *; do rsvg-convert $i -o `echo $i | sed -e 's/svg$/png/'`; done

and png to avi:

ffmpeg -i image%d.png -vcodec huffyuv test.avi

ukurereh
Posts: 10
Joined: Sat Apr 14, 2012 11:35 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ukurereh » Sun Jun 03, 2012 6:30 am

Hey Lars, much thanks for your bash(?)+autotrace solution.

To anyone who can't get the win32 version of color_trace_multi to work, it's not your fault (ImageMagick giving an error about unsupported decoder/codec or whatnot) because apparently I forgot to include some important files. A quickfix is to install ImageMagick and then copy the installed files over to the included "bin\imagemagick-q8" folder.

mike_
Posts: 1
Joined: Mon Jul 16, 2012 8:47 pm

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby mike_ » Mon Jul 16, 2012 8:58 pm

Hi ukurereh,

thank you for developing the color trace tool. I've tried it, but unfortunately I ran into a problem.

When I try to run the tool with the command :

Code: Select all

c:\temp\color_trace>color_trace_multi.exe -i 4208.png -c 16 -d C:\temp --output temp.png

I get the following error :
[Errno 2] No such file or directory: 'C:\\temp\\temp~tmp_scaled.png'

I have tried to run ImageMagick's `convert` tool from the command line and it works, since I had the other version (6.7.8) already installed in my Program Files. I don't have the PATH variable setup to the color_trace/bin sub-directories.
I wanted to try to fix the problem, but I'm new to Python and after installing Python 3.2 and running your script it complains it cannot find the `svg_stack` module.

I get the same problem on two separate machines, one is running 32bit and other one is 64bit Windows 7 Enter&prise ;)

Can you please help me with this issue ?

Thank you.

//edit: Apparently I've missed the post above. I've replaced the bundled IM with fresh install and it worked like charm. My bad, sorry about that. :roll:
//The results weren't correct, because the colors and shapes were different from one's in the Inkscape and looked worse.

webkopejka
Posts: 2
Joined: Sat Aug 18, 2012 2:21 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby webkopejka » Sat Aug 18, 2012 2:28 am

Hello.
I tried to run
python color_trace_multi.py -i 1.png -o 1.svg -c 16
but get a message "'module' object has no attribute 'SW_HIDE'"
Using ubuntu 12.04 + python3.2

No new files created.
Can you help me run the script correctly, please?

webkopejka
Posts: 2
Joined: Sat Aug 18, 2012 2:21 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby webkopejka » Sat Aug 18, 2012 3:11 am

Hello.

Now I try to run:
sudo python color_trace_multi.py -i './1.png' -o './1.svg' -c 16 -q nq -v

and output is:

"convert" "./1.png" -filter lanczos -resize 200% "./1~tmp_scaled.png"
"pngnq" -f -d "." -n 16 -e ~quant.png "./1~tmp_scaled.png"
"convert" "./1~tmp_reduced.png" -unique-colors -compress none ppm:-
"identify" -ping -format "%w" "./1.png"
"convert" - -fill #000001 -opaque "#999a9e" -fill #ffffff -opaque "#898679" -fill #ffffff -opaque "#000000" -fill #ffffff -opaque "#f2e5dd" -fill #ffffff -opaque "#8f8557" -fill #ffffff -opaque "#f9f500" -fill #ffffff -opaque "#f7f000" -fill #ffffff -opaque "#ccb100" -fill #ffffff -opaque "#bea938" -fill #ffffff -opaque "#992026" -fill #ffffff -opaque "#0b0a0a" -fill #ffffff -opaque "#080706" -
"convert" - -fill #FFFFFF -opaque "#ffffff" -fill #000000 -opaque #000001 "./1~tmp_layer.ppm"
"potrace" --svg -o "./1~tmp_trace.svg" -C "#999a9e" -t 2 -a 1.0 -O 0.2 -W 3.422222222222222 "./1~tmp_layer.ppm"
Document is empty, line 1, column 1

In the end I can see a file "-fill" and it is not vectorized.

Can anybody help me to vectorize something?
Thank you a lot :)

User avatar
hellocatfood
Posts: 193
Joined: Fri Aug 29, 2008 8:49 pm
Contact:

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby hellocatfood » Fri Sep 07, 2012 8:18 pm

I tried to run version 1.0 on Ubntu 12.04 using this code

Code: Select all

python color_trace_multi.py -i /home/hellocatfood/Desktop/color_trace_multi-1.00/ -o /home/hellocatfood/ -c 10


and I got this error, with no output

Code: Select all

  File "color_trace_multi.py", line 47
    def verbose(*args, level=1):
                           ^
SyntaxError: invalid syntax
I'm on The Web | Flickr | tumblr

abhasbaghel
Posts: 1
Joined: Wed Jan 29, 2014 7:46 pm

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby abhasbaghel » Wed Feb 26, 2014 3:41 pm

hellocatfood wrote:I tried to run version 1.0 on Ubntu 12.04 using this code

Code: Select all

python color_trace_multi.py -i /home/hellocatfood/Desktop/color_trace_multi-1.00/ -o /home/hellocatfood/ -c 10


and I got this error, with no output

Code: Select all

  File "color_trace_multi.py", line 47
    def verbose(*args, level=1):
                           ^
SyntaxError: invalid syntax


Hello hellocatfood,

I am also facing same problem. Please tell me if you have solved this. I used windows version as well as linux but on both plateform I faced some problems. I have to create raster to svg . So I request to all please help me ASAP. :cry: :cry: :cry: :cry:

User avatar
hellocatfood
Posts: 193
Joined: Fri Aug 29, 2008 8:49 pm
Contact:

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby hellocatfood » Wed Feb 26, 2014 6:48 pm

Nope, I didn't find a solution
I'm on The Web | Flickr | tumblr

ShawnDion
Posts: 5
Joined: Tue Jun 23, 2015 5:10 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ShawnDion » Tue Jun 23, 2015 5:17 am

I know this is a old post but maybe someone has some information.

I rebuilt this tool to be 100x faster my problem is that the svg_stack.py crashes when stacking over 256 layers I wanted to know if anyone had a better way for stacking svg files including a plugin inside of inkscape?

Thanks in advance

Shawn

mxz
Posts: 1
Joined: Fri Jul 03, 2015 3:27 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby mxz » Fri Jul 03, 2015 3:34 am

Hello I took a look and fixed an small problem in the Ukurereh's code and I put it here in case someone else is interested:
https://github.com/migvel/color_trace

Cheers.

ShawnDion
Posts: 5
Joined: Tue Jun 23, 2015 5:10 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby ShawnDion » Fri Jul 03, 2015 7:16 am

I'll definitely give it a try... My new version uses less commands to generate the final product.

Once I'm done I'll post what I have as a Windows Version and maybe some can tweak the potrace or the color stacking method.

Currently the best is from Darkest to Lightest and works not bad I'm also fixing a anti-alias issue my final goal would be to have a extra add-on for Inkscape that can also work offline but give more color choices to the user.

I know that a 16 million color image converted to SVG would be the dumbest thing but sometimes for gradient purposes a few extra colors to the chart doesn't hurt.

I'll keep you all updated as for the new tool that inkscape added on the last build for "Trace Pixel Art" makes great puzzles to print now if I can figure on how to make this one in dos =P

Shawn

P.S: My first PC was a VIC-20 since then Ascii art and pixel art are a all time favorite also on a side note for those who have a font software that can make colored fonts I'll have a .py script to import the images into the fonts.

Spaceboy64
Posts: 6
Joined: Sun Oct 11, 2015 7:25 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby Spaceboy64 » Tue Oct 20, 2015 5:35 am

Lars_Bylund wrote:I know that this is a solved one, and I havent really read every post, but anyway.

I use :

for i in *.png; do autotrace "$i" -despeckle-level 14 --output-file "$(echo $i | sed 's/.png/.svg/')"; done

in the map with my (png) pictures.

I used this as an 'effect' on a video you can see here (please view in full hd!)
http://www.youtube.com/watch?v=R8zW2MHhgUE

This is how it's done, even if it's a bit of topic.

I first convert avi to pngs:

ffmpeg -i fro.avi image%d.png

png's to svg:

for i in *.png; do autotrace "$i" -despeckle-level 14 --output-file "$(echo $i | sed 's/.png/.svg/')"; done

svg to png:

for i in *; do rsvg-convert $i -o `echo $i | sed -e 's/svg$/png/'`; done

and png to avi:

ffmpeg -i image%d.png -vcodec huffyuv test.avi


This is exactly what I would like to do, although my images are B&W and high contrast.
I have very little experience with command line programs. Could you possibly explain in more detail? Like, an example of the complete command? Does the program need to be in the same directory as my images?
I have Python installed, as it gets installed with Blender, but I tried a command and I got an error message that it could not be run because it could not find Python 32.dll.

Thanks!

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby hulf2012 » Tue Oct 20, 2015 7:16 am

Hello Spaceboy64

I'm none of the autors, I haven't tried any of the solutions, and might be wrong, but:

- In this topic there are 2 solutions for the target "How do I replicate Inkscape's bitmap tracer?":

-- ukurereh 's solution:
--- It involves installing Python 3 in your system. It seems that works for windows, Linux, etc
--- It also involves installing other software, which is packed in the zip file provided above (in post #9)

-- Lars_Bylund 's solution:
--- They are several commands that can be executed easily in Linux, but you also have to install few programs:
---- ffmpeg
---- autotrace
---- rsvg-convert (?) :?:
---- sed (I think it already is installed in Linux distributions)

So, these versions are different. For ... "the lars" you don't need to use python, but it's better if you are in a Linux distribution, I think. You have to run these commands after locate you in your working directory (I mean, the directory where your images are), the important part is:

Code: Select all

for i in *.png; do autotrace "$i" -despeckle-level 14 --output-file "$(echo $i | sed 's/.png/.svg/')"; done


- mmm... well I don't think that putting "done" at the end is neccesary, or even it can be recognised for your sistem.
- But the other parts of code, doesn't need to be changed. It will work if you have only PNG files in that directory.

Hope it helps

Note:
About autotrace help:
http://linux.die.net/man/1/autotrace

I think, it will vectorize each image in 256 colors, :|
-despeckle-level will bypass small details of the images... I suppose

It may take a loooong time the vectorization of each image. Your machine could suffer of over warming

Greetings
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

Spaceboy64
Posts: 6
Joined: Sun Oct 11, 2015 7:25 am

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby Spaceboy64 » Tue Oct 20, 2015 1:42 pm

I downloaded this, which says I don't need to install Python, but I haven't figured out how to use it. I'm wondering why I got the error message about Python not being found.

ukurereh wrote:Now in executable form, with no need to install Python and such! (Windows only for now, Mac version in the future)
Windows executable & external programs (i.e. Potrace and such already included):
color_trace_multi-win32-exe-1.00a.zip
- You might need to install the Microsoft Visual C++ 2008 Redistributable Package (free download, for x86 (32 bit) Windows or for x64 (64 bit) Windows).


I was hoping for an example command, including everything I would need to type in.

I thought that was what Lars_Bylund was using, but I can see now that he's using several programs. Is Autotrace a separate program? or is that Potrace?
This just doesn't seem like a complete command to me. Is it? or is there more that was left out ? Is this only for Linux users?
Lars_Bylund wrote:I use :

for i in *.png; do autotrace "$i" -despeckle-level 14 --output-file "$(echo $i | sed 's/.png/.svg/')"; done


Sorry, I just never use the command line, nor Linux. I don't need to convert to PNG, I just need the autotrace, and I'm using Windows 7.

Thanks.

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: [SOLVED] How do I replicate Inkscape's bitmap tracer?

Postby hulf2012 » Wed Oct 21, 2015 1:15 am

ummm :|

Check this post:

mxz wrote:Hello I took a look and fixed an small problem in the Ukurereh's code and I put it here in case someone else is interested:
https://github.com/migvel/color_trace

Cheers.


Reading early posts I see that ukurereh solution had some mistakes in its code. Later mxz solve some of them and left the code in the link I'm quoting. So, give it a try. Be aware You need to install several programs (Python 3.2, Potrace , etc.)

Spaceboy64 wrote:I thought that was what Lars_Bylund was using, but I can see now that he's using several programs. Is Autotrace a separate program? or is that Potrace?
This just doesn't seem like a complete command to me. Is it? or is there more that was left out ? Is this only for Linux users?


Autotrace an Potrace are two different tracing programs. You have to found them, and install, ... one of them. For Lars's solution you have to install Autotrace. But You are in Windows 7, so I think you must use the mxz solution, with all the required programs. You can keep posting your progress here, but I won't be capable of answering all your doubts. so with all respect: google it!... RTFM! ... Think smart!.

Spaceboy64 wrote:Sorry, I just never use the command line, nor Linux. I don't need to convert to PNG, I just need the autotrace, and I'm using Windows 7.




... Try to learn GNU linux, and its command line. Windows also have its command line program. Learn it, because you are going to need for any of these solutions. Keep windows 7 and DON'T UPDATE IT TO WIN 10 (at least not this year)

Second thoughts:
I don't want to dis-encourage you to post your progress here. It will help to all. So... post all the questions you have

Pd; Maybe You should open another topic, with a significant, helpful title
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.


Return to “General Discussions”