store captured image as variable into a py file

Asked by Ben Dix on 2021-05-18

Hey Folks,

capture() works fine with the "_" in front of the name. I've seen in another post this happens to be sure, it will not bei deleted in further progress.

I am trying to achieve a few captures by executing a sikuli script and I want the captured images of every region be stored as individual variables at the end of another existing python file...

Still searching for the way to achieve this and also remove the "_" of the png name.
Even if this is a general python case, I would appreciate your help, maybe you have an idea.

thx for reading!

Question information

English Edit question
SikuliX Edit question
No assignee Edit question
Solved by:
Ben Dix
Last query:
Last reply:
Revision history for this message
Ben Dix (bendix80) said :

yeah, I got it how to rename the file 😄


import os

a = "name"

os.rename(r'/Users/MYNAME/Desktop/Sikulix/scripts/AAANewScripts/screen_capture_test.sikuli/_' + str(a) + '.png',r'/Users/MYNAME/Desktop/Sikulix/scripts/AAANewScripts/screen_capture_test.sikuli/'+ str(a) + '.png')

first I failed using shortcuts, but now I think I get it 😄

here_it_is = (getBundlePath() + '/')
os.rename(r'' + str(here_it_is) + '_' + str(a) + '.png',r'' + str(here_it_is) + str(a) + '.png')

so looks like this question is solved, still looking for the way to write the image with its name as variable at the end of an existing py file 😄

Revision history for this message
RaiMan (raimund-hocke) said :

Uuuups, why so complicated:

img = "_someName.png"
capture(someRegion, img)

and you have your variable to do with it, whatever your want:

imgFilename = os.path.join(getBundlePath(), img)

a statement:
line = 'variable = "%s"' % imgFilename

... and now you could append this line to an file using the normal File-I/O

in turn you now can import this using
from import *

and you have your variable names available (even with absolute filenames, so no imagepath needed)

... now the _ takes care, that your images are never deleted.

Revision history for this message
Ben Dix (bendix80) said :

Thx RainMan 👍😉