python PIL模块的基本使用

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模块的资料请关注我们其它相关文章!

(0)

相关推荐

  • Python图像处理库PIL的ImageFilter模块使用介绍

    ImageFilter模块提供了滤波器相关定义:这些滤波器主要用于Image类的filter()方法. 一.ImageFilter模块所支持的滤波器 当前的PIL版本中ImageFilter模块支持十种滤波器: 1.  BLUR ImageFilter.BLUR为模糊滤波,处理之后的图像会整体变得模糊. 例子: >>> from PIL importImageFilter >>> im02 =Image.open("D:\\Code\\Python\\test

  • Python图像处理库PIL的ImageGrab模块介绍详解

    ImageGrab模块用于将当前屏幕的内容或者剪贴板上的内容拷贝到PIL图像内存. 当前版本只支持windows系统. 一.ImageGrab模块的函数 1.  Grab 定义:ImageGrab.grab()⇒ image ImageGrab.grab(bbox) ⇒ image 含义:(New in 1.1.3)抓取当前屏幕的快照,返回一个模式为"RGB"的图像.参数边界框用于限制只拷贝当前屏幕的一部分区域. 例子: >>> from PIL importImag

  • Python图像处理库PIL的ImageEnhance模块使用介绍

    ImageEnhance模块提供了一些用于图像增强的类. 一.ImageEnhance模块的接口 所有的增强类都实现了一个通用的接口,包括一个方法: enhancer.enhance(factor) ⇒ image 该方法返回一个增强过的图像.变量factor是一个浮点数,控制图像的增强程度.变量factor为1将返回原始图像的拷贝:factor值越小,颜色越少(亮度,对比度等),更多的价值.对变量facotr没有限制. 二.ImageEnhance模块的Color类 颜色增强类用于调整图像的颜

  • Python图像处理库PIL的ImageDraw模块介绍详解

    ImageDraw模块提供了图像对象的简单2D绘制.用户可以使用这个模块创建新的图像,注释或润饰已存在图像,为web应用实时产生各种图形. PIL中一个更高级绘图库见The aggdraw Module 一.ImageDraw模块的概念 1.  Coordinates 绘图接口使用和PIL一样的坐标系统,即(0,0)为左上角. 2.  Colours 为了指定颜色,用户可以使用数字或者元组,对应用户使用函数Image.new或者Image.putpixel.对于模式为"1","

  • Python离线安装PIL 模块的方法

    python的库一般都用pip安装. 但是有时候也会出现在线安装失败的情况,如下图安装PIL模块时报错: 这时候可以采取离线安装的方式: 一.首先下载离线安装包 PIL官方版不支持py3,不过有非官方那个的替代品pillow,地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow 注意选择版本:我的python是Python2.7,系统是64位的,但是安装了32位的Python2.7.10 选择 Pillow-4.2.1-cp27-none-wi

  • Python图片处理模块PIL操作方法(pillow)

    一.PIL的基本概念: PIL中所涉及的基本概念有如下几个:通道(bands).模式(mode).尺寸(size).坐标系统(coordinate system).调色板(palette).信息(info)和滤波器(filters). 1.通道 每张图片都是由一个或者多个数据通道构成.PIL允许在单张图片中合成相同维数和深度的多个通道. 以RGB图像为例,每张图片都是由三个数据通道构成,分别为R.G和B通道.而对于灰度图像,则只有一个通道. 对于一张图片的通道数量和名称,可以通过方法getban

  • python pillow模块使用方法详解

    pillow Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库.pillow可以说已经取代了PIL,将其封装成python的库(pip即可安装),且支持python2和python3,目前最新版本是3.0.0. Pillow的Github主页:https://github.com/python-pillow/Pillow Pillow的文档(对应版本v3.0.0): https://pillow.readthedocs.org/en/latest/handb

  • python 实现PIL模块在图片画线写字

    图片上画线条 import sys from PIL import Image,ImageDraw im = Image.open("th.png") draw = ImageDraw.Draw(im) #实例化一个对象 draw.line((0, 0) + im.size, fill=128, width=5) #线的起点和终点,线宽 draw.line((0, im.size[1], im.size[0], 0), fill=128) draw.line((0,im.size[1]

  • Python图像处理PIL各模块详细介绍(推荐)

    Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内.如open.save.conver.show-等功能. open类 Image.open(file) ⇒ image Image.open(file, mode) ⇒ image 要从文件加载图像,使用 open() 函数, 在 Image 模块: @zhangziju from PIL import Image ##调用库 im = Image.open("E:\mywif

  • python3使用Pillow、tesseract-ocr与pytesseract模块的图片识别的方法

    1.安装Pillow pip install Pillow 2.安装tesseract-ocr github地址: https://github.com/tesseract-ocr/tesseract 或本地下载地址:https://www.jb51.net/softs/538925.html windows: The latest installer can be downloaded here: tesseract-ocr-setup-3.05.01.exe and tesseract-oc

  • Python图像处理库PIL的ImageFont模块使用介绍

    ImageFont模块定义了相同名称的类,即ImageFont类.这个类的实例存储bitmap字体,用于ImageDraw类的text()方法. PIL使用自己的字体文件格式存储bitmap字体.用户可以使用pilfont工具包将BDF和PCF字体描述器(Xwindow字体格式)转换为这种格式. 从版本1.1.4开始,PIL可以配置是否支持TrueType和OpenType字体(和FreeType库支持其他的字体格式一样).对于更早的版本,只在imToolkit包中支持TrueType字体. T

  • Python 3.6 -win64环境安装PIL模块的教程

    PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了许多新特性,因此,我们可以直接安装使用Pillow. 32位的电脑环境安装pillow 如果安装了Anaconda,Pillow就已经可用了.否则,需要在命令行下通过pip安装: $ pip

  • 详解python3安装pillow后报错没有pillow模块以及没有PIL模块问题解决

    也许自己真的就是有手残的毛病,你说好端端的环境配置好了,自己还在那里瞎鼓捣,我最不想看到的就是在安装一个别的模块的时候,自动卸载了本地的其他模块,每每这个时候,满满的崩溃啊,今天就是一个鲜活的例子. 我们都知道由于2和3版本的差异,2中的PIL模块可以直接安装和导入使用,3中需要安装的实际是Pillow模块,导入的却是PIL模块,我在安装别的模块的时候居然自动地把我本机安装好的Pillow模块卸载了,导致我后面使用的时候一直报错,想着直接再安装一下就行了,却发现事情真的是一团糟. python

随机推荐