【Python画画】失败案例总结

攻略一网站:https://blog.csdn.net/pipisorry/article/details/46564967

1.spy++找窗口句柄

2.游戏照片提取

3.俩图 切成好多方块 比较 颜色直方图 差异

4.标记不同

from ctypes import*
import os,time
import pyautogui as pag
import win32con
import win32gui
from PIL import ImageGrab

from _test import huaHua
from PyQt5.QtWidgets import QApplication
import sys
#from Utility.Colors import DEFAULT,RED
from numpy import*


#--------------------------------------------------------------------
def channel_compare(cha_a,cha_b):
#比较两个颜色通道差异值并返回
sum_a = sum([i * v for i, v in enumerate(cha_a)])
sum_b = sum([i * v for i, v in enumerate(cha_b)])
global diff_channel
if sum_a + sum_b > 0:
diff_channel = abs(sum_a - sum_b) / max(sum_a,sum_b) * 10000
return diff_channel #差异值

def image_compare(image_a,image_b):
#返回俩图差异值,返回俩图红绿蓝差值万分比之和
histogram_a = image_a.histogram()
histogram_b = image_b.histogram()
if len(histogram_a) != 768 or len(histogram_b) != 768:
print(RED,"get histogram error",DEFAULT)
return None
#print([i*v for i,v in enumerate(histogram_a)][0:10])
diff_red = channel_compare(histogram_a[:256],histogram_b[:256])
diff_green = channel_compare(histogram_a[256:512],histogram_b[256:512])
diff_blue = channel_compare(histogram_a[512:768],histogram_b[512:768])
return diff_red,diff_green,diff_blue

#---------------------------------------------------------------------
#获取句柄
hwnd = win32gui.FindWindow(32770,"大家来找茬")
if not hwnd:
print(RED,'window not found!',DEFAULT)
else:
print(hwnd)
#---------------------------------------------------------------------
win32gui.ShowWindow(hwnd,win32con.SW_RESTORE) #强行显示界面
win32gui.SetForegroundWindow(hwnd) #将窗口提到最前
#截屏
game_rect = win32gui.GetWindowRect(hwnd) #获取句柄矩形
#src_image = ImageGrab.grab(game_rect)
src_image = ImageGrab.grab((game_rect[0]+116,game_rect[1]+390,game_rect[2]-117,game_rect[1]+747)) #截图句柄矩形 game_rect[0]为图片左上角x轴
width,hight = src_image.size #图片的宽高
left_box = (0,0,476,hight) #左边图的裁剪,0,0,476,高
right_box = (571,0,width,hight) #右边图的裁剪,571,0,width,hight
image_left = src_image.crop(left_box) #左边图赋予到image_left
image_right = src_image.crop(right_box) #以上
#---------------------------------------------------------------------
#俩图裁剪成N个小图片
width_new,hight_new = image_left.size
print(width_new,hight_new)
result = zeros((hight_new // 10,width_new // 10))
for col in range(0,width_new // 10):
for row in range(0,hight_new // 10):
clip_box = (col * 10, row * 10, (col + 1) * 10, (row + 1) * 10)
clip_image_left = image_left.crop(clip_box)
clip_image_right = image_right.crop(clip_box)
clip_diff = image_compare(clip_image_left,clip_image_right)
if sum(clip_diff) > 1500:
result[row][col] = 1

app = QApplication(sys.argv)
huahua = huaHua(result,1007,402,10,10)
sys.exit(app.exec_())
#---------------------------------------------------------------------
x = image_compare(image_left,image_right)
print(x)


#enumerate i*v i是12345678910,v是列表遍历
#---------------------------------------------------------------------
#print(width,hight)
#---------------------------------------------------------------------

#_test.py
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QFont,QPen
from PyQt5.QtCore import Qt
class huaHua(QWidget):
def __init__(self,result,right_x,y,width,height): #创建对象时要做的事
super().__init__() #继承父类,QWidget中有paintEvent函数
self.initUI() #创建窗口
self.result = result #赋值给到新对象
self.ANCHOR_RIGHT_X = right_x
self.ANCHOR_Y = y
self.CLIP_WIDTH = width
self.CLIP_HEIGHT = height
def initUI(self):
self.setGeometry(436,431,1920,1080) #坐上角的坐标 与 窗口大小
self.setWindowOpacity(0.5) #透明度
self.setWindowTitle('huaHua') #窗口名字
self.show() #秀窗口
def paintEvent(self,event): #event不知道啥意思
qp = QPainter() #创建画画者对象
qp.begin(self) #开始画画
qp.setPen(QPen(Qt.black, 2, Qt.SolidLine)) #设置笔为笔,不是刷子,黑色,粗细为2,等等
for row in range(len(self.result)): #result是二维数组,每一个坐标的值是1或0, 1是说明不同
for col in range(len(self.result[0])): #rusult[0]是一行,就是说col是列,从0到n列
if self.result[row][col] != 0: #如果他们等于1
base_r_x = self.ANCHOR_RIGHT_X + self.CLIP_WIDTH * col
base_y = self.ANCHOR_Y + self.CLIP_HEIGHT * row
print(base_r_x,base_y) #以下代码是最边界时画画,旁边的方块是0时画画的代码
if row == 0 or self.result[row-1][col] == 0:
qp.drawLine(base_r_x,base_y,base_r_x + self.CLIP_WIDTH,base_y)
if row == len(self.result) - 1 or self.result[row+1][col] == 0:
qp.drawLine(base_r_x,base_y + self.CLIP_HEIGHT,base_r_x + self.CLIP_WIDTH,base_y + self.CLIP_HEIGHT)
if col == 0 or self.result[row][col-1] == 0:
qp.drawLine(base_r_x,base_y,base_r_x,base_y+self.CLIP_HEIGHT)
if col == len(self.result[0]) -1 or self.result[row][col+1] == 0:
qp.drawLine(base_r_x + self.CLIP_HEIGHT,base_y,base_r_x+self.CLIP_WIDTH,base_y+self.CLIP_HEIGHT)
qp.end() #结束画画
#qp.fillRect(self.btn_toggle.geometry(), QBrush(QColor(0, 0, 0)))

猜你喜欢

转载自www.cnblogs.com/naraka/p/9027334.html
今日推荐