Getting the formattedWidth and height of multiple images

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Getting the formattedWidth and height of multiple images

Postby quailcreek » Tue Sep 12, 2017 8:19 pm

Hi,
I'm trying to pull in multiple images that have been exported as .png to a specific folder on iOS. Everything works without any errors. I can get the original image from the users library, album or camera and save it as a .png. The problem comes when retrieving multiple .png files. The code below steps thru for each .png file and puts them each into a separate image object in a group.

Here's what's happening.
Only the final image in the repeat provides the formattedwidth and formattedheight. All the rest are "0". If there is only 1 .png it provides the true formattedwidth and formattedheight. If there are more than 1 .png files only the last one provides the true ormattedwidth and formattedheight even if all of the .png files are the same image.

So my question is, why am I only getting the correct values on the last .png? I tried putting wait statements is various places, thinking that LC was jut too fast for it's own good but no-joy.

Code: Select all
put 1 into theNewImage
      repeat for each line theLine in tFiles -- tFiles is a list of the .png file names
        put thePath & "/" & theLine into tPicPath -- build the path to each .png file
       
        put line theNewImage of the_Group_IDs into theGrp_ID -- here I'm getting the ID of a group which contains an image object
       
        set the filename of control 1 of grp ID theGrp_ID of grp "picGroup" to tPicPath -- this sets the image of the image object
       
        put the formattedwidth of the last image of this cd into tWidthSource
        --         get the formattedwidth of the last image of this cd -- this didn't work either
        --         put it into tWidthSource
        put the formattedheight of the last image of this cd into tHeightSource
        --         get the formattedheight of the last image of this cd
        --         put it into tHeightSource
       
        if tWidthSource = "0" then put "240" into tWidthSource -- I had to add this to get around the 0. The size of the image objects is 240 x 240
        if tHeightSource = "0" then put "240" into tHeightSource
       
        answer "tWidthSource" && tWidthSource &CR& "tHeightSource" && tHeightSource
Tom
MacBook Pro OS 10.12.5
quailcreek
 
Posts: 586
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Getting the formattedWidth and height of multiple images

Postby Klaus » Tue Sep 12, 2017 8:32 pm

Hi Tom

Code: Select all
...control 1 of grp ID theGrp_ID of grp "picGroup"...

is NOT neccessarily
Code: Select all
the last image of this cd

But you always only check the latter.

Should probably read:
Code: Select all
...
put the formattedwidth of control 1 of grp ID theGrp_ID of grp "picGroup" into tWidthSource
put the formattedheight of control 1 of grp ID theGrp_ID of grp "picGroup" into tHeightSource
...

Best

Klaus
Klaus
 
Posts: 9495
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany

Re: Getting the formattedWidth and height of multiple images

Postby quailcreek » Tue Sep 12, 2017 8:57 pm

Thanks Klaus. That was the answer!
Tom
MacBook Pro OS 10.12.5
quailcreek
 
Posts: 586
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Getting the formattedWidth and height of multiple images

Postby jmburnod » Tue Sep 12, 2017 10:43 pm

Hi quailcreek ,
If you have a lot of img you may use this function which return width and height without displaying image (.png only).
It is faster because it avoid image display

2 ticks for 100 image files
12 ticks for 4792 image files
27 ticks for 7385 image files

Here is the thread about it
http://forums.livecode.com/viewtopic.php?f=8&t=12386

Code: Select all
function GetWidthHeightPngFile pPathIm --
   put empty into rGetWidthHeightPngFile
   open file pPathIm for binary read
     read from file pPathIm for 24
     put it into tData
     close file pPathIm
     if byte 13 to 16 of tData <> "IHDR" then -- "Invalid PNG format"
      return empty
      exit GetWidthHeightPngFile
     end if
     local tWidth, tHeight
     put binaryDecode("MM", byte 17 to 24 of tData, tWidth, tHeight)\
         into tNumConvertedVals
     if tNumConvertedVals <> 2 then -- Couldn't decode binary data
       return empty
      exit GetWidthHeightPngFile
     end if
   put tWidth & "," & tHeight into rGetWidthHeightPngFile
   return rGetWidthHeightPngFile
end GetWidthHeightPngFile

Best regards
Jean-Marc
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 1930
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève

Re: Getting the formattedWidth and height of multiple images

Postby quailcreek » Wed Sep 13, 2017 12:10 am

Thanks, Jean-Marc. Right now I'm only dealing with a few image files but as they say... "We all have a need for speed!"
Tom
MacBook Pro OS 10.12.5
quailcreek
 
Posts: 586
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA


Return to Talking LiveCode

Who is online

Users browsing this forum: No registered users and 3 guests