getting the color of pixels on the screen in Python3.x - colors

I want to get the color of pixels on the screen in Python3.x. (example x,y)
I work on windows. and using tkinter.
But, It can't use PIL in Python 3.x.
How can i do this.
Thank you.

You can use PIL on Python3.x; there is a binary installer for Windows. The code might not work as is both on Python2.x and Python3.x:
try: import Image # Python 2.x
except ImportError:
from PIL import Image # Python 3.x
You could capture an area of the screen using an analog of grab() method.

Related

Extract small regions from a PIL image python

enter image description here
I have a PIL image in python as shown and I want to extract each of the small red regions separately into a jpeg format. So from this file, I'm expecting file1.jpg, file2.jpg, etc.
How can I obtain each sub-regions in each different file?
PIL doesn't really excel at that. I would consider using:
OpenCV findContours(), or
scikit-image regionprops(), or
ImageMagick Connected Components, or
Python wand bindings to ImageMagick as above

Matplotlib & LaTeX

I use MikTeX and try to obtain LaTeX fonts in my matplotlib plots.
However, using the demo code, Jyputer Notebook says that there is no latex,
Failed to process string with tex because latex could not be found
I try to add into PATH the path to latex.exe, dvipng.exe and ghostscript. Unfortunately, it still does not work. What I do wrong?
If I evaluate the following
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.sin(np.arange(0, 10, 0.1)),label=r"$\mathcal{M}=2$")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
it returns me the next picture,
So, I see that \mathcal{} command works perfectly, whereas the fonts are not "latex".
You need to add plt.rc('text', usetex=True).

How to change Python background to a certain colour with colorama?

I am using colorama to change the colour of my text and it works fines, but now I want to change the colour of the whole background instead of just my text.
I am using Windows:
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Back.GREEN)
print(Fore.RED)
But, that code only makes the text coloured. Is there a way to do that in python? I want it like CMD where you can have the background of it a colour. I cannot use the OS module as I do not have admin rights, but I'm open to using any other module.
How do I solve this problem?
After a while in playing with it i figured it out. Just forgot about this post. Here is what I did.
import colorama
from colorama import Back as bg
colorama.init()
print(bg.RED)
print(colorama.ansi.clear_screen())
I think clearing the screen fixed the Issue
After playing with colorama on my Windows 10 box it seems it's only used to change the text but not the console/terminal background. I was however able to change the background using this standard library solution:
import ctypes
try:
ctypes.windll.msvcrt.system(b"color 4f")
print("Hello World!")
except ValueError:
pass
The terminal background will change to red with white text.

Imagemagick's import is frustratingly slow

I am writing an small python script to take screenshots of a game window (which will be in the background/minimized) and performing some simple template matching and ocr using cv2.
I am currently calling im's import as follows:
import -window windowID png:-
to take a screenshot of an inactive window.
However this takes almost 4s and is easily the slowest part of my script by a factor of 100x.
Is there any alternative to import or perhaps another way of approaching this that will be faster?
I have already tried graphicsmagick (ended up being slower than imagemagick) and xwd (did not capture the unfocused window even though the windowID was specified)
Link to full python script (Line 44 is where the screenshot taking happens)
You are doing all the PNG encoding and zlib compression in ImageMagick and then decompressing it all again in OpenCV. I guess you would do better if you found a format that is more closely shared between the two.
Specifically, ImageMagick could give you RGB pixels directly, which you could then convert to BGR very easily in OpenCV with cvtColor().
import -window windowID rgb:
You would have to query the window dimensions to get width and height.
Alternatively, you could use PPM format which OpenCV can also read without any libraries and which includes dimensions:
import -window windowID ppm:

How to show image from webcam

I never needed to work with camera before, so I do not know where to start.
I need to display the image of a real-time camera, to capture and save to a file.
I'm using python3 and gtk3.
Does gtk has any feature for this?
The easiest way to get the image from a webcam is to use OpenCV. It allows you to get the image with just 2 lines of code and 2 more to show it, like so:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('frame',frame)
cv2.waitKey(0)
But there is a downside, namely that OpenCV for Python 3 has to be build from source. Most people don't like building from source so they say it is not available.
Luckily there is the Unofficial Windows Binaries for Python Extension Packages by Gohlke (University of California) which also offers a precompiled version of OpenCV for Python 3. Installing that using pip should be easy.

Resources