Photoshop Applescript help with grayscale images or another photo app!

I am trying to find the average color of gray in a grayscale photo using applescript. Also, it would be nice to find the darkest and lightest values as well. Let me know if anyone has any ideas.

Thanks,

-Matt

How’s this?

tell application "Adobe Photoshop CS"
	tell current document
		if mode is grayscale then
			set a_grayscale to true
			tell channel 1
				set gry_hist to histogram
			end tell
		else
			set a_grayscale to false
		end if
	end tell
end tell
if a_grayscale then
	set sigma_percent to 0
	set sigma_pixels to 0
	set perct_hist to {}
	set high_value to 100
	set low_value to 0
	repeat with hist_idx from 1 to 256
		set perct_value to 100 - (round (hist_idx * 100 / 256)) --Convert histogram value to percentage
		set sigma_percent to sigma_percent + (perct_value * (contents of item hist_idx of gry_hist)) -- Sum percentages
		set sigma_pixels to sigma_pixels + (contents of item hist_idx of gry_hist) -- Sum pixel count
		if high_value = 100 and (contents of item hist_idx of gry_hist) ≠ 0 then set high_value to perct_value
		if (contents of item hist_idx of gry_hist) ≠ 0 then set low_value to perct_value
	end repeat
	set avg_px_value to round (sigma_percent / sigma_pixels)
	display dialog "Average pixel value : " & avg_px_value & return & "Darkest pixel : " & high_value & return & "Lightest pixel : " & low_value
end if

Works great, thanks!

I’m glad I could help. I think that may have been the only success I had that day. However, I want to point out that when I was originally writing the script, I began by converting the entire histogram to percent values. Half-way through the script, I decided it was un-necessary but I failed to remove the one line:
set perct_hist to {}
There is no reason for it to be there now. Sorry if it confused anyone.