how to save a object to a file

Asked by jack.cai

find("image")

I find a picture in web page, then i want to save this pic to a file, but it fail

p=find("Vefifjf.png")
f=file("E:\\WorkPlc\\a.jpg","wb")
f.write(p)
f.close()

TypeError: argument 1 must be string or buffer, not org.sikuli.script.Match

Question information

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

--1. p is a Match object, that only knows, where on the screen the picture Vefifjf.png was found.

--2. Why do you want to save a picture, that you already have as file: <script folder>/Vefifjf.png
the file's path is:
import os # put it at the beginning of the script

pathImg = os.path.join(getBundlePath(), "Vefifjf.png")

--3. you might copy the picture to some other place using:
import shutil # put it at the beginning of the script

img = "Vefifjf.png"
p=find(img)
shutil.copy(img, "E:\\WorkPlc\\a.png") # switching to .jpg not possible this way

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

Sorry, again:

--3. you might copy the picture to some other place using:
import shutil # put it at the beginning of the script
import os # put it at the beginning of the script

img = "Vefifjf.png"
p=find(img)
shutil.copy(os.path.join(getBundlePath(), img), "E:\\WorkPlc\\a.png") # switching to .jpg not possible this way

Revision history for this message
jack.cai (chy710) said :
#3

Thank you RaiMan. I understand your mean.

in fact, above img is a input text, have a Verification Code on img's right

so my code as below:
p=find("Vefifjf.png")
# region of verification code
p.right(50)

you know the verification code will change each time, i want save this code(pic) to a file, then to another program to handle(discern).
do you have better way.

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

this should do it:

mport shutil # put it at the beginning of the script
import os # put it at the beginning of the script

img = "Vefifjf.png"
p=find(img)
code = capture(p.right(50)) # creates a temp image file of the region
shutil.move(img, "E:\\WorkPlc\\a.png") # moves it to the permanent storage

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

Sorry, copy and paste typos.

import shutil # put it at the beginning of the script

shutil.move(code, "E:\\WorkPlc\\a.png") # moves it to the permanent storage

Revision history for this message
jack.cai (chy710) said :
#6

RaiMan, thank you very much.

Revision history for this message
jack.cai (chy710) said :
#7

Thanks RaiMan, that solved my question.