OPEN IMAGE USING PYTHON showing error - python

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.

Related

Use python to grab the screen(screenshot) But image is blurry, unsharp

Environment:
win10 PC notbook
python 3.9
PyQt5==5.15.4 pywin32==304 opencv-python==4.5.5.64 Pillow==9.1.0
Question:
I am trying to grab my whole screen by python. The function is easy, but I get a strange question.
The screenshot image is always blurry and unsharp.
screenshot by windows10 self: windows10 self (PNG)
screenshot by my codes: my codes (PNG)
My Codes as follows:
import cv2
import os
from PIL import ImageGrab
import numpy as np
if os.path.exists('xx.jpg'):
os.remove('xx.jpg')
captureImage = ImageGrab.grab()
width,height = captureImage.size
img = cv2.cvtColor(np.asarray(captureImage), cv2.COLOR_RGB2BGR)
cv2.imwrite('./xx.jpg',img)
cv2.waitKey(0)
I have try alternative method to achieve screenshot function, just like pyqt5/mss/pywin32/pyautogui, but every method leads to the same question.
And I google lots of articles, I found that my screen has scaled to 125%(as recommended), as follow:
PNG:
So I revise it to 100%, the screenshot is sharp. But 125% is normal and comfortable vision.
Then I doubt if it's DPI. But I try these codes, it does not work.
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
I am so crazy and curious that how those screenshot software resolve it. After all, we can not ensure every user has the 100% scale and has normal dpi.
I will be grateful if u could give me solutions or advices.
Solution:
Thanks to Christoph Rackwitz.
I had grab a PNG screen and update it and the two PNG image still same. But it's really different in my PC before upload.
I test a lot and I found that I open my codes screenshot by iQIYI Player. But I open windows10 self screenshot by windows Player.
It's the Reason!!!My codes is right and everything is right except iQIYI Player Software. Maybe it's the software bug.
Thanks again.

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

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

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)

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()

Tkinter Background Image not showing up

I am trying to create a weather App in Tkinter GUI. It is working fine. I want to add a background image to it but it wont show up. initially i did it without the reference line but it didnt work. On some website they said to keep reference of the image but that too didnt work.
Also my Tkinter window size is 1920x1080 and the image was of same dimension but it still doesnt show up. I tried to reduce image size than window but still not working. Any suggestions accepted. Also there is no error.
bg = PhotoImage('clearsky2.jpg')
bgl = Label(gui,image=bg)
bgl.image = bg #given a reference
bgl.place(x=0, y=0, relwidth=1,relheight=1)
bgl.pack()
Its sad that tkinter.PhotoImage does not support JPEG files, but it does support PNG in the newer version and has proper support for GIF too. To use JPEG you need PIL installed.
In the terminal say:
pip install Pillow
After that import it like:
from PIL import Image,ImageTk
Then now, to open the image with PIL, say:
img_file = Image.open('clearsky2.jpg')
bg = ImageTk.PhotoImage(img_file)
bgl = Label(gui,image=bg)
bgl.place(x=0, y=0, relwidth=1,relheight=1)
This will work with JPEG and PNG files as well and, keep in mind you dont need to keep a reference unless your looping over images or creating image inside of a function.
Hope it cleared your problem, do let me know if any more errors
Cheers

Categories