一 PIL(Pillow)

https://pillow.readthedocs.io/en/latest/index.html 安装: pip install pillow

Basic

  1. from PIL import Image
  2. im = Image.open('test.png')
  3. # 宽,高
  4. width, height = im.size
  5. # 查看
  6. im.show()
  7. # 保存
  8. im.save('out.png')

ImageEnhance

增强工具,包括亮度,颜色,对比度,锐化等

  1. from PIL import ImageEnhance
  2. im = Image.open('test.png')
  3. # 对比度增强
  4. ImageEnhance.Contrast(im).enhance(2.0)
  5. # 锐化
  6. ImageEnhance.Sharpness(im).enhance(2.0)
  7. # 颜色增强
  8. ImageEnhance.Color(im).enhance(2.0)
  9. # 亮度增强
  10. ImageEnhance.Brightness(im).enhance(2.0)

ImageFilter

Filter工具,可进行模糊/锐化处理

  1. from PIL import ImageFilter
  2. im = Image.open('test.png')
  3. im.filter(ImageFilter.BLUR)
  4. im.filter(ImageFilter.GaussianBlur(radius=2))
  5. im.filter(ImageFilter.SMOOTH)
  6. im.filter(ImageFilter.SHARPEN)
  7. im.filter(ImageFilter.EDGE_ENHANCE)
  8. # more filters ...

convert

色彩空间的转换,如RGB,灰度,二值等

  • 1 (1-bit pixels, black and white, stored with one pixel per byte)
  • L (8-bit pixels, black and white)
  • P (8-bit pixels, mapped to any other mode using a colour palette)
  • RGB (3x8-bit pixels, true colour)
  • RGBA (4x8-bit pixels, true colour with transparency mask)
  • CMYK (4x8-bit pixels, colour separation)
  • YCbCr (3x8-bit pixels, colour video format)
  • I (32-bit signed integer pixels)
  • F (32-bit floating point pixels) ```python im = Image.open(‘test.png’)

im2 = im.convert(‘L’)

  1. <a name="frs6q"></a>
  2. ### ImageOps
  3. > 包含多种图像处理方法,如反色,自动调节对比度,裁剪,伸缩等,大多需要 `L` 或 `RGB` 模式
  4. ```python
  5. im = ImageOps.invert(im.convert('RGB'))

PixelAccess

像素数据获取和修改

示例,过去每个像素点的RGB值,根据条件进行黑白颜色替换

  1. im = Image.open('test.png')
  2. im = im.convert('RGB')
  3. pixdata = im.load()
  4. weight, height = im.size
  5. for x in range(weight):
  6. for y in range(height):
  7. rgb = pixdata[x, y]
  8. if (rgb[0] - rgb[1] > 50) and (rgb[0] - rgb[2] > 50):
  9. pixdata[x, y] = (0, 0, 0)
  10. else:
  11. pixdata[x, y] = (255, 255, 255)
  12. im.save('out.png')

原图
image.png
转换后
image.png

PS:每个像素点的RGB值可通过PS软件进行查看 吸管工具 - 点击像素点

image.png