Page 1 of 2
get the width and height of an image file .png
Posted: Fri Jul 06, 2012 5:10 pm
by jmburnod
Hi All,
Is there a way to get the width and height of an image file .png without display it in an image ?
Best regards
Jean-Marc
Re: get the width and height of an image file .png
Posted: Fri Jul 06, 2012 5:21 pm
by Klaus
Hi Jean-Marc,
yes
Do this:
Code: Select all
...
## This is the trick:
lock screen
## :-)
import paint from file "your/file/here.png"
put the formattedwidth of last img into tWidth
put the formattedheight of last img into tHeight
delete last img
unlock screen
...
Look ma, no hands erm... display
Best
Klaus
Re: get the width and height of an image file .png
Posted: Fri Jul 06, 2012 5:28 pm
by jmburnod
Hi Klaus,
Yes, I do that.
i mean a way to get the width and height without import it
Best
Jean-Marc
Re: get the width and height of an image file .png
Posted: Fri Jul 06, 2012 5:41 pm
by Klaus
Bonsoir Jean-Marc,
jmburnod wrote:i mean a way to get the width and height without import it
No, sorry.
On the Mac you could use shell and SIPS to get this (and more!) info about an image!
Open the terminal and enter:
man sips
This will show you the help pages of SIPS, which is very powerful.
Here an old script of mine:
Code: Select all
on mouseUp
answer file "Bild laden.../Load image..."
put QUOTE & it & QUOTE into bild
## Get ehight of image:
get shell("sips -g pixelHeight" && bild)
## It is the last word
put last word of it into hoch
## Same for width:
get shell("sips -g pixelWidth" && bild)
put last word of it into breit
put breit & "," & hoch into fld 1
end mouseUp
No idea about Windows...
Maybe you could use "ImageMagick", but this is not installed per default on Windows.
Best
Klaus
Re: get the width and height of an image file .png
Posted: Fri Jul 06, 2012 5:57 pm
by mwieder
The PNG file format is well-defined and documented.
Read and toss the first 12 bytes of the image file.
Read the next 4 bytes from the file. These should be "IHDR".
The next 4 bytes are the width in hex.
The next 4 bytes of the height in hex.
Re: get the width and height of an image file .png
Posted: Fri Jul 06, 2012 6:21 pm
by Klaus
Ah, great info, thanks, Mark!
Re: get the width and height of an image file .png
Posted: Sat Jul 07, 2012 2:47 pm
by jmburnod
Hi Klaus and mark,
Thanks for reply
i tested the shell and "setting the filename" ways for 100 files .png with these results
• Shell: 614 ticks
• set the filename; 42 ticks
I also readed the Mark's suggesting, but i don't know how can i use it.
I tried byteToNum like that:
Code: Select all
on WayByHex
answer file "open:"
if it = empty then exit WayByHex
put it into tFile
-- binfile or file ?
put url("binfile:" & tfile) into tBuf -- return 001244,001244 at the end of script
-- put url("file:" & tfile) into tBuf -- return 32124432,3212448 at the end of script
repeat with i = 17 to 20
put byteToNum(byte i of tBuf) after tW
end repeat
repeat with i = 21 to 24
put byteToNum(byte i of tBuf) after tH
end repeat
put tW & "," & tH
end WayByHex
with this result: 001244,001244 for a png file 500,500
I'm afraid i cooked a strange thing
Best regard
Jean-Marc
Re: get the width and height of an image file .png
Posted: Sat Jul 07, 2012 5:10 pm
by Klaus
Hi Jean-Marc,
maybe one needs ot "binarydecode" this somehow?
Unfortunately I do not have the slightest idea about this binary stuff!
Mark, could you give us a little hint?
Thanks!
But 42 ticks for 100 images setting the filename is not too bad, I think!
Don't think that reading the 100 binary files will be faster.
Best
Klaus
Re: get the width and height of an image file .png
Posted: Sat Jul 07, 2012 5:27 pm
by jmburnod
Hi Klaus,
42 ticks for 100 images setting the filename is not too bad, I think!
Yes. With a lock screen of course but i believe that is no good for the processor.I'm right or not ?
That is the reason i search an other way
Best
Jean-Marc
Re: get the width and height of an image file .png
Posted: Sat Jul 07, 2012 5:43 pm
by Klaus
jmburnod wrote:... but i believe that is no good for the processor.
Hey, that's his JOB!
No, really, don't worry, this is OK!
Re: get the width and height of an image file .png
Posted: Sat Jul 07, 2012 6:40 pm
by LittleGreyMan
Salut Jean-Marc,
jmburnod wrote:with this result: 001244,001244 for a png file 500,500
which is, AFAIK, the good result.
As mwieder wrote, the value is an hex value.
001 244 reads 01 F4 in hex, which is 500 in decimal
1x256 + 244 = 500
You'll have to manage the conversion.
Re: get the width and height of an image file .png
Posted: Sat Jul 07, 2012 7:28 pm
by FourthWorld
jmburnod wrote:Code: Select all
repeat with i = 17 to 20
put byteToNum(byte i of tBuf) after tW
end repeat
That code is translating a single byte at a time, concatenating the result with the other three in the set. The result will be a long string, but not the true value of the four-byte binary number.
To translate multi-byte binary values use the binaryDecode function, used here reading only the relevant first 24 bytes since we don't actually need the entire image data:
Code: Select all
on mouseUp
answer file "Select a PNG file:" with type "PNG File|png|PNGf"
if it is empty then exit to top
put it into tFile
open file tFile for binary read
read from file tFile for 24
put it into tData
close file tFile
--
if byte 13 to 16 of tData <> "IHDR" then
answer "Invalid PNG format"
exit to top
end if
--
local tWidth, tHeight
put binaryDecode("MM", byte 17 to 24 of tData, tWidth, tHeight)\
into tNumConvertedVals
if tNumConvertedVals <> 2 then
answer "Couldn't decode binary data"
exit to top
end if
put tWidth, tHeight
end mouseUp
Many thanks to mwieder for digging up that PNG header info.
Re: get the width and height of an image file .png solved
Posted: Sat Jul 07, 2012 9:54 pm
by jmburnod
Didier...
Thank for explanation (you win a beer with some "gratons" i know you are a "gone")
Mark... many thanks for help
Richard…
Thank one more for script and explanations
This is the faster way:
2 ticks for 100 image files
12 ticks for 4792 image files
27 ticks for 7385 image files
Code: Select all
on mouseup
answer folder "Select a folder with PNG files:"
if it = empty then exit mouseup
put the ticks into old
lock screen
put it into tPathFol
put LesFilesInFol(tPathFol) into tFiles
--put line 1 to 100 of tFiles into tFiles
repeat for each line tLine in tFiles
put tPathFol & "/"& tLine into tPathIm
open file tPathIm for binary read
read from file tPathIm for 24
put it into tData
close file tPathIm
--
if byte 13 to 16 of tData <> "IHDR" then
next repeat
end if
--
local tWidth, tHeight
put binaryDecode("MM", byte 17 to 24 of tData, tWidth, tHeight)\
into tNumConvertedVals
if tNumConvertedVals <> 2 then
next repeat
end if
put tLine & numtochar(1) & tWidth & "," & tHeight & return after rGetAllDimImg
end repeat
delete char -1 of rGetAllDimImg
put the ticks - old && the num of lines of rGetAllDimImg& return & rGetAllDimImg -- 2 ticks for 100 image files !
end mouseup
function LesFilesInFol tFol -- files in a folder
put empty into rLesFilesInFol
set the defaultFolder to tFol
put the files into rLesFilesInFol
filter rLesFilesInFol without ".*"
return rLesFilesInFol
end LesFilesInFol
Best regards
Jean-Marc
Re: get the width and height of an image file .png solved
Posted: Sat Jul 07, 2012 10:24 pm
by LittleGreyMan
jmburnod wrote:Thank for explanation
You're welcome. Hex is sort of an aggregate of binary digits.
"There are 10 types of people in the world: those who understand binary, and those who don't."
Welcome to the club!
And thanks for the Cardinal (Swiss beer), but Richard and mwieder deserve a full pack!
Re: get the width and height of an image file .png solved
Posted: Sun Jul 08, 2012 12:14 pm
by Klaus
jmburnod wrote:This is the faster way:
2 ticks for 100 image files
12 ticks for 4792 image files
27 ticks for 7385 image files
WOW! Très cool
