Get an image file size

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Get an image file size

Post by doobox » Fri Nov 23, 2012 10:20 am

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
Kind Regards
Gary

https://www.doobox.co.uk

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

Re: Get an image file size

Post by Klaus » Fri Nov 23, 2012 10:37 am

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

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Get an image file size

Post by doobox » Fri Nov 23, 2012 10:47 am

Ah.. thanks Klaus, your a star :-)
Kind Regards
Gary

https://www.doobox.co.uk

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Get an image file size

Post by bn » Fri Nov 23, 2012 11:05 am

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

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Get an image file size

Post by doobox » Fri Nov 23, 2012 11:16 am

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 :-)
Kind Regards
Gary

https://www.doobox.co.uk

Post Reply