PIL(Python Image Library)常用操作简介

本文介绍了PIL库在Python3中的基本图像操作,包括裁剪特定区域、旋转、分拆与合并颜色通道、几何变换、颜色转换、图像滤波、点变换、单独处理颜色通道以及转换为JPEG格式等。
摘要由CSDN通过智能技术生成

Basic Operations

因为我有点儿懒。。。所以本篇后续就基本是英文了(输入法切换好累0.0),本教程配套有Jupyter Notebook,请在此处下载
Python3 based PIL(python image library) tutorials from Documents

from PIL import ImageFilter
from PIL import Image
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
im = Image.open("test.jpg")
print('format: ', im.format)
print('size: ', im.size)
print('mode: ', im.mode)
im.show()   # show the figure in photo viewer

format: JPEG
size: (625, 625)
mode: RGB

Cut A Certain Region

box = (100, 100, 400, 400)
region = im.crop(box)
# region.show()
plt.imshow(region)
plt.show()

这里写图片描述

Rotate

box = (100, 100, 400, 400)
region = im.crop(box)
region = region.transpose(Image.ROTATE_180)
im.paste(region, box)
# im.show()
plt.imshow(im)
plt.show()

这里写图片描述

Splitting and Merging Bands

im = Image.open("test.jpg")
r, g, b = im.split()
# r.show()
# g.show()
# b.show()
f, ax = plt.subplots(2, 2)
ax[0, 0].imshow(r)
ax[0, 0].set_title("R")
ax[0, 1].imshow(g)
ax[0, 1].set_title("G")
ax[1, 0].imshow(b)
ax[1, 0].set_title("B")

im = Image.merge("RGB", (g, b, r))

ax[1, 1].imshow(im)
ax[1, 1].set_title("Mixed Image")

plt.show()

这里写图片描述

Geometrical Transforms

im = Image.open("test.jpg")
out = im.resize((
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值