Using random.choice with pictures

Asked by Romain Claret

Hi,

I was wondering if their was a way to use this command for pictures instead of a letter?

I had the idea to use eventually something like that:

#import random

a = (picture)
b = (anotherPicture)
c = (otherPicture)

d = random.choice('abc')
click(d)

But it was with no success...
It's only returning the value a, b, or c if I popup it..

Thanks if you have an idea.

Romain

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

you have to make a list (if it is dynamically appended) or a tuple (if it is fixed) of these picture filenames (for Python-newbees: these are mutable and immutable arrays)

--- e.g. tuple

import random

myImgs = (
  "img1.png",
  "img2.png",
  "img3.png",
  "img4.png",
  "img5.png",
  "img6.png",
  "img7.png",
  )

d = random.choice(myImgs)
click(d)

--- e.g. list

import random
import os

allFiles = os.listdir(getBundlePath())
myImgs = [] # empty list
for f in allFiles:
   if not '.png' == os.path.splitext(f)[1]: continue
   myImgs.append(f)

# myImgs should contain at least one entry!
d = random.choice(myImgs)
click(d)

Can you help with this problem?

Provide an answer of your own, or ask Romain Claret for more information if necessary.

To post a message you must log in.