[Help] Get Pixel Color

I am trying to use the color of a certain pixel in my script. I can get the color manually using Pixie so if the script doesnt automatically get the color I’m fine with that.
this is what I want to happen:

If Pixel {x,y} is color {R,G,B} then – any color #'s will work, RBG is just my preferred
right click – covering that with an automator application
else
Blahblahblah
blahblahblah
end if

thanks for the help

Hi purple37,

Maybe the free iMagine Photo application can be of help for you, as it also allows to «get pixel values» of imported graphics.

Okay, now I scripted a custom solution for you, as this problem was quite a challenge for me. But it only works on Macs running Mac OS X 10.5 Leopard.

First, download the following Python helper script and place it on the desktop. Then execute the following AppleScript code, which will ask you to choose an image file and returns the RGB pixel color for the given pixel position:


set imgpath to quoted form of POSIX path of (choose file with prompt "Please choose an image file:" without multiple selections allowed and invisibles)
set pixelposx to "1.0"
set pixelposy to "1.0"
set pyscriptpath to quoted form of POSIX path of (((path to desktop) as Unicode text) & "pixelcolor.py")
set command to "/usr/bin/python " & pyscriptpath & " " & imgpath & " " & pixelposx & " " & pixelposy
set output to do shell script command
set {red, green, blue} to words 2 through 4 of output

That was fun :slight_smile: Time to go to bed…

I am still not satisfied with the Python solution, because it is quite slow. It works like a charm, but it is slow. So what could be faster than a Python script? Yes, a Foundation Tool written in Obj-C. And as I am currently reading Aaron Hillegass’ Cocoa programming book, I invested some time to transfer the slow Python into a fast Obj-C script.

As before, please download the helper script, unzip it and place it on your desktop and then execute the AppleScript code below. If you are interested in the Obj-C source code, please point your browser here.


set imgpath to quoted form of POSIX path of (choose file with prompt "Please choose an image file:" without multiple selections allowed and invisibles)
set pixelposx to "1.0"
set pixelposy to "1.0"
set foundationtoolpath to quoted form of POSIX path of (((path to desktop) as Unicode text) & "pixelcolor")
set command to foundationtoolpath & " " & imgpath & " " & pixelposx & " " & pixelposy
set output to do shell script command
set {red, green, blue, alpha} to words 1 through 4 of output

That was even more fun!

P.S.: StefanK is not only an excellent AppleScripter, but also a very good Obj-C programmer. He found a small bug in my pixelcolor.m code, which is now corrected: I forgot to release the image object initialized before. Thanks, Stefan!

Hi folks,

After much trawling on google for this exact script, I came across this thread… I’m looking to use this exact function for an art project I’ve got on the go. I’ve got a batch of images (small at the moment) that are RGB files containing one colour (different for each image). I’m trying to find a means of inputting multiple files to this exact function, and giving a line of output for each one. I did code a little in high school but it’s been a very long time, so I was wondering if you any of you would be so kind as to either point me in the right direction, or be able to modify the script to include this?

Thanks in advance,

Chris

Hi aktualsize,

The following AppleScript is based on my last example (Obj-C) and also requires the pixelcolor helper script to be loacted on the desktop to work correctly. Please copy the code below into a new Script Editor window and save it as an Application bundle.

Then you can drag and drop image files onto the script application. It will show you the color values for the given pixel position of each dropped image. I hope this example might get you started. Please note that it does not include a lot of error handling, but I just have a newborn baby in my arms which makes typing and coding a bit difficult :lol:


on open imgpaths
	set pixelposx to "1.0"
	set pixelposy to "1.0"
	set foundationtoolpath to quoted form of POSIX path of (((path to desktop) as Unicode text) & "pixelcolor")
	repeat with imgpath in imgpaths
		set imginfo to info for imgpath
		set imgname to name of imginfo
		set imgpath to quoted form of POSIX path of imgpath
		set command to foundationtoolpath & " " & imgpath & " " & pixelposx & " " & pixelposy
		set output to do shell script command
		set {red, green, blue, alpha} to words 1 through 4 of output
		tell me
			activate
			display dialog "Image name: " & imgname & return & "Red: " & red & return & "Green: " & green & return & "Blue: " & blue
		end tell
	end repeat
end open

Hope that helps!

Martin,

Many thanks for your help, with a little modification, I’ve got the script doing pretty much exactly what I was aiming for :slight_smile: I’ll try and post a link to the finished project when it’s all up and running.

Congratulations on the newborn, I appreciate that you probably have you hands full with that at the moment!

All the best,

Chris

Is anyone else having problems with pixelcolor returning unexpected values at an X, Y locations in a PNG file when comparing those values in a program like GIMP which allows you to use the color picker at a specific X, Y coordinate? I can’t seem to get this to work right…

Browser: Firefox 3.0.5 BeatnikPad
Operating System: Mac OS X (10.5)