Page 1 of 1

Get an image file size

Posted: Fri Nov 23, 2012 10:20 am
by doobox
I may well be well of the mark here, but this is what i have, and it does not yield the result i expected :

Code: Select all

   put specialfolderpath("home") & "/Pictures/plane.jpg" into tImagefile // 446,502 bytes in finder get info panel
   set the filename of img "holder" to tImagefile // put the image into temp placeholder
   put the imagedata of image "holder" into tData
   put the number of bytes of tData into field "test" // shows 2124036 why..?????
As you can see i would have expected the total bytes output to field "test" to be 446,502

Re: Get an image file size

Posted: Fri Nov 23, 2012 10:37 am
by Klaus
Hi Gary,

I dont think counting the bytes on screen will tell you the actual filesize!
But you can get the real filesize with Livecode, check (long or detailed) "files" in the dictionary for more info.

Here is a function of mine that I use to get the filesize of any given file on disk:
tFilepath = abolute pathname of the file

Code: Select all

function fFileSize tFilePath
   set itemdel to "/"
   put item -1 of tFilePath into tFile
   delete item -1 of tFilePath
   put the folder into OldDir
   set folder to tFilePath
  
   ## Checking the LONG files is the trick!
   put the long files into tLF

   ## the actual filename is URLENCODED in "the long files"
   put line(lineoffset(urlencode(tFile),tLF)) of tLF into tLine
   set itemdel to ","
   set folder to OldDir

   ## ITEM 2 in that line is the filesize in BYTES
   return item 2 of tLine
end fFileSize
Best

Klaus

Re: Get an image file size

Posted: Fri Nov 23, 2012 10:47 am
by doobox
Ah.. thanks Klaus, your a star :-)

Re: Get an image file size

Posted: Fri Nov 23, 2012 11:05 am
by bn
Hi Gary,

you look at the filsize in the finder for a jpeg image. Jpeg images are most of the time compressed. If you check the size of the imageData you have the uncompressed size of the image.

Also imageData is not all there is sizewise. It is only a representation of the pixel, i.e. 4 bytes per pixel. Additionally you have alphaData and maskData.

To see the size of the image when expanded in liveCode you could put
put image "xyz" into tTotalImage /or/ put the text of image "xyz" into tTotalImage
put length of tTotalImage

Kind Regards
Bernd

Re: Get an image file size

Posted: Fri Nov 23, 2012 11:16 am
by doobox
Thanks Bernd,

That would have been neater for the code i presented.
But i was only really expanding the image in LC to get the size.
Now with Klaus solution i don't ever need to expand the image. I can just get the size, and move on (looping).

Your solution provides another little wrinkle in my brain though.. cheers :-)