Check color value of pixel on screen - python

I'm making a bot that automatically clicks if the mouse is not over a specific color. I have everything from positioning to clicking, but I do not know how I can check if the pixel the mouse is over is a specific color. I currently use mouse and keyboard, and google hasn't given me the results fitting for my situation.
It is also not an image, but live on the screen.
It should look something like this:
colorUnderMouse = getColorUnderMouse()
if colorUnderMouse == "FF00FF":
mouse.click('left')
else:
mouse.move(10, 0, absolute=False, duration=0)

You can use pyautogui:
import pyautogui
def getColorUnderMouse():
x, y= pyautogui.position()
pixel = pyautogui.screenshot(
region=(
x, y, 1, 1
)
)
return pixel.getcolors()

Related

I'm trying to make a bot with pyautogui that shoots targets based on their pixel color but the cursor gets stuck once the bot finds that pixel

I'm attempting to scan through screenshots and find a specific pixel color location where the bot must move the cursor to and then click it, however, it seems like as soon as the bot finds the pixel the cursor gets stuck in a loop where it keeps moving a very short distance up and down. The site I'm using for this bot is aimtrainer.io.
import pyautogui
import keyboard
import win32api, win32con
import time
# defining a function that moves the mouse to a specific 'x, y' position and then performs a click
def win_click(x_axis, y_axis):
win32api.SetCursorPos((x_axis, y_axis))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
time.sleep(0.2)
target_rgb = (255, 87, 34) # this is the color that the bot will look for
x_fourth = 1366 / 4
y_fourth = 768 / 4
scr_shot_size = ((x_fourth, 3 * x_fourth), (y_fourth, 3 * y_fourth)) # this is cutting the screen such that it's 1/4 of
# distance from the border on all its sides
last_pixel = None # this is just a placeholder
while not keyboard.is_pressed('q'):
scr_shot = pyautogui.screenshot()
# these 2 for loops will scan the cut screen with a pace of 5 pixels
# such that for each 'x', it will look for its 'y's
for x in range(int(scr_shot_size[0][0]), int(scr_shot_size[0][1]), 5):
for y in range(int(scr_shot_size[1][0]), int(scr_shot_size[1][1]), 5):
curr_pixel_rgb = scr_shot.getpixel((x, y)) # gets the pixel in the current 'x, y' coordinate
if curr_pixel_rgb == target_rgb:
# the below if statements tries to prevent the bot from clicking twice on the same position
if (x, y) != last_pixel:
win_click(x, y)
else:
time.sleep(0.1)
last_pixel = (x, y) # stores the value of the current pixel that will later be compared with the value
# of the current pixel
First I would recommend, you add a Keypress check inside the nested loop, so you don't need to wait for the whole loop to terminate before you can exit.
The problem with the stuck moving cursor is that you jump each 5 pixels.
Let's imagine you have a Rectangle of the size 150x300px with its top left corner positioned at (100,100) and with the correct color.
As you can see, your cursor would repeatedly jump column by column inside of the rectangle.
(I'm sorry for this awful drawing)
You can try to fix this by checking the whole color area and place the cursor centered inside of it once.
Let me know, if you need help with that!

Windows: Draw rectangle on camera preview

Similar questions have been asked a lot for Android, but so far I haven´t been able to find resources related to Windows OS. So basically, as the topic suggests, I would like to draw a rectangle on my camera preview. Some work has been done, but there´s still some problem in my program. Due to some limits, I would like to avoid using opencv as much as possible. Following is my approach:
Open Window´s built-in camera app
Run Python code that draws rectangle on screen, pixel by pixel (see below)
Click on screen with mouse to move rectangle with its upper-left corner
As you can see in the code, I´m not actually drawing on the camera preview but rather drawing on my screen, where the camer preview runs on one layer lower.
Here´s the python code:
import win32gui, win32ui, win32api, win32con
from win32api import GetSystemMetrics
dc = win32gui.GetDC(0)
dcObj = win32ui.CreateDCFromHandle(dc)
hwnd = win32gui.WindowFromPoint((0,0))
monitor = (0, 0, GetSystemMetrics(0), GetSystemMetrics(1))
red = win32api.RGB(255, 0, 0) # Red
past_coordinates = monitor
rec_x = 200 # width of rectangle
rec_y = 100 # height of rectangle
m = (100, 100) # initialize start coordinate
def is_mouse_down():
key_code = win32con.VK_LBUTTON
state = win32api.GetAsyncKeyState(key_code)
return state != 0
while True:
if(is_mouse_down() == True):
m = win32gui.GetCursorPos()
for x in range(rec_x):
win32gui.SetPixel(dc, m[0]+x, m[1], red)
win32gui.SetPixel(dc, m[0]+x, m[1]+rec_y, red)
for y in range(rec_y):
win32gui.SetPixel(dc, m[0], m[1]+y, red)
win32gui.SetPixel(dc, m[0]+rec_x, m[1]+y, red)
As a result, I´m able to draw a red rectangle. However, because the screen is constantly being refreshed, the two horizontal lines of my rectangle (see gif below) are shown as running dots that go from left to right. I can´t find or think of a way to improve this, whilst keeping the possibility to move the rectangle around per ckick.
PS. Ignore white rectangle. It´s a built-in thing of the camera app when you click anywhere on the preview.
Here are the references I used to get to this step:
How to draw an empty rectangle on screen with Python
https://python-forum.io/Thread-How-to-trigger-a-function-by-clicking-the-left-mouse-click

Click on every circle on screen with python

So i'm trying to create a python script that takes a screenshot a few time in a second and locates the orange circles then clicks on them. I'm using for the website https://mouseaccuracy.com to automatically click on every circle very fast. Here is the code i have so far:
import pyautogui
from time import *
color = (235, 133, 0)
while True:
s = pyautogui.screenshot("screenshottest.png",region=(0,160, 1366, 768))
for x in range(s.width):
for y in range(s.height):
if s.getpixel((x, y)) == color:
pyautogui.click(x, y+160)
The problem with this is that it clicks on literally every pixel when i only want to click on every circle instead of every pixel of the circle.
Here is what the circles that it needs to click on look like:
So how do i make it click on every orange circle instead of every orange pixel? thank you in advance

How to move the mouse to any position on the screen, without absolute mouse movements?

I'm trying to move my mouse in games, which only support raw input. How to move the mouse to a known point?
None of the absolute mouse events have worked in game.
The only one that worked for me was this
windll.user32.mouse_event(1, x, y, 0, 0)
This function moves the mouse relatively to the current position of the mouse, but I don't know how to move it to a specific pixel.
Try such workaround then:
MOUSEEVENT_MOVE = 1 # it's better to keep that as variable
def set_mouse_pos(x, y):
current_x, current_y = win32api.GetCursorPos()
windll.user32.mouse_event(MOUSEEVENT_MOVE, x - current_x, y - current_y, 0, 0)

Get the color of a pixel on a pyxel retro game

I am writing a simple shooting game using Pyxel, where an enemy shows up randomly on the screen. I already implemented a strategy to detect if a given shot hit the enemy or not, but a much simpler solution for me would be to check the color of a pixel on the screen under the mouse coordinates. I did not find an API function to do that.
I checked the source code of the Renderer module (https://github.com/kitao/pyxel/blob/master/pyxel/renderer.py) and found a call to the OpenGL lib:
capture_image = gl.glReadPixels(
0, 0, self._width, self._height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
I tried to do the same on my script like so:
capture_image = gl.glReadPixels(
pyxel.mouse_x, pyxel.mouse_y, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
but no matter when I call the gl.glReadPixels(..) I always get the background color.
Is there a way to get the color of a single pixel on the screen on a pyxel script?
Have you tried:
capture_image = gl.glReadPixels(
pyxel.mouse_x,
window_height - pyxel.mouse_y,
1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
Get the color of the pixel at (x, y):
pget(x, y)

Categories

Resources