Python: Using Pyscreenshot image to get RGB values (Linux) - python

I'm on python 3.5 on a Ubuntu machine. I'm writing a script where I want to grab the screen and search for certain pixel colors in the image.
Since it's not windows, PIL.ImageGrab doesn't work and after some research I started using pyscreenshot.
The following works:
import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox(1,1,100,100))
Now my problem is that the type of im is PIL.PngImagePlugin.PngImageFile which doesn't have the method .getpixel like PIL.Image does.
While I could save it to a file and load it again with PIL that seems super ugly and not efficient. How do I make a PIL.Image out of this?
I thought something along the lines of
im = Image.new(ImageGrab.grab(bbox(1,1,100,100)))
but that's obviously not it ;)
(Sidenote: If there are other/easier ways than pyscreenshot to get screenshots on Ubuntu, that's fine too)

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.

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

Python matplotlib saving figures

I am not sure if I could describe this situation clearly, but let's say I have a function imported from a library:
from somelib import somefunc
somefunc(someinputs)
If I run this code on my local machine, a figure would pop up and pause the code until I manually close it. For some reason I cannot run it on my local machine so I had to run it remotely via ssh session. In this case the figure doesn't pop up anymore, I wanted to find a way to save this figure as png file and so I could download it from my remote machine to see it. I cannot modify the source files of this library. Anyone could give me some suggestions? Thanks you!
You can use multiple ways depending upon your image..
from PIL import Image
import PIL
# creating a image object (main image)
im1 = Image.open(r"C:\Users\System-Pc\Desktop\flower1.jpg")
# save a image using extension
im1 = im1.save("new.jpg")
using matplotlib
from matplotlib import pyplot as plt
plt.savefig('new.png')

Converting a PIL Image to an autopy bitmap?(and back)

Recently I started working with PIL together with AutoPy to automate some tasks. One of the core functions AutoPy is needed for is its suprisingly fast (though buggy, but there are fixes for that - the build and installation from source can be annoying, and if you get the error Unable to find vcvarsall.bat, just google it).
But just now I ran into a problem I can't find a fix for. PIL and AutoPy use fundamentally diffrent formats for their screenshots. For AutoPy I couldnt get it to work with multiple screens, but for PIL i easily could. Now I need to convert these Images into a format AutoPy accepts - and that in a reasonable amount of time.
A theoratically possible solution:
def test():
Img = Image.open("example.png")
Img.save("test.png")
Img2.open("test.png")
Going by my testing, this takes roughly around 0.5 seconds to complete. Far more than the 0.1 seconds I'd need.
I dug around the documentation to find a bitmap image conversion tool.
pil_im = Image.open("test.png")
pil_im = pil_im.convert(mode ="1")
#this is to prevent a value error in the tobitmap function
pil_im = pil_im.tobitmap(im)
I had a similar problem earlier where a pyautogui screenshot was not considered the same as a recently opened png. pil_im.copy() created an image to compare with without all the formatting.

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