[SOLVED] Image problems

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

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

[SOLVED] Image problems

Post by redfield » Sat Jul 04, 2020 10:29 am

Hi guys,

I am trying to display an image at a certain location. Rather various images, therefore I put the image names into an array. Trying to display them gave me object expression errors. With all my different trials I am totally confused, so please forgive me when I don't post them and the relating errors. I have googled around and copied some code but also with this last try I am getting the error "no such object":

Code: Select all

put "/home/Documents/Pics/someImage.png" into varImage
put url("binfile:" & varImage) into imageNames[11]
put imageNames[11] into img "theImage"
set the location of image ""theImage" to 100,100
When I do an

Code: Select all

answer imageNames[11]
the result is something like []PNG and under it another []. Help please :D
Last edited by redfield on Sat Jul 04, 2020 1:49 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Image problems

Post by richmond62 » Sat Jul 04, 2020 10:42 am

Dunno, but you could try this:

Code: Select all

put "/home/Documents/Pics/someImage.png" into varImage
put url("binfile:" & varImage) into imageNames[11]
set the filename of img "theImage" to imageNames[11]
set the location of image ""theImage" to 100,100

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Image problems

Post by SparkOut » Sat Jul 04, 2020 10:55 am

You have put the actual binary date of the image file into the array.

To set the filename of the image object, you want to give it the path you stored in the variable varImage. This will give a display of a "referenced image" - in other words the image data is stored in the external file and not part of the stackfile, just the reference is. So to move the stack you will also have to move or re-reference the external image files.

Or, to put the binary data that you imported into the array into the image for display, you can (oddly enough) set the text of the image. This will in essence "import" the data that becomes a part of the stack and will increase its size, but will be portable without having to make the external image files portable along with it.

Code: Select all

set the filename of image "theImage" to varName
or

Code: Select all

set the text of image "theImage" to imageNames[11]

[Edit]and you can also

Code: Select all

put url ("binfile:" & varName) into image "theImage"
as well as Klaus said. Additionally you could

Code: Select all

import paint from file varName
--but that will create a new image object each time, so you would want to name the new object whose long id is returned in the "it" variable after creation
set the name of it to "theNewImage" --or a different name each time you import
Last edited by SparkOut on Sat Jul 04, 2020 11:47 am, edited 3 times in total.

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

Re: Image problems

Post by Klaus » Sat Jul 04, 2020 11:38 am

Hi friends,

we actually CAN put binary stuff into any image and that will work! 8)
Just tested this and it works of course:

Code: Select all

...
put url("binfile:/Volumes/1TB/Dokumente2/RRMC etc Hilfe/Andernfalls/Grafik/Koelnrockt_Logo_2011.jpg") into imageNames[11]
put imageNames[11] into img 1
...
Et voila, the image is displayed, also tested with a PNG image.
And redfield did not say he/she wants to set the filename of the image!

Could you please post a screenshot of the problem?


Best

Klaus

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

Re: Image problems

Post by Klaus » Sat Jul 04, 2020 11:41 am

Hi redfield,
redfield wrote:
Sat Jul 04, 2020 10:29 am
...When I do an

Code: Select all

answer imageNames[11]
the result is something like []PNG and under it another [].
yes, that is how BINARY data look in a (text!) field! :D
Remember:
put url("BINfile:" & varImage) into imageNames[11]
So THAT was your problem in the end?
Did not get this at first read...


Best

Klaus

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

Re: Image problems

Post by Klaus » Sat Jul 04, 2020 11:58 am

richmond62 wrote:
Sat Jul 04, 2020 10:42 am

Code: Select all

put "/home/Documents/Pics/someImage.png" into varImage
## put url("binfile:" & varImage) into imageNames[11]
set the filename of img "theImage" to imageNames[11]
set the location of image ""theImage" to 100,100
Will work for the filename, IF you comment out line 2! 8)

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Image problems

Post by redfield » Sat Jul 04, 2020 12:23 pm

Thank you so much for your suggestions party people! I tried them but:

This doesn't work, I get "no such object" - also after commenting out the third line:

Code: Select all

put "someImage.png" into imageNames[11]
put "/home/Documents/Pics/someImage.png" into varImage
put url("binfile:" & varImage) into imageNames[11]
set the filename of img "theImage" to imageNames[11] // no such object
set the location of image ""theImage" to 100,100
This also doesn't work, same error:

Code: Select all

put "/home/Documents/Pics/someImage.png" into varImage
put varImage into imageNames[11]
set the text of image "theImage" to imageNames[11] // no such object
This gives no error, but it doesn't display the image:

Code: Select all

put url("binfile:/home/Documents/Pics/someImage.png") into imageNames[11]
put imageNames[11] into img1 
@Klaus: you asked for a screenshot. You mean of the card? It's only an empty card with a button on it. Or do you mean a screenshot of the script editor?

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Image problems

Post by redfield » Sat Jul 04, 2020 12:28 pm

Damn it! When I add

Code: Select all

set the location of image img1 to 100,100
after

Code: Select all

put url("binfile:/home/Documents/Pics/someImage.png") into imageNames[11]
put imageNames[11] into img1 
I get the "no such object" error again :(

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Image problems

Post by SparkOut » Sat Jul 04, 2020 12:30 pm

You're mixing and matching a lot of the different possible combinations and methods referred to, with errors resulting:

Code: Select all

put "someImage.png" into imageNames[11] -- this is the name of the file only, no path and no data
 -- correction: not necessary here - if you commented out the 3rd line of your script above, then the filename alone will not help without the path too
 
put "/home/Documents/Pics/someImage.png" into varImage -- this is the path, and use varImage as the path if you want to set the filename
-- correction: change to use:
set the filename of img "theImage" to varImage

-- ******** OR ********

put url("binfile:" & varImage) into imageNames[11] -- this is the binary data
set the filename of img "theImage" to imageNames[11] // no such object -- because you are using the binary data you read in the line above
-- correction: change to use:
set the text of image "theImage" to imageNames[11]
-- or
put imageNames[11] into image "theImage" 


set the location of image ""theImage" to 100,100
For

Code: Select all

put url("binfile:/home/Documents/Pics/someImage.png") into imageNames[11]
put imageNames[11] into img1 
I think you are mistaking Klaus' shorthand reference img 1 (the first image on the card) with img1 (a non-existent object name - which should be in quotes - and preceded by the object type, unless you do actually have a variable img1 which contains the correct full object reference)

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Image problems

Post by SparkOut » Sat Jul 04, 2020 12:53 pm

Perhaps we could also take a step back and ask for some more detail about what you are trying to do - how do you want the interface to look and what actions you want to change the view of images and what the application is for - how are you planning on packaging and delivering the different images?

More info about what you want to achieve will help to show you a technique that will be more useful in the given scenario than trying to mix and match snippets and lines of code

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

Re: Image problems

Post by Klaus » Sat Jul 04, 2020 12:59 pm

What SparkOut said!

"no such object" means there is no object (image) with the name you wrote in your script:

Code: Select all

...
put "/home/Documents/Pics/someImage.png" into varImage
put url("binfile:" & varImage) into imageNames[11]

## ALWAYS, yes, ALWAYS use quotes around names!
## ALWAYS! 
## The engine is less forgiving (with "sloppy" code) with every new version!
set the text of image "theImage" to imageNames[11] // no such object
set the location of image "theImage" to 100,100
## Did I mention ALWAYS! :-D
...
requires that you have an image object on the card named "img1".

And as SparkOut wrote, I was referring to the single image (= image 1) on the card of my test stack.
And I was asking for a screenshot because I did not understand your problem at first. :D

So your first code is definitively correct, however it requires an image object on the card named "theImage" or you get "no such object":

Code: Select all

...
put "/home/Documents/Pics/someImage.png" into varImage
put url("binfile:" & varImage) into imageNames[11]
put imageNames[11] into img "theImage"
set the location of image ""theImage" to 100,100
...
And now that you know how BINARY data look in a TEXT field, this should not surprise you anymore. :-)

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

Re: Image problems

Post by Klaus » Sat Jul 04, 2020 1:06 pm

I can highly recommend these stacks learn more about the basics of LC:
http://www.hyperactivesw.com/revscriptc ... ences.html

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Image problems

Post by redfield » Sat Jul 04, 2020 1:26 pm

I am dying here of embarrassment and you're both right, I'm mixing up everything and misunderstood the "img1" thing from Klaus's code too :oops: . And I still don't get it to work :oops: :oops: :

Code: Select all

put "/home/Documents/Pics/someImage.png" into varImage 
put url("binfile:" & varImage) into imageNames[11]
put imageNames[11] into img "theImage"
set the location of image "theImage" to 100,100
Does this not create the object needed?

Code: Select all

put imageNames[11] into img "theImage"
In the long term the goal is a simple Memory game. For now the goal is only to display an image on the screen (card). I am assuming I don't need to do an "Import As Control" -> "Image File", to first add the image to the stack, because there is a file path involved in the code (-> put "/home/Documents/Pics/someImage.png") ... :?:

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Image problems

Post by richmond62 » Sat Jul 04, 2020 1:35 pm

No object will be created by your code, you need to set up a blank image on your stack:
-
blankImage.png
-
then give it your name "theImage" via the properties palette:
-
Screenshot 2020-07-04 at 15.34.57.png

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

Re: Image problems

Post by Klaus » Sat Jul 04, 2020 1:36 pm

Hi redfield,
redfield wrote:
Sat Jul 04, 2020 1:26 pm
Does this not create the object needed?
no, as I tried to tell you already with e.g. "... requires that you have an image object on the card named "img1"." 8)
redfield wrote:
Sat Jul 04, 2020 1:26 pm
In the long term the goal is a simple Memory game.
I have an old, but well commented example of a memory game on my webpage: https://major-k.de/xtalk.html
Scroll down to "simple_memory1" Yoj may want to change the file suffix from .mc to .livecode if you prefer doubleclicking documents.
redfield wrote:
Sat Jul 04, 2020 1:26 pm
I am assuming I don't need to do an "Import As Control" -> "Image File", to first add the image to the stack, because there is a file path involved in the code (-> put "/home/Documents/Pics/someImage.png") ... :?:
No, when you do "Import as control" LC does this behind the scenes:
1. Creates a new and empty IMAGE object as Richmond showed you in the last post
2. Put the BINARY data of the image INTO that so far EMPTY image object.

So doing this "by hand/script" always requires step 1 before either
-> set the filename of img...
Or
-> put url("binfile:...) into img X

Best

Klaus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”