Page 1 of 8
Images in Livecode
Posted: Tue Jul 25, 2017 9:49 am
by bidgeeman
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
Re: Images in Livecode
Posted: Tue Jul 25, 2017 11:22 am
by jmburnod
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
Re: Images in Livecode
Posted: Tue Jul 25, 2017 12:07 pm
by bidgeeman
Thanks again Jean-Marc
Regards
Bidge
Re: Images in Livecode
Posted: Wed Jul 26, 2017 3:38 am
by bidgeeman
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
Re: Images in Livecode
Posted: Wed Jul 26, 2017 9:40 am
by Klaus
Hi David,
...
answer file "Choose a photo:" with type "Jpeg, PNG, BMP|jpg,png,bmp|"
...
Best
Klaus
Re: Images in Livecode
Posted: Wed Jul 26, 2017 9:50 am
by bidgeeman
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
Re: Images in Livecode
Posted: Wed Jul 26, 2017 10:12 am
by Klaus
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"
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
Re: Images in Livecode
Posted: Wed Jul 26, 2017 11:55 am
by bidgeeman
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
Re: Images in Livecode
Posted: Wed Jul 26, 2017 12:30 pm
by Klaus
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!
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
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
Re: Images in Livecode
Posted: Wed Jul 26, 2017 12:51 pm
by bidgeeman
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
Re: Images in Livecode
Posted: Tue Aug 01, 2017 6:47 am
by bidgeeman
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
Re: Images in Livecode
Posted: Tue Aug 01, 2017 7:45 am
by AndyP
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
Re: Images in Livecode
Posted: Tue Aug 01, 2017 7:51 am
by bidgeeman
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
Re: Images in Livecode
Posted: Tue Aug 01, 2017 8:00 am
by AndyP
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
Re: Images in Livecode
Posted: Tue Aug 01, 2017 8:23 am
by bidgeeman
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