tkinter.filedialog.askopenfilename() function: How can I use it? - python

So currently I'm reading images into my Python project with the cv2.imread() function, but it would be much cooler if I could go to my user interface, which I created with the tkinter module, and choose my picture from my Windows Explorer.
I found this function in Internet. If I use this my Windows explorer opens and I can select my picture, but nothing happens afterwards. I want save the picture in img but it doesnt work. Do you know what the problem is?
path = tkinter.filedialog.askopenfilename(title='select', filetypes='.png')
img = cv2.imread(path, 1)

Related

Python, open a photo WIndows Live Photo Gallery and at the end close it

I'm trying to open an image with WIndows Live Photo Gallery.
The image has path:
C:\Users\User\Desktop\Image.jpg
I want to open it at the beginning of the loop and then close it a the end of the loop.
I've successfully managed to open it with:
img = os.startfile("image.jpg")
However I can't seem to find any way to close the image at the end of the loop.
Also if any of you know any better way to call and then close an image (it doesn't need to be with Windows Live Photo Viewer).
you better install Python Pillow module
from PIL import Image
im = Image.open(r"C:\Users\System-Pc\Desktop\bla.jpg")
im.show()
when done use im.close()

Pygame application not opening a png

For some reason, my python program will not open my file in a directory called imgs in which I have a png file that I wish it to display when it runs.
The imgs directory is in the same location as my python program on my C drive. I have made sure that I am entering the correct names and that there is no / before imgs
It always pops up with the message
pygame.error: Couldn't open imgs/tileGrass1
tileGrass1 being the png
Any suggestions on how to fix this??
Few checks to ensure a pygame program is running well
Ensure You Initialize the pygame library at the beginning of your code
pygame.init()
Ensure Path to your image is correct and that the image exists. Check that the extension is correct (png, jpg, jpeg, etc)
It would also be useful if you share the code to your program so that the exact error is easily noted

How to add images to Pythonista's ui designer

I have been fooling around with python and Pythonista 2.5 on iOS. I currently am far too inexperienced to create good UIs in scripts and need some help using the designer in Pythonista. I currently wish to add an image asset, yet I am only able to use stock images provided, is there any folder path I can follow to add my images to that list, or is there another simple way of doing it?
Keep in mind I have little experience and thank you for any help!
You can store the images files in any folder. A simple way to copy external images would be to first copy it in clipboard (may be from photos) and then save it as png file by running the following script.
(Custom images are currently not supported in the UI editor, You have to load them via code.
https://forum.omz-software.com/topic/3668/images-in-ui-designer
But you can use the [+] button in the code editor (at the top) to view bundled images/textures. (images in the current directory are also shown.)
https://forum.omz-software.com/topic/3760/itunes-file-sharing )
import clipboard
image = clipboard.get_image()
image.show()
image.save('img1.png')
You can also use dropbox or appex scripts to store images.

Using PIL (Python Image Library) to detect image on screen

I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.
I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.
The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.
What I need is the code method I would use to search for the image on screen and send back the cords to a variable.
Image explanation/example:
Reference image of rifle:
PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html
Edit:
I assume you are using windows, as your example image looks like window. In this case you can use:
from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)
then use opencv to process the image to find sub image you are looking for.
If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532
Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you.
You have to just go to this webpage and read the locate function topic completely
and you'll be able to solve your problem.
I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.

OPEN IMAGE USING PYTHON showing error

I am trying to open an image using python; I wrote the following code :
from PIL import Image
im=Image.open("IMG_1930.jpg")
im.show()
But the windows photo viewer opens but it shows the following message instead of the photos:
"windows photo viewer can not open this picture because either the picture is deleted , or it isn't in a location that is accessible."
The show method in PIL is a poor's man way of viewing an image - it has got a hardcoded image viewer application, and writes your image data to a temporary file before calling that as an external application.
What is happening there is that you are either having problems with Windows' uneven access rights policies, and the viewer can't open the file in Python's temporary directory, or there is a problem with Window's problematic path specifications - it might even be a bug in PIL, that renders the temporary paht generated by PIL unusable by the image viewer.
If you are using show in a windowing application, use your tookit's way of viewing images to display it instead - otherwise, if it is a simpler application, build up a Tkitner Window and put the image in it, instead of show.
import sys
import Tkinter
from PIL import Image, ImageTk
window = Tkinter.Tk()
img = Image.open("bla.png")
img.load()
photoimg = ImageTk.PhotoImage(img)
container = Tkinter.Label(window, image=photoimg)
container.pack()
Tkinter.mainloop()
(Linux users: some distributions require the separate install of Tkinter support for PIL/PILLOW. In Fedora, for example, one has to install the python-pillow-tk package )
I also had problems with this. Take a look at this post it fixed my problem: PIL image show() doesn't work on windows 7
Good luck fixing it.

Categories