adding a quit timer that closes a photo - python

After many hours of searching online and in my python book I can't seem to find the answer to my question which is what do I add to my code so I can put in a timer that automatically closes the photo? It pulls itself up but then I have to manually close the photo to get back to my main program. Any help would be appreciated.
from PIL import Image
img = Image.open('battleship load screen.png')
img.show()

This is not possible using PIL alone - img.show() is just launching another program, it's intended for debugging really, not for presenting things to the user.
From the docs.
Displays an image. This method is mainly intended for debugging
purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it.
This method returns None.
If you want to display an image and have control over it, use a graphical toolkit and construct a UI for your purpose. I've linked there to an example using PySide, a set of QT bindings, but of course you could use any toolkit - each will be different.

Related

Open files with python script in Gimp like File->Open

TL;DR: How to open an Image in Gimp with Python like you do with (File->Open) in order to get the Overwrite file.png option and avoid the Save the changes to image 'sample.png' before closing? Dialog.
I wrote a Python script for Gimp which does a few modifications on a selected area, and then saves it as a PNG. After that I want to open the file so the user can check, whether everything is correct or change something. Since the users of the plugin dont necessarily know how to use Gimp, so the "Save the changes to image 'sample.png' before closing?" Dialog might be confusing.
The method I currently use is
pdb.file_png_save(image, drw, new_file, new_file,0,9,0,0,0,0,0)
gimp.Display(pdb.gimp_file_load(new_file, new_file))
This cleans the “dirty” bit – you will not be asked to save the image if you close it afterwards (unless you do further edits):
image.clean_all()
See it referred to here: https://www.gimp.org/docs/python/
Have a look at gimp_image_clean_all. This resets the image's dirty flag and allows it to be closed.
Note that you can't prevent it from being set again if the users change something in the image.

Unable to show an image using python PIL Image.show

I'm using the Python Imaging Library and I am unable to open an image successfully in Windows Live Photo Gallery. There is a message that shows up saying "There are no photos or videos selected" instead of the image.
This is what I've tried:
import Image
img = Image.open(r"C:\Users\User\Pictures\image.jpg")
img.show()
This is pretty much the same as in the PIL handbook tutorial, so I'm not sure where I'm going wrong.
The documentation says:
On Windows, it [show()] saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.
Problem is that your program exits immediately somehow, the temporary file is deleted upon exit and Windows etc. cannot find it. As a temporary solution, try adding:
import time
# Your code as above
time.sleep(30)
This will make the program wait 30 seconds before exiting. If you prefer, you could make it wait the user to press a key.
EDIT: it seems like you are experiencing problems with temporary files. As a workaround, save the image somewhere on the disk using, say, img.save("C:\Users\User\Pictures\test.jpg") and open it with your favorite image viewer. Whenever you want to show the processed image, call save again and reload the picture in the image viewer.

Python PIL-ImageGrab inaccurate when capturing game pixels

I am trying to capture the pixels of a game to script a bot. I have a simple function:
def printPixel():
while True:
flags, hcursor, (x,y) = win32gui.GetCursorInfo()
print x,y,':',ImageGrab.grab().getpixel((x,y))
This prints the current x,y coords and the RGB value of that pixel. This works as expected on my desktop hovering over various icons and such, but the same function does not work in-game. Any thoughts?
edit: When I save the image to a file and perform this same operation on the saved image, it works perfectly in-game. However, it is way slower. I'd like to operate on the image in memory, and not from a file.
Video games often deal with th graphical system directly for performance reasons, so some of the typical windows apis might not work on them. Try and take a screenshot by pressing the print screen button. If that captures your screen than you can take a screenshot in python and check the image you have captured taking into account the cursor position.
To take a screenshot on windows you can check out this answer to the question Fastest way to take a screenshot with python on windows it uses the win32gui library as you are using.

How do I display and close an image with Python?

I would like to display an image with Python and close it after user enters the name of the image in terminal. I use PIL to display image, here is the code:
im = Image.open("image.jpg")
im.show()
My application display this image, but user task is to recognize object on image and write answer in terminal. If answer entered is correct user should get another image. Problem with PIL is that I can't close the image and with research the only solution was to kill the process of image viewer, but this is not really reliable and elegant.
Are there any other libraries for displaying images that have methods like .show() and .close() ?
Just open any image viewer/editor in a separate process and kill it once user has answered your question e.g.
from PIL import Image
import subprocess
p = subprocess.Popen(["display", "/tmp/test.png"])
raw_input("Give a name for image:")
p.kill()
A little late to the party, but (as a disgruntled data scientist who really can't be bothered to learn gui programming for the sake of displaying an image) I can probably speak for several other folks who would like to see an easier solution for this. I figured out a little work around by expanding Anurag's solution:
Make a second python script (let's call it 'imviewer.py'):
from skimage.viewer import ImageViewer
from skimage.io import imread
img = imread('image.png') #path to IMG
view = ImageViewer(img)
view.show()
Then in your main script do as Anurag suggested:
import subprocess
p = subprocess.Popen('python imviewer.py')
#your code
p.kill()
You can make the main script save the image you want to open with 'imviewer.py' temporarily, then overwrite it with the next image etc.
Hope this helps someone with this issue!
Terminal is meant to deal with linear command flow - meaning it asks a question, user answers, and then it can ask a different question. What you are trying to do here is for terminal to do two things, show an image and at the same time ask user a question. To do this you can do two of either things:
Multiprocessing
You can start a new thread/process and make PIL show the image using that thread, and meanwhile in the first thread/process ask a user a question. Then after the user answers, you can close the other thread/process. You can take a look at Python's threading module (link) for more information on how you can do that.
GUI
Instead of making your user interface in terminal, make a simple GUI application using whatever framework you are comfortable. I personally like PyQt4. Qt is very powerful GUI development toolkit and PyQt4 is a wrapper for it. If you make a GUI, then what you are tyring to do is rather trivial.
Not all GUIs are difficult to use.
Here is a single-line solution using PySimpleGUI. Normally I wouldn't write it as a single line, but since it's a one-off, perhaps doesn't need adding to, then it's OK to do.
import PySimpleGUI as sg
sg.Window('My window').Layout([[ sg.Image('PySimpleGUI.png') ]]).Read()
Might be an overkill, but for me the easiest and most robust solution was just to use matplotlib as it properly keeps track of the figures it creates, e.g. :
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
imgplot = plt.imshow(mpimg.imread('animal.png'))
plt.ion()
plt.show()
animal_name = raw_input("What is the name?: ")
plt.close()

Python: OSX Library for fast full screen jpg/png display

Frustrated by lack of a simple ACDSee equivalent for OS X, I'm looking to hack one up for myself. I'm looking for a gui library that accommodates:
Full screen image display
High quality image fit-to-screen (for display)
Low memory usage
Fast display
Reasonable learning curve (the simpler the better)
Looks like there are several choices, so which is the best? Here are some I've run across:
PyOpenGL
PyGame
PyQT
wxpython
I don't have any particular experience with any of these, nor any strong desire to become an expert - I'm looking for the simplest solution.
What do you recommend?
[Update]
For those not familiar with ACDSee, here's what it does that I care about:
Simple list/thubmnail display of images in a directory
Sort by name/size/type
Ability to view images full screen
Single-key delete while viewing full screen
Move to next/previous image while viewing full screen
Ability to select a group of images for:
move to / copy to directory
delete
resize
ACDSee has a bunch of niceties as well, such as remembering directories you've moved images to in the past, remembering your resize settings, displaying the total size of the images you've selected, etc.
I've tried most of the options I could find (including Xee) and none of them quite get there. Please keep in mind that this is a programming/library question, not a criticism of any of the existing tools.
I will recommend using wxPython to create such a viewer, wxPython is easy to learn, free, cross platform and blends well in OSX. Even if you want to use pyopengl, wxPython would be good with pyopengl.
see such tutorials http://showmedo.com/videotutorials/video?name=1790000&fromSeriesID=179
and there is already cornice written in wxpython/PIL, may be you can modify that. It has been inspired by the famous Windows-only ACDSee :)
it's not an answer to your coding question but for (a big part of) the lack of ACDsee equivalent (requires OSX 10.5+):
Simple list/thubmnail display of images in a directory: Finder.app
Sort by name/size/type: Finder.app will do name & type, not image size (but does file size)
Ability to view images full screen: quick preview (spacebar / eye icon)
Single-key delete while viewing full screen: command-backspace while viewing in quickpreview, both windowed and fullscreen
Move to next/previous image while viewing full screen: both quickprewiew (after selecting a group of images or whole directory with cmd-a) and Preview.app
Ability to select a group of images for[...]: Finder.app will does all but resize
seems like you have everything except resize just pressing the spacebar while in finder.
Preview.app will resize both a single image or multiple ones in one batch.
Use an App like Picasa (now available on mac). Use AppleScript through Python to control it from your application.
Failing that, use PyObjC to create Cocoa image display component and dialogs, and so on.
I ended up using PyGame, has been pretty good so far.

Categories

Resources