Opening image and closing it, outer program - python

I'm trying to open and close a picture. Open it at the beginning of the loop and close it at the end of the loop. So far I've only managed to open the image, however for the closing I couldn't find the right options.
For opening I'm using
img = os.startfile("image.jpg")
and for closing I've tried
img.close()
I've also tried
os.close("image.jpg")
How does one do this?

Something in that direction:
import subprocess
import os
import signal
import time
image_viewer = "name of the image viewer program"
pid = subprocess.Popen([image_viewer, "image.jpg"]).pid
time.sleep(5)
os.kill(pid, signal.SIGTERM)
You need to know the name of an application used to view the image.
If it's just about displaying an image, you can also use a GUI toolkit like tkinter.

Related

Is there a way to increase python time between mouse clicks

I know I can use time.sleep(), but I need something that would affect whole script.It is automatic test homework and aplication buttons are clicked almost instantly. It is a bit anoying because I cant see if everything is working as supposed(still learning).
import pyautogui
import time
from pywinauto.application import Application
app = Application(backend="uia").start(r"C:\Users\User\Desktop\WPF_RentACar_3maj\WPFRentACar\bin\Debug\WPFRentACar.exe")
pyautogui.FAILSAFE = True
#app.LoginWIndow.print_control_identifiers()
dlg =app.LoginWindow
dlg.MaximizeButton.click()
dlg.MinimizeButton.click()
dlg.MaximizeButton.click()
dlg.Restore.click()
try:
dlg.Edit1.type_keys("123")
dlg.Edit2.type_keys("123")
dlg.LoginButton.click()
dlg.Button1.click()
finally:
print("Cant login with wrong credentials!")
time.sleep(2)
dlg.Edit1.type_keys("'^a{BACKSPACE}")
dlg.Edit2.type_keys("'^a{BACKSPACE}")
dlg.LoginButton1.click()
time.sleep(5)
time.sleep() does stop the whole script. Or are you using threading or python 2? Also can you tell us what you are also trying to automate.

How to run a python code until the condition is satisfied

Below is my code :
import time
import pyautogui
location = pyautogui.locateOnScreen('ok.png')
pyautogui.click(location)
Now I don't know when the "OK" image appears. So I want to loop the code to search for the image until it is found. Otherwise it is terminating immediately before the image appears.
Once found break the loop and click that image.
How do I do I do that?
And time.sleep() dosent work because the image can appear anytime.
I have done something similar,
Using the autoit module to find the window title and button name
import autoit,time
while True:
try:
autoit.control_click("[TITLE:MTMS Update; CLASS:#32770]", "Button1")
print("Clicked OK")
except:
time.sleep(30)
pass

Import image as plane in blender script

Import images as planes work great in blender GUI, but when I try to use it in python module, I've got this error:
RuntimeError: Operator bpy.ops.mesh.primitive_plane_add.poll() Missing 'window' in context
My code is:
import bpy
import addon_utils
# enable plugins
addon_utils.enable("io_import_images_as_planes")
# remove Cube object
bpy.data.objects['Cube'].select = True
bpy.ops.object.delete()
file = "test.jpg"
bpy.ops.import_image.to_plane(files=[{'name':file}], directory='.')
The import images as planes operator only functions within the 3dview, the current context is the window under the cursor when the script is run which would be the text editor where the script is. It is possible to override the current context, read this answer for more info.
Another option is to put your code into an operator and either run it by searching in the spacebar menu or from a button you add to the sidebar in the 3dview. You can find a template for creating a simple operator in blenders text editor or view it online.

Automatically close an output window from a python script

I have written a little script to batch plot some data. This script cannot run without a window displaying the newly created plot opening. It stops running till the window is manually closed, then the script continues to run. So my question is as follows:
Is there a way automatically close an output window from within a python script?
Here is the script
import pynbody
import numpy as np
import pynbody.plot.sph as sph
f = open('files.txt', 'r')
for line in f:
line = line[0:-1]
s = pynbody.load(line)
sph.image(s.gas,qty="temp",width=16, filename=line[0:-6]+line[-5:len(line)], units='K', cmap="YlOrRd", ret_im=False , approximate_fast=False)
#At the end of the loop I would like to close the output window so the script
#will continue to run
The pynbody.plot.sph package seems as it should be able to turn the output window on and off within the sph.image function call but I have exhausted all the possibilities and am now resorting to this to get the script running.
I found the answer using this link
view and then close the figure automatically in matplotlib?
Essentially, here's the answer:
plt.show(block=False) # block = False allows automatic closing of the image
# the following command will close the graph in 3 seconds
time.sleep(3)
plt.close()
According to the documentation, you should set the noplot argument to True
Add a line pynbody.plot.plt.close() to the for loop.
Definition: pynbody.plot.plt.close(*args) Docstring: Close a figure
window.
close() by itself closes the current figure
close(h) where h is a :class:Figure instance, closes that
figure
close(num) closes figure number num
close(name) where name is a string, closes figure with that
label
close('all') closes all the figure windows

Using Sikuli to take automated screenshots of a window?

Simple question here: I'd like to use Sikuli to take a screenshot of a window on a mac, which would be done by hitting CMD+SHIFT+4 then hitting Space, then clicking a window.
For the CMD+SHIFT+4 I'm having trouble. This doesn't work:
keyDown(KEY_META)
keyDown(Key.SHIFT)
wait(1)
type("4")
wait(1)
keyUp(Key.SHIFT)
keyUp(KEY_META)
Anyone have any ideas? I'm open to other routes of hitting the key combo, for instance, I know to copy this works well:
type("c",KEY_META)
But, it doesn't accept three arguments.
type("4", KeyModifier.CMD+KeyModifier.SHIFT)
Or, even better:
import shutil
import os
screenshotsDir = "absolute-path-to-a-folder"
img = capture(some_region)
shutil.move(img, os.path.join(screenshotsDir, "some-name.png"))
where some_region is:
some_region = SCREEN # for whole screen
or
someRegion = App.focusedWindow() # for the frontmost window
This has the advantage, that you can control the file name of the shot.
Have found a better solution, which actually works:
screen = Screen()
scr_img = screen.capture(screen.getBounds())
scr_img.save("C:\Screenshots", "screenshot")
Screen.capture() returns an instance of ScreenImage class with methods:'save', 'saveInBundle', 'getFile', 'getFilename'. The method save() adds an unique number to a supplied prefix parameter.

Categories

Resources