get the width and height of an image file .png

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

get the width and height of an image file .png

Post by jmburnod » Fri Jul 06, 2012 5:10 pm

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
https://alternatic.ch

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get the width and height of an image file .png

Post by Klaus » Fri Jul 06, 2012 5:21 pm

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 :D


Best

Klaus

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: get the width and height of an image file .png

Post by jmburnod » Fri Jul 06, 2012 5:28 pm

Hi Klaus,

Yes, I do that.
i mean a way to get the width and height without import it

Best
Jean-Marc
https://alternatic.ch

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get the width and height of an image file .png

Post by Klaus » Fri Jul 06, 2012 5:41 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: get the width and height of an image file .png

Post by mwieder » Fri Jul 06, 2012 5:57 pm

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.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get the width and height of an image file .png

Post by Klaus » Fri Jul 06, 2012 6:21 pm

Ah, great info, thanks, Mark!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: get the width and height of an image file .png

Post by jmburnod » Sat Jul 07, 2012 2:47 pm

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. :oops:

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
https://alternatic.ch

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get the width and height of an image file .png

Post by Klaus » Sat Jul 07, 2012 5:10 pm

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! 8)
Don't think that reading the 100 binary files will be faster.

Best

Klaus

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: get the width and height of an image file .png

Post by jmburnod » Sat Jul 07, 2012 5:27 pm

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
https://alternatic.ch

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get the width and height of an image file .png

Post by Klaus » Sat Jul 07, 2012 5:43 pm

jmburnod wrote:... but i believe that is no good for the processor.
Hey, that's his JOB! :D :D :D

No, really, don't worry, this is OK!

LittleGreyMan
Posts: 49
Joined: Sat Jun 16, 2012 7:57 pm

Re: get the width and height of an image file .png

Post by LittleGreyMan » Sat Jul 07, 2012 6:40 pm

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.
Best regards,

Didier

FourthWorld
VIP Livecode Opensource Backer
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

Post by FourthWorld » Sat Jul 07, 2012 7:28 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: get the width and height of an image file .png solved

Post by jmburnod » Sat Jul 07, 2012 9:54 pm

Didier...
Thank for explanation (you win a beer with some "gratons" i know you are a "gone") :D
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
Last edited by jmburnod on Sat Jul 07, 2012 10:28 pm, edited 2 times in total.
https://alternatic.ch

LittleGreyMan
Posts: 49
Joined: Sat Jun 16, 2012 7:57 pm

Re: get the width and height of an image file .png solved

Post by LittleGreyMan » Sat Jul 07, 2012 10:24 pm

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! :D

And thanks for the Cardinal (Swiss beer), but Richard and mwieder deserve a full pack!
Best regards,

Didier

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get the width and height of an image file .png solved

Post by Klaus » Sun Jul 08, 2012 12:14 pm

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 :D

Post Reply