SoFunction
Updated on 2024-11-19

python implementation of the keysmith to find the color click function tutorial, using pywin32 and Pillow library

Python image processing module PIL (pillow)

The main role of pywin32

1. Capture window;

2. Simulate mouse and keyboard actions;

3. Automatically get the list of files under a certain path;

screenshot function

Find color click function ideas:

Grab a snapshot of the current screen, specify a coordinate and a color, and click on the coordinate if the color of the coordinate matches.

1. Grab a snapshot of the current screen () to return an image of the mode "RGB".

2. method() returns a pixel access object for reading and modifying pixels

3. Operating the mouse and keyboard with pywin32

King's Honor Auto Brush Adventure Mode Source Code Delivery

Due to the different resolution, you have to modify the coordinates and colors accordingly in order to run it properly on your own computer.

/janyroo/pvpkey

import win32gui, win32api, win32con,time
from win32api import GetSystemMetrics
from PIL import ImageGrab

def PilImage(x,y):
 a, b = GetSystemMetrics(0), GetSystemMetrics(1) # Python get screen resolution
 im = ((0,0,a,b))# Unlike coordinates, where 0, 0, 1, 1 is a pixel, coordinates are from 0 to 1919
 pix = ()
 return pix[x,y]

def DisplaySize():
 return GetSystemMetrics(0), GetSystemMetrics(1) # Python get screen resolution

def LeftClick(x, y): # Left mouse click on the screen coordinates (x, y)
 ((x, y)) # Mouse positioning to coordinates (x, y)
 # Note: Different screen resolution will affect the mouse positioning, please use percentage conversion if needed
 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) # Left mouse button down
 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) # Left mouse button pops up

 # win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN + win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) # test

def PressOnce(x): # Simulate keyboard entry of a key value, keycode: x
 win32api.keybd_event(x, 0, 0, 0)
'''
# Testing
a, b = DisplaySize()
print(a,b)
LeftClick(30, 30) # Click
PressOnce(13) # Enter
PressOnce(9) # TAB
print(PilImage(80,546))
'''
# SetCursorPos', 'No error message is available' Run as administratorpycharm
if __name__=="__main__":
 jisu=0
 ltime=0
 ntime=0
 (5)
 print(PilImage(875, 573))
 print(PilImage(908,323))
 print(PilImage(1050, 635))
 print(PilImage(914,486))
 while 1:

  if PilImage(875, 573)==(220, 154, 39):
   LeftClick(875, 573)
   jisu += 1
   ntime = ()-ltime
   ltime = ()
   print("Swiped %d time, gained %d experience, took %d seconds." % (jisu,jisu*82,ntime))
  elif PilImage(908,323)==(26, 35, 101):
   LeftClick(908,323)
  elif PilImage(1050,635)==(216, 125, 26):
   LeftClick(1050,635)
  elif PilImage(1061,138)==(48, 131, 205):
   LeftClick(1061,145)
  elif PilImage(1061,138)==(46, 125, 197):
   LeftClick(1061,145)
  elif PilImage(914,486)==(196,39,80):
   LeftClick(914,486)
  else:
   pass

  (5)

Additional knowledge:python batch change one color to random color in all images in a folder

upfront

Requirement: sometimes need to change one color in the picture to another color, ps can help us to complete this task, but if there are a number of pictures, ps can not meet our needs, then we need to use python to help us quickly solve.

source code (computing)

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 26 20:03:10 2018
@author: Administrator
"""
from PIL import Image
import random
 
for n in range(1,4):# of images in the folder
 i = 1
 j = 1
 img = ("C:/Users/Administrator/Desktop/aa/"+str(n)+".png")#Read the picture
 img = ("RGB")
 
 width = [0]# Length
 height = [1]# Width
 for i in range(0,width):# Iterate over points of all lengths
  for j in range(0,height):# Iterate over points of all widths
   data = ((i,j))#i,j denote pixel points
   if (data[0]==255 and data[1]==255 and data[2]==255):
    m = (160,190)# Take a random value of color 160-190
    print("m=",m)
    ((i,j),(m,m,m))#Color change
 img = ("L")#Convert images to grayscale
 ("C:/Users/Administrator/Desktop/aa/"+str(n)+".png")#Save the image after modifying the pixel points

misdirection

Here you need the image to be in RGB mode, it can't be an L grayscale image, otherwise you will get the following error:

TypeError: 'int' object is not subscriptable

/questions/8220702/error-int-object-is-not-subscriptableIt explains the problem, but the main reason may lie in the fact that the image is a grayscale image, then the DATA data is empty, and the RGB color values cannot be extracted, and the program reports an error.

Random color

m = (160,190)# Take a random value of color 160-190
print("m=",m)
((i,j),(m,m,m))#color change

Here, it is random set random color value, change between 160-190, if you need to set other color value, you can change the moving range, or set m to a fixed constant, then represents a fixed color value.

P.S. Picture black and white swap

For binary graphs, the black and white colors in the image can be interchanged with each other via python.

from PIL import Image
import  
 
for i in range(1,37551): 
# Read in the picture
 image = ('C:/Users/Administrator/Desktop/bbb/'+str(i).zfill(6)+'.png')
 #image = ((64,64))
 #Reverse
 inverted_image = (image)
 #SavePicture
 inverted_image.save('C:/Users/Administrator/Desktop/bbb/'+str(i).zfill(6)+'.png')

in the end

Above this python to achieve the keysmith to find the color click function tutorial, the use of pywin32 and Pillow library is all I have to share with you, I hope to be able to give you a reference, and I hope that you have more support for me.