Last active
December 20, 2015 02:49
-
-
Save Yhzhtk/6059549 to your computer and use it in GitHub Desktop.
拍书产品用到的Python脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=gbk | |
''' | |
Created on 2013-7-22 | |
@author: gudh | |
''' | |
import win32gui,win32api,win32con | |
import time | |
DialogName = "登录".encode("gbk") | |
ButtonName = "登录".encode("gbk") | |
win = win32gui.FindWindow(0,DialogName) | |
while win == 0: | |
win = win32gui.FindWindow(0,DialogName) | |
#win32api.MessageBox(0, "请先运行五子棋程序".encode("gbk"), "错误!".encode("gbk"), win32con.MB_ICONERROR) | |
#win32api.SendMessage(win,16) #发送16是关闭窗口的意思 | |
time.sleep(1) # 休眠X秒 | |
hbtn = win32gui.FindWindowEx(win,None,None,ButtonName) | |
(left,top,right,bottom) = win32gui.GetWindowRect(hbtn) | |
win32api.SetCursorPos((left+(right-left)/2,top+(bottom-top)/2)) #光标定位 | |
time.sleep(0.5) | |
# 鼠标点击 | |
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) | |
time.sleep(0.05) | |
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) | |
time.sleep(0.05) | |
#获取和移动鼠标位置 | |
oldCursorPos = win32gui.GetCursorPos() | |
win32api.SetCursorPos((1,1)) | |
#上面代码慢且容易出错,可以直接用发消息的方式,更快也不容易出错: | |
savewin = win32gui.FindWindow(None,'Save as...') | |
inputfile = win32gui.GetDlgItem(savewin,0x47C) | |
win32gui.SendMessage(inputfile,win32con.WM_SETTEXT,0,'result') | |
savebtn = win32gui.GetDlgItem(savewin,1) | |
win32gui.SendMessage(savebtn,win32con.BM_CLICK,0,0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
''' | |
Created on 2013-7-22 | |
@author: gudh | |
''' | |
import win32api,win32con,win32gui | |
import time | |
import ImageGrab,Image | |
import urllib | |
import os | |
def move(loc): | |
'''移动鼠标''' | |
win32api.SetCursorPos(loc) | |
def click(left=True): | |
'''单击鼠标''' | |
if left: | |
d = win32con.MOUSEEVENTF_LEFTDOWN | |
u = win32con.MOUSEEVENTF_LEFTUP | |
else: | |
d = win32con.MOUSEEVENTF_RIGHTDOWN | |
u = win32con.MOUSEEVENTF_RIGHTUP | |
win32api.mouse_event(d, 0, 0) | |
time.sleep(0.01) | |
win32api.mouse_event(u, 0, 0) | |
time.sleep(0.01) | |
def cut(dect): | |
'''屏幕截图''' | |
im = ImageGrab.grab() | |
im1 = im.crop(dect) | |
return im1 | |
def save(img, path): | |
'''保存图片''' | |
img.save(path) | |
spath = path.replace("high", "low") | |
img.save(spath, 'JPEG', quality = 95) | |
def white(img): | |
'''判断是否是全白色''' | |
size = img.size | |
for x in range(size[0]): | |
for y in range(size[1]): | |
print img.getpixel((x, y)) | |
if img.getpixel((x, y)) != (255, 255, 255): | |
return False | |
return True | |
def prgb(img): | |
'''打印所有RGB''' | |
size = img.size | |
for x in range(size[0]): | |
for y in range(size[1]): | |
print x,y,img.getpixel((x, y)) | |
def iscu(img, w, h, p): | |
'''是否是粗体''' | |
size = img.size | |
arr = [] | |
for y in range(size[1]): | |
arx = [] | |
for x in range(size[0]): | |
pix = img.getpixel((x, y)) | |
r = False | |
if pix[0] < p and pix[1] < p and pix[2] < p: | |
# 判断像素点是否满足黑的条件 | |
r = True | |
if x >= w and y >= h: | |
# 如果已经获取的像素达到判断范围则进行范围判断 | |
b = True | |
# 在以x,y为坐标的前面w,h点均满足条件则判断是粗体,否则跳出继续判断 | |
for i in range(x-w, x): | |
for j in range(y-h, y): | |
b &= arr[j][i] | |
if not b: | |
break | |
else: continue | |
break | |
if b: | |
return True | |
# 将当前像素信息记录到数组 | |
arx.append(r) | |
arr.append(arx) | |
return False | |
# dect = (600, 600, 720, 720) | |
# img = cut(dect) | |
# save(img, "c:/a.jpg") | |
for i in range(202,206): | |
p = r"D:\dd\好父母决定孩子一生\high\%d.jpg".encode("gbk") % i | |
if os.path.exists(p): | |
img = Image.open(p) | |
print p,iscu(img, 3, 3, 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment