欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python PIL模块的基本使用

程序员文章站 2022-07-03 20:00:59
pil基本功能介绍from pil import imagefrom pil import imageenhance img = image.open(r'e:\img\f1.png')img.sho...

pil基本功能介绍

from pil import image
from pil import imageenhance
 
img = image.open(r'e:\img\f1.png')
img.show()
#图像二值化
img = img.convert('l')
# 图像放大
img = img.resize((img.width * int(3), img.height * int(4)), image.antialias)
# # 对比度增强
enh_con = imageenhance.contrast(img)
contrast = 2
img_contrasted = enh_con.enhance(contrast)
# 亮度增强
enh_bri = imageenhance.brightness(img_contrasted)
brightness = 2.5
image_brightened = enh_bri.enhance(brightness)
#色度增强
enh_col = imageenhance.color(img)
color = 50
image_colored = enh_col.enhance(color)
# # 锐度增强
enh_sha = imageenhance.sharpness(img)
sharpness = 2
image_sharped = enh_sha.enhance(sharpness)
image_sharped.save(r'e:\img\f22.png', dpi=(300, 300), quality=95)
# image_sharped.save(r'e:\img\f22.png')
 
# 图片汉字识别
img2 = image.open(r'e:\img\f22.png')
code2 = pytesseract.image_to_string(img2, lang='chi_sim')
# print(code2)
# 图片裁剪
image_cro = image.open(r'e:\img\f24.png')
image_cropped = image_cro.crop(res)
image_cropped.save(u'e:\img\\f25.png') 

对图片进行黑白化处理

img_main = image.open(u'e:/login1.png')
img_main = img_main.convert('l')
threshold1 = 138
table1 = []
for i in range(256):
  if i < threshold1:
    table1.append(0)
  else:
    table1.append(1)
img_main = img_main.point(table1, "1")
img_main.save(u'e:/login3.png')

计算小图在大图的坐标

def get_screenxy_from_bmp(main_bmp, son_bmp):
  # 获取屏幕上匹配指定截图的坐标->(x,y,width,height)
 
  img_main = image.open(main_bmp)
  img_main = img_main.convert('l')
  threshold1 = 138
  table1 = []
  for i in range(256):
    if i < threshold1:
      table1.append(0)
    else:
      table1.append(1)
  img_main = img_main.point(table1, "1")
 
  img_son = image.open(son_bmp)
  img_son = img_son.convert('l')
  threshold2 = 138
  table2 = []
  for i in range(256):
    if i < threshold2:
      table2.append(0)
    else:
      table2.append(1)
  img_son = img_son.point(table2, "1")
 
  datas_a = list(img_main.getdata())
  datas_b = list(img_son.getdata())
  for i, item in enumerate(datas_a):
    if datas_b[0] == item and datas_a[i + 1] == datas_b[1]:
      yx = divmod(i, img_main.size[0])
      main_start_pos = yx[1] + yx[0] * img_main.size[0]
 
      match_test = true
      for n in range(img_son.size[1]):
        main_pos = main_start_pos + n * img_main.size[0]
        son_pos = n * img_son.size[0]
 
        if datas_b[son_pos:son_pos + img_son.size[0]] != datas_a[main_pos:main_pos + img_son.size[0]]:
          match_test = false
          break
      if match_test:
        return (yx[1], yx[0], img_son.size[0], img_son.size[1])
  return false

imagegrab实现屏幕截图

im = imagegrab.grab()
im.save('d:/as1.png')
 
#   # # # 参数说明
#   # # # 第一个参数 开始截图的x坐标
#   # # # 第二个参数 开始截图的y坐标
#   # # # 第三个参数 结束截图的x坐标
#   # # # 第四个参数 结束截图的y坐标
bbox = (897, 131, 930, 148)
im = imagegrab.grab(bbox)
im.save('d:/as2.png')

以上就是python pil模块的基本使用的详细内容,更多关于python pil模块的资料请关注其它相关文章!

相关标签: python PIL 模块