How to add a image into PyCharm on Mac - python-3.x

I am trying to use tkinter to make a GUI and everything works for me except getting an image to import. The error is this:
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "MainDisk/Users/Matt/PythonProjects/Car_image.jpg": no such file or directory
My line of code:
photo = tk.PhotoImage(file="MainDisk/Users/Matt/PythonProjects/Car_image.jpg")
Is this the way that you would import an image on Mac? Sorry I am very confused and just am not sure how to get this to work. The image is in the right place but I have to be doing something wrong clearly. Me and someone else had this working on a windows computer but can't get it to work on Mac.

You can try :
photo = tk.PhotoImage(r"MainDisk/Users/Matt/PythonProjects/Car_image.jpg")

Related

Relative path code is giving me a different error for auto-py-to-exe

I am trying to convert my .py to .exe using auto-py-to-exe. I have two logos that I want on my file, and the first edition of the app worked with no issues, and the onefile option worked great. However, I have made updates to the app over the past month, and when I go to use onefile again, the program couldn't locate my logos. So, I pasted the relative path code below. Now I get an error which I have screenshotted. I have not the slightest clue how to fix this. If someone could give me some direction, that would be fantastic. Thank you!
IMAGE:
https://www.dropbox.com/s/zby2e8s45b9ovu6/Stack.PNG?dl=0
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp and stores path in MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")```

Tkvideo is working on one file but not an exact copy

Alright, guys, I'm in need of some serious help here. I am running python3 on a mac, using a virtual environment in vs code. So to the point, I have a python project where I am opening a window and playing a video on loop here is the code
from tkinter import *
import numpy
from tkvideo import tkvideo
from PIL import Image, ImageTk
import os
root = Tk()
my_label = Label(root)
root.geometry("500x500")
root.configure(bg='black')
root.resizable(False, False)
my_label.pack()
my_label.config(borderwidth=0)
player = tkvideo("path/test.mp4", my_label, loop = 1, size = (600,338))
# root.iconbitmap('path/Logo.png')
root.title('Elvis')
ico = Image.open('Logo.png')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
player.play()
root.mainloop()
it works fine and runs with no issues, the problem comes in at my other python project.
I have everything the exact same but with different file locations.It does run but the video doesn't play, the UI looks different, and the logo is replaced with a standard python logo. If you were wondering yes, I do have all the same packages installed and have even tried uninstalling and reinstalling the packages. anyways this is the error RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable. i am new to python but i have a pretty good grasp on how it works. I just don't see how it would work on one but not on another identical one with the same packages. If anyone more advanced than me could help it would be greatly appreciated. I am sorry if there is a super obvious answer to this. I have worked for a good 4 hours and can't seem to figure it out. anyways ---thank you

Convert images to Icons is giving errors

I'm converting images to icons using this code:
import PIL.image
img = PIL.Image.open ("imagepath.png")
img.save ("iconpath.ico")
This is giving me an icon file as desired, but when I try to open it an error pops up:
Paint:
Microsoft photos error:
When I try to open other icons with the same programs they work perfectly, but it doesn't with the ones I made. Does anyone know any other way or library for doing this?
Try this:
img.save('iconpath.ico',format = 'ICO', sizes=[(32,32)])
You can change size to 16,16
First time I was converting image with PIL I've used this tutorial:
Tutorial
Everything worked fine.
The image that is being converted has to have a 1:1 proportion, if not, when trying to open the generated icon it will cause errors.

Tkinter and some kind of issue when trying to display images

First off, I'm very new to Python and coding in general. I'm using Python, Tkinter, and Idle version 3.7.3. I'm using an HP Chromebook, with Chrome OS Version 81.0.4044.141.
from tkinter import *
window = Tk()
window.title('Image Example')
img = PhotoImage(file = 'python.gif')
label = Label(window, image = img)
label.pack()
window.mainloop()
As you can see above, this is the small snippet of code that I'm having issues with. As far as I understand, everything is written correctly and the file "python.gif" is in the correct directory. For reference this is what the image should look like:
python.gif (normal)
But when I run the program, this is what I get:
python.gif (screenshot of running program)
That's the result 99% of the time, but I should mention that there have been a RARE number of occasions where the image displayed correctly upon program execution. However, I do not know how to replicate that. Also for more context, I've tried other images to see what happened. I found a free .pgm image to try as an example, and upon execution either I got the same result, or half of the image would appear correctly while the bottom half (also sometimes this would be reversed and the top half would be affected) would be "blacked out".
In conclusion, I wanted to ask if anybody has an idea of what's going on. I'm not sure if this is a hardware issue (because I can view all mentioned images in a normal image viewing app with no problems), or if this has something to do with Python/Tkinter.
Any assistance is very appreciated! Please and Thank You!

How to fix Python 3 PyAutoGUI screenshot error? (macOS)

I keep getting an error with any of PyAutoGUI's screenshot taking functions such as:
pyautogui.locateOnScreen('button.png')
pyautogui.pixelMatchesColor(x, y, (r, g, b))
im = pyautogui.screenshot()
The error I get is:
screencapture: cannot write file to intended destination, .screenshot2018-1009_16-43-26-003190.png
Traceback (most recent call last):
File "~/program.py", line 111, in <module>
pyautogui.locateOnScreen('/images/play!.png')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 265, in locateOnScreen
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 331, in _screenshot_osx
im = Image.open(tmpFilename)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '.screenshot2018-1009_16-43-26-003190.png'
I don't tell it to or want it to save the new screenshotted image to any directory (and it shouldn't). With the pyautogui.screenshot() function I could manually save it to a real directory in my project, but I don't have an option to do that with the other methods. Any idea on how to fix this?
What I've tried:
I looked at all the documentation I could find online of pyautogui screenshots
Restarting computer
Downgrading versions for Pillow and pyscreeze
EDIT:
I tried it on another mac and got the same error.
Tried it on windows bootcamp (windows on my mac) and it works fine.
possible, very hack-ish fix - I don't actually like this answer but it was a quick and easy fix (done on OSX with Mojave):
PLEASE NOTE: modifying the source code of libraries you don't understand is usually a bad idea, so do so at your own risk! This worked for me, your milage may vary.
Go to your file (your file path may be different, I just copied this from your error):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py
find the line under the function "_screenshot_osx" that looks like
tmpFilename = '.screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
copy it and then comment it out, paste the copied line directly below the commented out original and modify to something like this:
tmpFilename = r'<your preferred screenshot folder here>/screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
save the changes, and see if it works.
Also note: pyautogui.locateOnScreen can be a bit finicky so even if this removes your error you still might not get the coordinates you want (might return none). That might be related to a different issue. To test that part I do this:
import pyautogui
pyautogui.screenshot('testFull.png')
placePos = pyautogui.locateOnScreen('testFull.png')
print(placePos)
even the cursor blinking can mess this up though, and osx has translucent user interfaces so it's kind of annoying to test this perfectly without careful image curation.
I was facing this same issue on MacOS Mojave after changing to Python 3.8.
Here is my solution.
Go the same file mentioned by #Richard W.
There, together with all your 'imports', add the following line so the script can find the tmpFilename folder
dirname = os.path.dirname(__file__)
then, replace the also mentioned line by
tmpFilename = os.path.join(dirname,r'screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f')))

Resources