Finding pixels using applescript.

I am close to succeeding here!!! Just before you posted this message i woke up and discovered my stupidity. Ofcourse the answer lies in the this_col and this_row. What was i thinking HAHAHAH

Well my praise was a honest one and you should really take that as compliment, about me thinking diffrent (as in the apple slogan) i am more a guy who is often thinking to difficult. But i do see that applescript has alot of power to get things done and since it is easier to learn and understand then objective-c i am putting my faith in making an application partially objective-c (as i dont see a way on how to talk to the usb board through applescript) and Applescript (which i think can help me out alot on getting the realworld coordinates of a pixture which i can then send of to the objective-c programm for further processing)

Anyway it’s a kind of confusing thing but i am really appreciating all the help i am getting here. Hopefully my idea will be reality soon so that others have a go thereselves using there macs to control stuff! (like a lathe, drill or even a fun project robot) It’s a shame that there isn’t alot out there on these kind of projects for Mac

regards Bastiaan

Bastiaan,

You now have two possibilities. My script below uses iMagine Photo. The script will write to a text file, the coordinates of all your black pixels as long as you only have a black and white drawing.

Once you are satisfied the script is doing what you want, you can remove the line that ends with: “--------------- This restricts the results to the first row.”

That line restricts the script to the first row in your drawing file. The script assumes a black and white image so it only bothers to check the red color component of a red, green, blue triplet, and if the value is less than half of its maximum possible value it assumes the pixel is black. The script is not exactly fast, but I am sure it will be a lot faster than using Photoshop to do the job.

If you only want the black pixels bordering the outline of your object as suggested by PreTech I can modify the script to do that as well.

Kevin


on run
	set thisFile to choose file with prompt "Choose a drawing file: "
	set textFile to choose file name with prompt "Choose a file to save results to: " default name "BlackPoints.txt"
	tell application "iMagine Photo"
		set thisImporter to import graphic thisFile
		if the component error of thisImporter is not equal to 0 then
			close thisImporter
			display dialog "Not an image file that quicktime recognizes."
			return
		end if
		set {x, y, xDim, yDim} to the natural bounds of thisImporter
		set thisDocument to make new window document with properties {dimensions:{xDim, yDim}}
		set the drawing destination of thisImporter to thisDocument
		draw thisImporter
		close thisImporter
		set fileRef to open for access textFile with write permission
		set yDim to 1 --------------- This restricts the results to the first row.
		repeat with j from 0 to (yDim - 1)
			set pixelValues to get pixel values of thisDocument with properties {pixel value class:pixels on line, start point:{0, j}, end point:{xDim, j}}
			set pixelColours to pixel colors of pixelValues
			set pixelPoints to pixel points of pixelValues
			repeat with i from 1 to xDim
				if (item 1) of item i of pixelColours is less than 32767 then
					set xVal to item 1 of (item i of pixelPoints)
					set yVal to item 2 of (item i of pixelPoints)
					write (xVal as string) & ", " & (yVal as string) & return to fileRef
				end if
			end repeat
		end repeat
		close thisDocument
		close access fileRef
	end tell
end run

Thanks kTam

It works like a charm!!! you mentioned Pretechs idea about only writing the outer pixels to a file. If this can be done would you help me with this? As i see it it will save lots of time for converting the images to an exactly 1px outline, i don’t think it will speed up the scanning process but this is something i am not bothered with.

So if you would like to help me some more i would really appreciate it

regards Bastiaan

For those who are wondering how much time it takes to get the coordinates into a file, i just did a quick test with KTam’s script. To see the diffrence i do have to rerun this test with Drossi’s script. I myself am very curious on how much diffrence there will be. Not that i mind waiting for the scan to be finished as i have a nice hot cup of coffee on my desk which will entertain me for the time beeing, but both scripts do the same and it makes me wonder if there would be a rendertime diffrence and if so by how much.

One thing is sure i like having the two options, a script that can be run from within photoshop and a second that uses Imagine. When my complete programm is finished and i have put it online for download both scripts will be included so that people can pick there favourite tool to get the coordinates into a file, which then can be used for further processing.

below you’ll see 2 speedtests i have done with Ktams script.

first test: image with 3 handdrawn curved lines (1px brush full length of canvas)
Canvas size: 300x300 px
Resolution: 300 dpi
Mode: Indexed Color (hard black and white, no greys)
Time to scan and write data: Under 2 minutes

The data file:
Number of lines: 919
Number of Chars: 7845

Second test: Complete black canvas
Canvas size: 300x300 px
Resolution: 300 dpi
Mode: Indexed Color (hard black and white, no greys)
Time to scan and write data: +26 minutes

EDIT: *Drossi’s script took about 2.5 hours to finish, but i take in account that this was all black which won’t happen in a reallife example, still very happy with both scripts and handling

The data file:
number of lines: 90000
Number of Chars: 744000
file size: 728kb (pretty small)

This script was tested on my G5 1.8Ghz PowerPC (single processor) with 1GB DDR SDRam

I can certainly help in writing the scripts.

Kevin

Bastiaan,

You mentioned you only wanted the first black pixel in each column. I have made a few changes to my original script to take this into account.

This script on your computer should never take more than a few minutes.

Kevin


on run
	set thisFile to choose file with prompt "Choose a drawing file: "
	set textFile to choose file name with prompt "Choose a file to save results to: " default name "BlackPoints.txt"
	tell application "iMagine Photo"
		set thisImporter to import graphic thisFile
		if the component error of thisImporter is not equal to 0 then
			close thisImporter
			display dialog "Not an image file that quicktime recognizes."
			return
		end if
		set {x, y, xDim, yDim} to the natural bounds of thisImporter
		set thisDocument to make new window document with properties {dimensions:{xDim, yDim}}
		set the drawing destination of thisImporter to thisDocument
		draw thisImporter
		close thisImporter
		set fileRef to open for access textFile with write permission
		repeat with i from 0 to (xDim - 1)
			set pixelValues to get pixel values of thisDocument with properties {pixel value class:pixels on line, start point:{i, 0}, end point:{i, yDim}}
			set pixelColours to pixel colors of pixelValues
			set pixelPoints to pixel points of pixelValues
			repeat with j from 1 to yDim
				if (item 1) of item j of pixelColours is less than 32767 then
					set xVal to item 1 of (item j of pixelPoints)
					set yVal to item 2 of (item j of pixelPoints)
					write (xVal as string) & ", " & (yVal as string) & return to fileRef
					exit repeat
				end if
			end repeat
		end repeat
		close thisDocument
		close access fileRef
	end tell
end run

Sorry for my delayed response, i didn’t had time for any testing today since i was doing some illustrations and photoretouches for a clients of mine

Well i just did a quick test with a 300x300px whitecanvas and a straight line (stroke size 15px), after doing the render it indeed gave me a perfectly 1px line as coordinates (outer pixel).

It’s really great that this is working out perfectly. Hopefully next week i have some spare time to pick-up the (further) development of the driver for the usb board. It’s a slow process but i have good faith in succeeding.

Thanks again for all the help!!! If you are thinking of a new update for Imagine please put this script in as an example i am sure there are people out there with similair ideas which you will give an headstart by publishing this script! I am sure they will be appreciating it as much as i am doing.

regards Bastiaan

This is an interesing topic, and I will need to read it more thouroughly later. My initial thoughts are is it neccessary to work with pixel data? Illustrator plots paths using belzier math, and the point locations are easily grabed. If you could use this to read the data it might give you a more accurate representation of the outline and could be easiely scaled. Of course if you don’t know how to work with the math, as I do not, or you need to work with pixel data then it is a it might be best to work with the solutions that everyone else has given you to work with.

Just wondering if you could take another approach to this. If you are creating the images in the first place then you could control the complexity of the detail.

In photoshop you could probably take an approach such as;

select all black pixels (by color range)
convert selection to path
get points describing path
convert same to path for lathe

In Illustrator on Mac I believe you should have a DXF export option which may be of use. Plus there is an auto-trace tool to get the outline of an image.