How to use Mac finder import image with Python executable - python

I already finished my project source code. The code is basically about analysis an image. However, I am currently using cv2.imread() to import the image to source code.
I am expecting that user can choose image from Mac finder after I convert the source code to executable.
Basically what I want is:
When user open the executable, Mac finder will show up and user can choose image from it.
I really don't know what to do to achieve this.

You can use tkinter and file dialog to open a finder menu as follows:
from tkinter import filedialog, Tk
root = Tk()
root.withdraw()
path = filedialog.askopenfilename()

Related

Import and Display Image as a Screen in Python Tkinter

I am using Visual Studio Code, Python, and Tkinter in this program and I want to import and display an image from my computer as a screen(it is in the end). I tried to import the image by copying a statement from a video example I However, when I run the program, it says
tkinter.TclError: bitmap "IMAGE.jpg" not defined
import tkinter
from tkinter import Tk
from PIL import ImageTk, Image
screen = Tk()
screen.iconbitmap("IMAGE.jpg")
As far as I know, .jpg is not supported. Try converting it to .ico format. There are online tools, just upload your .jpg and download .ico.
I found a new solution. see this -
instead of screen.iconbitmap("IMAGE.jpg")
use
root.iconphoto(False, PhotoImage(file='IMAGE.png'))
NOTE
Supports png only
Import PhotoImage from tkinter package
For more detail see my github repo - here

Adding Downloaded Fonts To Tkinter

I want to download an open source font and use it in my Python Tkinter Program.
How can I tell Tkinter to import a font from a directory or by putting the font in the same folder as the program ?
Note:
I have searched for an answer for a while even reading API reference of Tkinter about every font related thing I could find. If there was an obvious answer to this question and I didn't know because maybe I didn't search hard enough or asked the wrong questions I am sorry.
This worked for me on Windows (and I would guess on any platform which supports the pyglet module)
import tkinter as tk
import pyglet, os
pyglet.font.add_file('myFont.ttf') # Your TTF file name here
root = tk.Tk()
MyLabel = tk.Label(root,text="test",font=('myFont',25))
MyLabel.pack()
root.mainloop()

How to ask user to input FOLDER PATH in Tkinter Python 2.7

I would like to know how to allow user to key in a folder path, so that when user key in the folder path, it will load all the text file in the folder. However, I would like to do this is Tkinter.
I understand that by using Python IDLE, the code is raw_input. I did some search about using it on tkinter, it stated that use something like ask_directory.
I tried to google ask_directory, however, I couldn't find any source of help on this.
Could anyone help me out regarding this?
On how to ask user to key in path folder and loop through every files in the folder.
To do this
from Tkinter.filedialog import askdirectory
Then to get it go path=askdirectory()
The variable path now has the directory in it.

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.

What could cause an open file dialog window in Tkinter/Python to be really slow to close after the user selects a file?

I can do the following in my program to get a simple open file dialog and print the selected file path. Unfortunately it doesn't go away right away when the user selects the file, and stays around for over 5 minutes. How do I make the window disappear immediately once a selection has been made before executing more python code? After the Tkinter code I do try to import some video using OpenCV which I think may be causing the slowing. My OpenCV code does execute properly and I don't think there is a problem with that alone (i.e. some interaction is causing the error & maybe some intensive process is started before Tkinter wraps up its GUI dialog).
import Tkinter as Tk
import cv2
from tkFileDialog import askopenfilename
root = Tk.Tk()
root.withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
cap = cv2.VideoCapture('video.mp4') # this works just fine
I am using Python 2.7 and Mac OS X 10.9 if that is useful.
[EDIT: This does not seem to be a problem for all, but it is for me, so I am changing the question to also include debugging the problem. I don't want anything to execute until the Tkinter open file dialog window is done closing in the GUI. It seems that a subsequent step in my program (an open cv video import) could somehow be causing Tkinter to slow things down, so I want to ensure it does close before any new process is started. Again, the Tkinter window does actually close after 5 minutes...]
I was having some trouble with Tkinter dialog boxes. Like you, the dialog just sat there after I had selected a file. I didn't try leaving it for very long, it may have disappeared after 5 minutes as you said your's did. After lots of random experimentation I found that calling root.update() before the askopenfilename() line seemed to fix it.
For reference, this is the code I was testing with:
import sys
from tkinter import *
from tkinter import filedialog
#instantiate a Tk window
root = Tk()
#set the title of the window
root.title('Tk test')
# I don't know, what this does, but it fixes askopenfilename if I use it.
root.update()
print(filedialog.askopenfilename(title='dialogue? surely.'))
Exactly the problem I had - sometimes the file dialog would go away after a while, sometimes not. But it always seemed to block a later status windows. Adding the root.update() fixed both problems immediately.

Categories