Images in Livecode

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
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Images in Livecode

Post by bidgeeman » Tue Jul 25, 2017 9:49 am

Hello.
A basic question about images in Livecode. I would like to know if I import an image into a stack as a control object
do I have to reference that image in a folder when building a standalone? or is the image hard
coded into the stack once imported a s a control object?

Thanks
Bidge

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Images in Livecode

Post by jmburnod » Tue Jul 25, 2017 11:22 am

Hi Bidge
if I import an image into a stack as a control object
do I have to reference that image in a folder when building a standalone?
No. You only have to do that if your image have a filename.
Best regards
Jean-Marc
Last edited by jmburnod on Tue Jul 25, 2017 12:56 pm, edited 1 time in total.
https://alternatic.ch

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Tue Jul 25, 2017 12:07 pm

Thanks again Jean-Marc :)

Regards
Bidge

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Wed Jul 26, 2017 3:38 am

Hello again.
I have another question regarding images. I have a code that loads an image in a folder but what happens if someone tries to load a file that is not an image? How do you write this in so that it answers" This is not an image. Please choose an image"? I only want to use JPG, PNG and BMP.
The code could match the file extensions but I am not sure how to write it.


Code: Select all

on mouseUp
   answer file "CHOOSE A PHOTO"
   if the result is "Cancel" then exit mouseUp
   else
      if it is not empty then
         set the filename of image "i1" of stack "ImageArea"  to it
      end if
   end if
Many thanks
Bidge

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

Re: Images in Livecode

Post by Klaus » Wed Jul 26, 2017 9:40 am

Hi David,

...
answer file "Choose a photo:" with type "Jpeg, PNG, BMP|jpg,png,bmp|"
...

Best

Klaus

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Wed Jul 26, 2017 9:50 am

Hi Klaus.
That worked great thank you.
What if my code points to a folder this is supposed to contain several images to load
and there is nothing there to load? How do we help the user there?

Code: Select all

on mouseUp
   
answer folder "CHOOSE AN IMAGE FOLDER"
if the result is "Cancel" then exit mouseUp
else
      
if it <> "" then

 set the defaultfolder to it
put the files into myListOfFiles
filter myListOfFiles with "*.png"     
repeat for each line myFile in myListOfFiles        
import paint from file myFile into card "Images" of stack "ImageLoad"
end repeat
         
end if
end if

Thanks again
Bidge

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

Re: Images in Livecode

Post by Klaus » Wed Jul 26, 2017 10:12 am

Hi David,

your approach is correct, but you will need to use FILTER several types to get your desired formats
and no need to mess around with "the defaultfolder" anymore" :D

And please format your scripts (JUST hit the TAB key from time to time in the editor) so they become better readable!

Code: Select all

on mouseUp
   
   ## Don't YELL at your users :-)
   answer folder "Choose a folder:"
   
   ##  if the result is "Cancel" then exit mouseUp
   ## When THE RESULT = CANCEL, IT is also EMPTY!
   if it = EMPTY then
      exit mouseup
   end if   
   ## I alsway avoid END IFs wherever possible!
   
   ## NEVER use IT more than neccessary!
   put it into tFolder
   
   ## set the defaultfolder to it
   ## New syntax:
   put files(tFolder) into myListOfFiles
   ## put the files into myListOfFiles
   
   ## Maybe there is a REGEX pattern we could usewith FILTER, but this is still a complete mistery to me :-D
   put myListOfFiles into tJpegs
   put myListOfFiles into tPngs
   put myListOfFiles into tBmps
   
   filter tJpegs with "*.jpg" 
   filter tPngs with "*.png" 
   filter tBmps with "*.bmp"    
   
   put tJpegs & CR & tPngs & CR & tBmps into tCompleteList
   
   ## Remove empty lines:
   filter tCompleteList without EMPTY
   
   repeat for each line myFile in tCompleteList        
    ##  import paint from file myFile into card "Images" of stack "ImageLoad"
      import paint from file (tFolder & "/" & myFile)
      
      ## "import ... INTO ..." will not work, IMPORT always goes into the current card!
      ## But if I remember correctly we already had this one 8-)
   end repeat 
end mouseup
Best

Klaus

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Wed Jul 26, 2017 11:55 am

Hi Klaus.
Sorry but this code imports the files directly into my interface stack.
I was wanting to import them into a separate stack called "imageLoad".

Also I don't think I explained myself correctly but I was wanting to know
how to handle a situation where a user has selected a folder with no
images in it. Is there some way of telling them the folder contains no images?

Thanks
Bidge

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

Re: Images in Livecode

Post by Klaus » Wed Jul 26, 2017 12:30 pm

Hi David,
bidgeeman wrote:Hi Klaus.
Sorry but this code imports the files directly into my interface stack.
I was wanting to import them into a separate stack called "imageLoad".
yes, didn't you read the comment in my script?
"import ... INTO..." is not supported in LC!

And do something for your memory capabilities! We've been there (and somewhere else) before! 8)
http://forums.livecode.com/viewtopic.ph ... 99#p155599
bidgeeman wrote: Also I don't think I explained myself correctly but I was wanting to know
how to handle a situation where a user has selected a folder with no
images in it. Is there some way of telling them the folder contains no images?
Sure, use an "Answer" dialog :D

Check right here at this place in the script (quite logical, innit?):

Code: Select all

...
## Remove empty lines:
filter tCompleteList without EMPTY

## No images in folder:
if tCompleteList = EMPTY then
  answer "No images in this folder, idiot!"
  exit to top
end if
...
Best

Klaus

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Wed Jul 26, 2017 12:51 pm

I thank once again Klaus :) Also for the memories. I needed to study that old thread once again to understand how it works.

Regards
Bidge

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Tue Aug 01, 2017 6:47 am

Hi.
I have another image question please?
When loading an image from a folder in the same directory as the Livecode Stack,
what is the correct way of writing this in code?
This doesn't work and I don't understand why?
EDIT:

Code: Select all

 put "Images/crow.jpg" into image "Image1" 
Not sure how to write the folder path so I have been storing all my images on a separate
stack and loading them when required but this is making the stack a large.

Thanks again for any help.
Bidge

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Images in Livecode

Post by AndyP » Tue Aug 01, 2017 7:45 am

Try this

Code: Select all

on mouseUp
   local tMyPath
   local tFile

   set the itemDelimiter to "/"
   put (item 1 to -2 of the effective fileName of this stack) & "/images/" into tMyPath
   -- tMyPath now contains the path to your stack and sub folder images
   
   put tMyPath & "crow.jpg" into tFile
   -- tFile contains the full path to your image
   
   put  Url ("binfile:" & tFile) into image "Image1"
   -- "binfile:" tell the stack that it is expecting data in binary format
end mouseUp
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Tue Aug 01, 2017 7:51 am

Hi Andy.
Sorry I don't think I made myself clear. I just need to load an image from a "SET" images folder that ports with the standalone. I'm not sure how to load any image from this directory when the card loads?

Thanks
Bidge

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Images in Livecode

Post by AndyP » Tue Aug 01, 2017 8:00 am

If I understand you correctly?

to change the directory you are going to load your images from
just change "/images/" to the name of the directory you will be using to load the images.

and

change the on mouseUp handler in the example to on openCard
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Images in Livecode

Post by bidgeeman » Tue Aug 01, 2017 8:23 am

Hi Andy. I though I did that and had the image directory path correct.
I have up-loaded the stack if someone please look at it and see why it is not loading the "crow.jpg" image?
It's more than likely my fault.

Many thanks
Bi9dge
Attachments
LoadImage.zip
Why is this not working?
(9.93 KiB) Downloaded 164 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”