一 PIL(Pillow)
https://pillow.readthedocs.io/en/latest/index.html 安装:
pip install pillow
Basic
from PIL import Image
im = Image.open('test.png')
# 宽,高
width, height = im.size
# 查看
im.show()
# 保存
im.save('out.png')
ImageEnhance
增强工具,包括亮度,颜色,对比度,锐化等
from PIL import ImageEnhance
im = Image.open('test.png')
# 对比度增强
ImageEnhance.Contrast(im).enhance(2.0)
# 锐化
ImageEnhance.Sharpness(im).enhance(2.0)
# 颜色增强
ImageEnhance.Color(im).enhance(2.0)
# 亮度增强
ImageEnhance.Brightness(im).enhance(2.0)
ImageFilter
Filter工具,可进行模糊/锐化处理
from PIL import ImageFilter
im = Image.open('test.png')
im.filter(ImageFilter.BLUR)
im.filter(ImageFilter.GaussianBlur(radius=2))
im.filter(ImageFilter.SMOOTH)
im.filter(ImageFilter.SHARPEN)
im.filter(ImageFilter.EDGE_ENHANCE)
# 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’)
<a name="frs6q"></a>
### ImageOps
> 包含多种图像处理方法,如反色,自动调节对比度,裁剪,伸缩等,大多需要 `L` 或 `RGB` 模式
```python
im = ImageOps.invert(im.convert('RGB'))
PixelAccess
像素数据获取和修改
示例,过去每个像素点的RGB值,根据条件进行黑白颜色替换
im = Image.open('test.png')
im = im.convert('RGB')
pixdata = im.load()
weight, height = im.size
for x in range(weight):
for y in range(height):
rgb = pixdata[x, y]
if (rgb[0] - rgb[1] > 50) and (rgb[0] - rgb[2] > 50):
pixdata[x, y] = (0, 0, 0)
else:
pixdata[x, y] = (255, 255, 255)
im.save('out.png')
原图
转换后
PS:每个像素点的RGB值可通过PS软件进行查看 吸管工具 - 点击像素点