File Pool display images
Hi again!
I was trying to select images from the File Pool and display them on a canvas in a loop. I want a second canvas following the first one, with some text on it. This is a part of the code I have in the prepare phase (not the most sophisticated one!)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import random
list = [ ]
curve = ["shape2.jpg", "shape3.jpg","shape7.jpg","shape8.jpg", "shape9.jpg","shape11.jpg","shape12.jpg", "shape13.jpg", "shape14.jpg" ]
list.append(curve)
close = ["shape2.jpg", "shape3.jpg", "shape6.jpg", "shape7.jpg", "shape8.jpg", "shape9.jpg", "shape11.jpg", "shape12.jpg", "shape14.jpg"]
list.extend(close)
diag = ["shape1.02.jpg", "shape3.jpg", "shape5.jpg", "shape6.jpg", "shape13.jpg"]
list.extend(diag)
sym = ["shape2.jpg", "shape3.jpg", "shape4.jpg", "shape5.jpg", "shape6.jpg", "shape7.jpg", "shape10.jpg", "shape14.jpg"]
list.extend(sym)
text = ['curve?', 'sym?', 'diag?', 'sym']
for sym in list:
list_sym = random.choice(list)
chosen_text = random.choice(text)
sym_cnvs = self.offline_canvas()
xc = sym_cnvs.xcenter()
yc = sym_cnvs.ycenter()
text_cnvs = self.offline_canvas()
cx = sym_cnvs.xcenter()
cy = sym_cnvs.ycenter()
symbol = exp.get_file(list_sym)
sym_cnvs.image(symbol, xc, yc)
text_cnvs.text(chosen_text, cx, cy)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
When I try to run this part, I get an error as follows:
IOError: The picture file '['shape2.jpg', 'shape3.jpg', 'shape7.jpg', 'shape8.jpg', 'shape9.jpg', 'shape11.jpg', 'shape12.jpg', 'shape13.jpg', 'shape14.jpg']' does not exist.
Instead, when I try to run only the following code, in an attempt to debug:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import random
list = []
curve = ["shape2.jpg", "shape3.jpg","shape7.jpg","shape8.jpg", "shape9.jpg","shape11.jpg","shape12.jpg", "shape13.jpg", "shape14.jpg" ]
list.append(curve)
close = ["shape2.jpg", "shape3.jpg", "shape6.jpg", "shape7.jpg", "shape8.jpg", "shape9.jpg", "shape11.jpg", "shape12.jpg", "shape14.jpg"]
list.extend(close)
diag = ["shape1.02.jpg", "shape3.jpg", "shape5.jpg", "shape6.jpg", "shape13.jpg"]
list.extend(diag)
sym = ["shape2.jpg", "shape3.jpg", "shape4.jpg", "shape5.jpg", "shape6.jpg", "shape7.jpg", "shape10.jpg", "shape14.jpg"]
list.extend(sym)
print list
text = ['curve?', 'sym?', 'diag?', 'sym']
list_sym = random.choice(list)
print list_sym
chosen_text = random.choice(text)
print chosen_text
sym_cnvs = self.offline_canvas()
symbol = exp.get_file(list_sym)
sym_cnvs.image(symbol)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
It prints out the values in list, list_sym and chosen_test as expected but I still get an error as follows: AttributeError: 'experiment' object has no attribute 'sym_cnvs'
Can someone please pin point where I'm going wrong? Could it be something in my code or the fact that it isn't able to get the path for images in the file pool?
Many thanks in advance!
Regards,
Anupama
Comments
HI Anupama,
Be aware of the difference between appending to a list and extending a list (see here)
So, when you do this:
your list would contain following: `[["shape2.jpg", "..." ,"shape13.jpg"],"shape2.jpg", "...", "shape6.jpg"]. So first, another list, and then three more items. See what I mean?
Therefore, you first error occurs, because you want to pass a list to
exp.get_file()
, which expects a string. I suppose the reason your debug attempt threw another error, is simply chance because you are randomly picking an item from the list, which in this case happened to be a proper string.Does this make sense?
Eduard
Hi Eduard,
Thanks for the feedback! I do notice what you mean by the difference between 'append' and 'extend'. I converted all the 'appends' to 'extends' and now I have one unified list with all the images.
When I randomly pick out images from the list, it is now always a string. However, when I try to create a canvas (inside the loop) to display this image, it still throws an error like "AttributeError: 'experiment' object has no attribute 'sym_cnvs' What could be the reason for this?
Thanks again,
Anupama
Which OpenSesame version do you use? Could you post the entire code you're using right now(e.g. uploading it on pastebin.com) or even the entire experiment?
Thanks,
Eduard
Hi Eduard,
Thank you for your prompt reply! I'm using OS 2.9.7.
The link to my experiment is http://pastebin.com/sjqKuZNK
Hi Eduard,
Do you know why it won't work yet?
Thanks,
Anupama