当需要对图片进行处理时,除了PS外,还可以考虑用python。尤其是大批量处理图片时,python的优势就体现出来了。此次介绍skimage包的基本用法。
1.网上下载的一张原始图片cat.jpg
2.代码
a.读取RGB和灰度图像
from skimage.io import imread, imshow
import matplotlib.pyplot as plt
image_color = imread('cat.jpg') # 读取RGB图像
image_gray = imread('cat.jpg', as_gray=True) # 读取灰度图像
plt.subplot(121), imshow(image_color); plt.title('RGB Format')
plt.subplot(122), imshow(image_gray); plt.title('Gray Format')
plt.show()
b.将RGB图像转化为灰度和HSV图像
from skimage.io import imread, imshow
from skimage.color import rgb2gray
from skimage.color import rgb2hsv
import matplotlib.pyplot as plt
img_rgb = imread('cat.jpg')
img_gray = rgb2gray(img_rgb)
img_hsv = rgb2hsv(img_rgb)
plt.subplot(131), imshow(img_rgb); plt.title('RGB Format')
plt.subplot(132), imshow(img_gray); plt.title('Gray Format')
plt.subplot(133), imshow(img_hsv); plt.title('HSV Format')
plt.show()
c.对图像按固定像素进行放缩
from skimage.io import imread, imsave
from skimage.transform import resize
import numpy as np
img = imread('cat.jpg')
img_resized = resize(img, (100, 100)) # 图像的尺寸转化为100 x 100。返回float类型。
img_unit8 = (img_resized * 255).astype(np.uint8) # 转化为uint8类型。ima_unit8/255转化为float类型
imsave('cat_resize.jpg', img_unit8)
d.对图像按比例进行放缩
from skimage.io import imread, imsave
from skimage.transform import rescale
import numpy as np
img = imread('cat.jpg')
img_rescaled = rescale(img, scale=(0.5, 0.5, 1)) # 第一二个维度(height, width)缩放为原来的一半,第三个维度(RGB)不变
img_unit8 = (img_rescaled * 255).astype(np.uint8)
imsave('cat_rescale.jpg', img_unit8)
e.将图像旋转一定角度
from skimage.io import imread, imshow
from skimage.transform import rotate
image = imread('cat.jpg')
image_rotated = rotate(image, angle=45, resize=True) # 逆时针旋转45度,改变大小以不丢失任何信息
imshow(image_rotated)
f. 图像左右、上下翻转
from skimage.io import imread, imshow
from numpy import fliplr, flipud
import matplotlib.pyplot as plt
cat = imread('cat.jpg')
cat_flipr = fliplr(cat) # 矩阵左右翻转
cat_flipud = flipud(cat) # 矩阵上下翻转
plt.subplot(131), imshow(cat)
plt.subplot(132), imshow(cat_flipr)
plt.subplot(133), imshow(cat_flipud)
plt.show()
g. 裁剪图像
from skimage.io import imread, imshow
image = imread('cat.jpg')
cropped = image[100:(image.shape[0]-100),100:(image.shape[1]-100)] # 上下左右各减去100像素
imshow(cropped)
h. 调节亮度
from skimage.io import imread, imshow
from skimage import exposure
import matplotlib.pyplot as plt
image = imread('cat.jpg')
image_bright = exposure.adjust_gamma(image, gamma=0.5, gain=1) # 变亮
image_dark = exposure.adjust_gamma(image, gamma=1.5, gain=1) # 变暗
plt.subplot(131), imshow(image)
plt.title('Original Image')
plt.subplot(132),imshow(image_bright)
plt.title('Bright Image')
plt.subplot(133),imshow(image_dark)
plt.title('Dark Image')
plt.show()
i. 滤镜
from skimage.io import imread, imshow
from skimage.filters import median
from skimage.filters import sobel_h
import matplotlib.pyplot as plt
image = imread('cat.jpg', as_gray=True)
image_median = median(image) # 中值滤镜(像素值被替换为相邻像素的中值)。平滑画面,保持画面清晰地同时,减小画面上微小的噪点。
image_sobelh = sobel_h(image) # sobel滤镜。突出显示图像的边缘。
plt.subplot(131), imshow(image)
plt.title('Original Image')
plt.subplot(132),imshow(image_median)
plt.title('Smooth Image')
plt.subplot(133),imshow(image_sobelh, cmap='Greys_r')
plt.title('Horizontal Edge')
plt.show()