此篇文章按照功能的方式介绍pillow的使用,例子是利用ImageFont & ImageDraw 生成简单验证码
添加图像识别对比技术
from PIL import Image
im = Image.open(‘test.jpg‘)
w,h = im.size
print(‘origin image size:%s %s‘ % (w,h))
# 缩放
im.thumbnail(w//2, h//2)
print(‘Resize image to :%s %s‘ % (w//2, h//2))
im.save(‘testdemo1.jpg‘)
# 模糊
from PIL import ImageFilter
im2 = im.filter(ImageeFilter.BLUR)
im2.save(‘testblur.jpg‘)
#获取图片模式并改变
img_mode = im.mode
im = im.convert(‘L‘)
# 获取每个像素点,i,j分别为长和宽
im.getpixel((i, j))
im.format im.size im.mode -> mode L :灰度图 RGB:真彩色
import random
# 生成英文字符
def rand_char():
return chr(random.randint(65, 90))
def rand_color():
return random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)
def rand_color2():
return random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)
width = 60 * 4
height = 60
# background
image = Image.new(‘RGB‘, (width, height), (255, 255, 255))
font = ImageFont.truetype(r‘/Library/Fonts/arial.ttf‘, 36)
# 建立画笔,并在x,y方向添加随机颜色像素点
draw = ImageDraw.Draw(image)
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rand_color())
# 画出一些字母,注意坐标 width=60*4 -> 60*t low_height = 10 | height=60 -> font = 36 36+20=56~60
for t in range(4):
draw.text((60 * t + 10, 10), rand_char(), font=font, fill=rand_color2())
image = image.filter(ImageFilter.BLUR)
image.save(‘code.jpg‘)
from PIL import Image, ImageGrab
class imgage_match:
def __init__(self):
# 定 义 大 图 和 小 图 对 象 并 赋 予 初 值 None
self.screen = None
self.template = None
# 定 义 大 图 和 小 图 的 数 据 对 象
self.screen_data = None
self.template_data = None
# 定 义 像 素 对 比 的 方 法 p1 大图 p2 小图
def compare(self, p1, p2):
# 像 素 的 四 个 元 素 指 RGBA 的 值
return p1[0] == p2[0] and p1[1] == p2[1] and p1[2] == p2[2] and p1[3] == p2[3]
# 获 取 小 图 中 心 点 对 应 大 图 匹 配 的 位 置 坐 标
def find_image(self, target):
# getcwd 获取当前路径
# path.join 拼 接 两 个 路 径
# 获 取 小 图 的 路 径
# 获 取 小 图 对 象
self.template = Image.open(target).convert(‘RGBA‘)
# 获 取 大 图 对 象
self.screen = ImageGrab.grab().convert(‘RGBA‘)
# 获 取 小 图 和 大 图的 尺 寸
template_width, template_height = self.template.size
screen_width, screen_height = self.screen.size
print(template_width, template_height)
print(screen_width, screen_height)
# 获 取 大 图 小 图 数 据 对 象
self.template_data = self.template.load()
self.screen_data = self.screen.load()
# 开始滑动对比
for y in range(screen_height - template_height):
for x in range(screen_width - template_width):
# 在五个点都匹配的前提下进行全像素匹配 如果匹配上就返回中心点坐标
right_x = template_width + x - 1
right_y = template_height + y - 1
left_y = template_height + y - 1
mid_x = x + int(template_width / 2) - 1
mid_y = y + int(template_height / 2) - 1
judge_x = self.compare(self.screen_data[x, y], self.template_data[0, 0]) and self.compare(
self.screen_data[right_x, y], self.template_data[template_width - 1, 0])
judge_y = self.compare(self.screen_data[x, left_y],
self.template_data[0, template_height - 1]) and self.compare(
self.screen_data[right_x, right_y], self.template_data[template_width - 1, template_height - 1])
judge_mid = self.compare(self.screen_data[mid_x, mid_y],
self.template_data[int(template_width / 2) - 1, int(template_height / 2) - 1])
judge = judge_x and judge_y and judge_mid
# print(x,y)
if judge:
print(‘111‘)
is_match = self.check_match(x, y)
if is_match:
pos_x = x + int(template_width / 2) - 1
pos_y = y + int(template_height / 2) - 1
return pos_x, pos_y
else:
return -1, -1
# 定义全像素比对方法
def check_match(self, x, y):
template_width, template_height = self.template.size
count = 0
for small_y in range(template_height):
for small_x in range(template_width):
# 大图的位置小图的位置
if self.compare(self.screen_data[x + small_x, y + small_y], self.template_data[small_x, small_y]):
count += 1
if count / (int(template_width) * int(template_height)) > 0.5:
return True
else:
# print(count)
return False
# if not self.compare(self.screen_data[x + small_x, y + small_y], self.template_data[small_x, small_y]):
# return False
# else:
# return True
def check_exists(self, target):
pass
if __name__ == ‘__main__‘:
# imgage_match().find_image(‘1.png‘)
import time
time.sleep(2)
print(imgage_match().find_image(‘1.png‘))
def find_image(self,target,similary):
# 小图路径
image_path=os.path.join(os.getcwd(),"source")
# 拼接大图路径及名称
screen_path=os.path.join(image_path,‘scrren_shot.png‘)
# 截大图并保存
ImageGrab.grab().save(screen_path)
# 分别获取小图大图对象
screen = cv.imread(screen_path)
template= cv.imread(os.path.join(image_path,target))
# 进行模板匹配
result=cv.matchTemplate(screen,template,cv.TM_CCORR_NORMED)
print(result)
min,max,min_loc,max_loc=cv.minMaxLoc(result)
# 针对max相似度 进行检查
if max < similary:
return -1,-1
# 计算中心点的坐标
x=max_loc[0] + int(template.shape[1]/2)
y=max_loc[1] + int(template.shape[0]/2)
return x ,y
原文:https://www.cnblogs.com/WheelCode/p/13070623.html