python PIL模块的基本使用

作者:傻白甜++ 时间:2022-08-28 16:35:19 

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')

来源:https://www.cnblogs.com/feifeifeisir/p/11027013.html

标签:python,PIL,模块
0
投稿

猜你喜欢

  • asp随机提取access数据库记录的几种方法

    2007-09-06 19:42:00
  • Python验证码识别的方法

    2023-05-30 10:22:39
  • CVE-2020-15148漏洞分析

    2023-06-13 13:41:18
  • 细品Dreamweaver MX 2004内建FW技术

    2010-09-02 12:38:00
  • Asp用正则表达式获取文章中的所有图片地址

    2010-07-17 13:11:00
  • sqlserver 不重复的随机数

    2012-02-12 15:29:29
  • 浅析is_writable的php实现

    2023-09-09 01:41:05
  • php中关于socket的系列函数总结

    2023-11-17 13:57:35
  • 通过实例了解python__slots__使用方法

    2023-03-16 13:24:50
  • 带你深入了解MySQL语句优化的基本原则

    2008-11-27 17:00:00
  • python 弹窗提示警告框MessageBox的实例

    2023-11-12 01:07:17
  • sql server 锁表语句分享

    2012-02-12 15:49:20
  • Python爬虫实现百度图片自动下载

    2021-07-12 22:42:56
  • asp和php页面全面封杀WVS扫描器的代码

    2011-02-28 10:43:00
  • Javascript中的isNaN函数使用说明

    2023-08-27 10:10:02
  • Mootools 1.2教程(15)——滚动条(Slider)

    2008-12-09 17:35:00
  • python实现浪漫的烟花秀

    2023-09-23 00:50:23
  • SQL查询重复记录

    2011-03-27 09:06:00
  • 不用mod_rewrite直接用php实现伪静态化页面代码

    2023-11-01 07:07:45
  • phpmyadmin中禁止外网使用的方法

    2023-09-12 01:10:22
  • asp之家 网络编程 m.aspxhome.com