get the width and height of an image file .png
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
get the width and height of an image file .png
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
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
https://alternatic.ch
Re: get the width and height of an image file .png
Hi Jean-Marc,
yes
Do this:
Look ma, no hands erm... display
Best
Klaus
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
...

Best
Klaus
Re: get the width and height of an image file .png
Hi Klaus,
Yes, I do that.
i mean a way to get the width and height without import it
Best
Jean-Marc
Yes, I do that.
i mean a way to get the width and height without import it
Best
Jean-Marc
https://alternatic.ch
Re: get the width and height of an image file .png
Bonsoir Jean-Marc,
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:
No idea about Windows...
Maybe you could use "ImageMagick", but this is not installed per default on Windows.
Best
Klaus
No, sorry.jmburnod wrote:i mean a way to get the width and height without import it
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
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
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.
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
Ah, great info, thanks, Mark!
Re: get the width and height of an image file .png
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:
with this result: 001244,001244 for a png file 500,500
I'm afraid i cooked a strange thing
Best regard
Jean-Marc
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
I'm afraid i cooked a strange thing
Best regard
Jean-Marc
https://alternatic.ch
Re: get the width and height of an image file .png
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
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
Hi Klaus,
That is the reason i search an other way
Best
Jean-Marc
Yes. With a lock screen of course but i believe that is no good for the processor.I'm right or not ?42 ticks for 100 images setting the filename is not too bad, I think!
That is the reason i search an other way
Best
Jean-Marc
https://alternatic.ch
Re: get the width and height of an image file .png
Hey, that's his JOB!jmburnod wrote:... but i believe that is no good for the processor.



No, really, don't worry, this is OK!
-
- Posts: 49
- Joined: Sat Jun 16, 2012 7:57 pm
Re: get the width and height of an image file .png
Salut Jean-Marc,
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.
which is, AFAIK, the good result.jmburnod wrote:with this result: 001244,001244 for a png file 500,500
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.
Best regards,
Didier
Didier
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: get the width and height of an image file .png
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.jmburnod wrote:Code: Select all
repeat with i = 17 to 20 put byteToNum(byte i of tBuf) after tW end repeat
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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: get the width and height of an image file .png solved
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
Best regards
Jean-Marc
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
Jean-Marc
Last edited by jmburnod on Sat Jul 07, 2012 10:28 pm, edited 2 times in total.
https://alternatic.ch
-
- Posts: 49
- Joined: Sat Jun 16, 2012 7:57 pm
Re: get the width and height of an image file .png solved
You're welcome. Hex is sort of an aggregate of binary digits.jmburnod wrote:Thank for explanation
"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!
Best regards,
Didier
Didier
Re: get the width and height of an image file .png solved
WOW! Très cooljmburnod wrote:This is the faster way:
2 ticks for 100 image files
12 ticks for 4792 image files
27 ticks for 7385 image files
