Hi all,
I’m a new user here, but I have certainly availed myself of your collective knowlege before and I certainly appreciate it.
The following script works as advertised, and I hope someone finds it useful. The short story is that it traces bitmaps into vector art (similarly to Macromedia Flash’s “trace bitmap” command or Adobe’s Streamline) using exclusively free tools. The downside is that it’s ugly, it creates a lot of temporary files, and relies on more than a few other pieces of Linux/UNIX software. Most of them can easily be installed via Fink, but it’s been a while and I can’t provide specifics as I have forgotten them. The author of potrace links to it at the bottom of the page (although he calls it pocopo instead of pacopo) and it has its own little spot in cyberspace. The project page gives a little more background and some details I’ve neglected to mention.
Mostly I’d like to know if anyone has any ideas for improvement. I have some thoughts but optimization &c. is always appreciated.
on run
display dialog "drag! drag! ok!" buttons "oh. right." with icon caution
end run
on open filelist
set levellist to {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "23"} -- levels of posterization in each channel
set levelchoice to choose from list levellist with prompt "num of steps?" default items "5" OK button name "all right"
set steps to levelchoice
set realsteps to 1 + steps
set size1list to {"100", "200", "300", "400", "750"} -- resize all images to this size before adding borders and processing
set size1choice to choose from list size1list with prompt "first resize?" default items "200" OK button name "all right"
set size1 to size1choice
set size2list to {"320", "640", "800"} -- final image size
set size2choice to choose from list size2list with prompt "final image size?" default items "640" OK button name "all right"
set size2 to size2choice
set size2x to size2 * 1.1
set linelist to {"0", "0.5", "1.0", "2.0", "3.0", "5.0"} -- outline thickness
set linechoice to choose from list linelist with prompt "outline thickness?" default items "0" OK button name "all right"
set linewidth to linechoice
--set stylelist to {"1", "2"} -- final image output
--set stylechoice to choose from list stylelist with prompt "which style? bitmap is 1, vector is 2" default items "1" OK button name "ok go"
--set tehstyle to stylechoice
--display dialog "your style is " & tehstyle buttons {"OK"} default button 1 giving up after 10
repeat with i in filelist
-- set steps to 8
set stamp to do shell script "date ``+%Y%m%d.%H%M%S``"
set bsuf to ".b.png"
set suff to ".prb.svg"
do shell script "/sw/bin/convert -normalize -bordercolor '#000000' -border 5% -resize " & size1 & "x" & size1 & " " & POSIX path of i & " " & POSIX path of i & bsuf
repeat with c from 0 to 2 by 1
if c = 0 then set chan to "r"
if c = 1 then set chan to "g"
if c = 2 then set chan to "b"
repeat with kstep from 0 to (realsteps - 1) by 1
set k to kstep / (realsteps - 1)
set rk to (round (k * 100)) / 100
set antik to 1 - rk
set ksuf to antik * 100
set lastk to (realsteps - 1)
do shell script "/sw/bin/pngtopnm " & POSIX path of i & bsuf & " | /sw/bin/pamchannel " & c & " | /sw/bin/pamtopnm -assume | /usr/local/bin/potrace -s -k " & antik & " -o" & POSIX path of i & "." & stamp & "_" & chan & ksuf & suff
set kgrey to antik * 256
if antik = 1 then set kgrey to 255
set rkgrey to round (kgrey)
set khex to do shell script "printf '%X' " & rkgrey
if c = 0 then set hex to khex & "ffff"
if c = 1 then set hex to "ff" & khex & "ff"
if c = 2 then set hex to "ffff" & khex
do shell script "sed 's/#000000/#" & hex & "/' " & POSIX path of i & "." & stamp & "_" & chan & ksuf & suff & " > " & POSIX path of i & ".2." & stamp & "_" & chan & ksuf & suff -- color/vector style
-- do shell script "sed 's/#000000/#" & khex & khex & khex & "/' " & POSIX path of i & "." & stamp & "_" & chan & ksuf & suff & " > " & POSIX path of i & ".2." & stamp & "_" & chan & ksuf & suff -- grey/bitmap style
do shell script "sed 's/none\">/#000000\" stroke-width=\"" & linewidth & "\">/' " & POSIX path of i & ".2." & stamp & "_" & chan & ksuf & suff & " > " & POSIX path of i & ".3." & stamp & "_" & chan & ksuf & suff
if kstep ≠0 then do shell script "sed -n '/<g/,/g>/p' " & POSIX path of i & ".3." & stamp & "_" & chan & ksuf & suff & " >> " & POSIX path of i & ".3." & stamp & "_" & chan & "100" & suff
-- comment out next 2 lines if you want to keep stuff
do shell script "rm " & POSIX path of i & "." & stamp & "_" & chan & ksuf & suff
do shell script "rm " & POSIX path of i & ".2." & stamp & "_" & chan & ksuf & suff
if ksuf ≠100 then do shell script "rm " & POSIX path of i & ".3." & stamp & "_" & chan & ksuf & suff
end repeat
end repeat
repeat with xs from 0 to 2 by 1
if xs = 0 then set xchan to "r"
if xs = 1 then set xchan to "g"
if xs = 2 then set xchan to "b"
do shell script "sed 's/<\\/svg>//g' " & POSIX path of i & ".3." & stamp & "_" & xchan & "100" & suff & " >> " & POSIX path of i & ".3." & stamp & "_" & xchan & "100x" & suff
do shell script "echo \"</svg>\" >> " & POSIX path of i & ".3." & stamp & "_" & xchan & "100x" & suff
do shell script "/sw/bin/convert -background none " & POSIX path of i & ".3." & stamp & "_" & xchan & "100x" & suff & " -resize " & size2x & "x" & size2x & " " & POSIX path of i & ".3." & stamp & "_" & xchan & "100x" & suff & ".png"
--comment next 2 lines if you want to keep vector stuff
do shell script "rm " & POSIX path of i & ".3." & stamp & "_" & xchan & "100" & suff
-- do shell script "rm " & POSIX path of i & ".3." & stamp & "_" & xchan & "100x" & suff
end repeat
-- a work in progress: trying to make the filenames more friendly...
--I'm here to lop off the end:
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set parentDir to (text items 1 through -2 of POSIX path of i) as string -- this is the magic line
--display dialog "parent equals " & parentDir buttons {"OK"} default button 1 giving up after 10
set AppleScript's text item delimiters to oldDelims
do shell script "/sw/bin/convert " & POSIX path of i & ".3." & stamp & "_r100x" & suff & ".png" & " " & POSIX path of i & ".3." & stamp & "_g100x" & suff & ".png" & " " & POSIX path of i & ".3." & stamp & "_b100x" & suff & ".png" & " " & "-channel RGB -combine -shave 5% " & POSIX path of i & ".png"
--comment next 4 lines if you want to keep png stuff
do shell script "rm " & POSIX path of i & ".3." & stamp & "_r100x" & suff & ".png"
do shell script "rm " & POSIX path of i & ".3." & stamp & "_g100x" & suff & ".png"
do shell script "rm " & POSIX path of i & ".3." & stamp & "_b100x" & suff & ".png"
do shell script "rm " & POSIX path of i & bsuf
end repeat
-- tell me
-- set rfile to POSIX path of i & ".3." & stamp & "_r100x" & suff
-- end tell
-- tell application "Illustrator CS"
-- activate
-- open rfile
-- end tell
end open
cheers
p