I am a beginner applescripter, and have recently moved to os x and am trying to get the
resolution of a chosen image. Here’s what I got, but it does not return the resolution.
Thanks for any advice.
on getResolution()
set theImage to choose file
tell application “Image Events”
launch
set theImageReference to open theImage
tell theImageReference
return resolution
end tell
end tell
end getResolution
Todd
Model: imac g4
Browser: Safari 312.3
Operating System: Mac OS X (10.3.9)
display dialog "resolution: " & getResolution() buttons {"OK"} default button 1
on getResolution()
set theImage to choose file
tell application "Image Events"
launch
try
open theImage
set {x, y} to resolution of image 1
close image 1
return (x as text) & " x " & y as text
on error
try
close image 1
end try
return "couldn't get resolution of " & name of (info for theImage)
end try
end tell
end getResolution
I tried using Image Event to extract the resolution from image file, it works fine with TIFF and JPEG; but not EPS. (Have not tried on other file types yet)
May I know how to overcome this issue? (As EPS is one of the most commomly used image file type.)
Well I have had a quick look and was unable to find the one of these that I made earlier (unlike Blue Peter) so I have knocked this together quickly. The principle should be there although there are many ways to do this. You need your script to do some filtering of eps files by creator type then you could call different handlers to extract the info you want based on this. This example is for a test file made with Photoshop CS2. In the file head you have the file dimensions in points in the line starting with “%%HiResBoundingBox:” you also have the pixel dimensions in the line starting with “%ImageData:” if you extract this then do the math you should be able to get what you want. BTW i’ve used SatImage OSXA for this.
set The_File to choose file without invisibles
--
my EPS_Res(The_File)
--
display dialog "Image resolution is " & result & "dpi"
--
on EPS_Res(The_File)
try
set HiResBox to find text "%%HiResBoundingBox: [. [:digit:]]{1,}" in The_File with regexp
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set Height_Points to text item -1 of matchResult of HiResBox
set Width_Points to text item -2 of matchResult of HiResBox
set AppleScript's text item delimiters to ASTID
on error
set HiResBox to ""
-- display dialog "No \"Hi Res Bounding Box\" found." giving up after 3
end try
try
set ImageData to find text "%ImageData: [ [:digit:]]{1,} \"beginimage\"" in The_File with regexp
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set Height_Pixels to text item 3 of matchResult of ImageData
set Width_Pixels to text item 2 of matchResult of ImageData
set AppleScript's text item delimiters to ASTID
on error
set ImageData to ""
-- display dialog "No \"Image Data\" found." giving up after 3
end try
if HiResBox ≠"" and ImageData ≠"" then
set Height_Res to (Height_Pixels / (Height_Points / 72)) as integer
set Width_Res to (Width_Pixels / (Width_Points / 72)) as integer
return Height_Res & "x" & Width_Res
else
return false
end if
end EPS_Res
Thank you for your help, Mark67! I really admire your approach in solving this issue (is a bit lengthy but with this approach, a lot of info can be extracted from there, I hope).
The line beginning with “%ImageData:” contains a list of numbers I can’t recall them all but its something like this:
First = Width in pixels
Second = Height in pixels
Third = Bit-Depth
Fourth = Channels
Fifth = Forgotten
Sixth = Forgotten
Seventh = Encoding (1 = binary, 2 = ascii) and so on
You would have to do some experimenting to fill in the rest. Where possible I try keep away from eps its an old fat bloated pig.
Thank you Mark67 for your further info. I found some info from the internet and do some experiments to find out more, hope to share with those who are interested…
Third = Bit-Depth per channel (either 1 or 8)
Fourth = Channels (1=bitmap/grayscale. 2=Lab, 3=RGB, 4=CMYK)
Fifth = Pad Channels (Number of other channels saved)
Sixth = Block Size (number of bytes per row per channel)
Seventh = Encoding (1=binary, 2=ASCII, 3=JPEG low, 4=JPEG Medium, 5=JPEG High, 6=JPEG Max, 7=ASCII85)
set The_File to choose file without invisibles
--
my EPS_Encoding(The_File)
--
display dialog "Encoding is " & result
--
on EPS_Encoding(The_File)
try
set ImageData to find text "%ImageData: [ [:digit:]]{1,} \"beginimage\"" in The_File with regexp
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set Encoding to text item 8 of matchResult of ImageData
set AppleScript's text item delimiters to ASTID
-- return Encoding
if Encoding = "1" then return "Binary"
if Encoding = "2" then return "ASCII"
if Encoding = "3" then return "JPEG low"
if Encoding = "4" then return "JPEG Medium"
if Encoding = "5" then return "JPEG High"
if Encoding = "6" then return "JPEG Max"
if Encoding = "7" then return "ASCII85"
on error
return "No \"Image Data\" found."
end try
end EPS_Encoding
I’m a little confused. Aren’t eps files resolution independent? You may be getting the image preview’s resolution, but if you were to output the file to a printer, it doesn’t use that.
A host of applications are capable of saving files in EPS format. As mentioned earlier in the thread the EPS data can contain Raster Data, Vector Data or Both. In print graphics EPS was widely used for many years for placing in page layout applications like QuarkXPress. It was solid for reliability because Quark would not touch the file data it just passes it straight into the postscript data. Also for many years EPS was the only format that you could save an image file with a clipping path, this is all a bit out dated now (I don’t use EPS for images at all but re-process them). Adobe Photoshop will save a raster file as EPS with and this is most likely what the users here are looking at. Some older RIPS did not like EPS if it had been saved with JPEG compression and some non-postscript printers prefer ASCII encoding else you get garbled text instead of an image. Reading the file head for this info is the only scripted method I know. The preview would be 72dpi