[python]图像处理模块skimage/open-cv/PIL.Image的常用操作

[python]图像处理模块skimage/open-cv/PIL.Image的常用操作

Introduction

做cv的小伙伴在看github上的代码的时候会频繁地碰到题目中地三个模块,因为它们都具有强大的图像处理功能,话不多说直接干货。

图像读取

  • PIL
from PIL import Image
img = Image.open('01.jpg')
# 转为灰度
imgGrey = img.convert('L')
# 特别注意PIL.Image读出来的对象是PIL.Image object不是numpy
img.show()
imgGrey.show()
 
  • skimage
from skimage import io
img = io.imread('01.jpg')
# 直接读出来就是numpy
  • cv2
import cv2
# 读出来就是numpy
# 大小是h * w * c 
img = cv2.imread("01.jpg")
imgGrey = cv2.imread("01.jpg",0)
cv2.imshow("img",img)
cv2.imshow("imgGrey",imgGrey)

图像存储

  • PIL
from PIL import Image
img = Image.open('01.jpg')
# 转为灰度
imgGrey = img.convert('L')
img.save('img_copy.jpg')
imgGrey.save('img_gray.jpg')
  • skimage
from skimage import io
img = io.imread('01.jpg')
# 直接读出来就是numpy
# 大小是h * w * c
io.imsave('02.jpg', img)
  • cv2
import cv2
 
img = cv2.imread("01.jpg")
imgGrey = cv2.imread("01.jpg",0)
cv2.imshow("img",img)
cv2.imshow("imgGrey",imgGrey)
cv2.waitKey()
cv2.imwrite("Copy.jpg",img)

图像resize

  • PIL
from PIL import Image
 
img = Image.open('01.jpg')
 
width, height = img.size
 
img_NEARESET = img.resize((width//2, height//2))  # 缩放默认模式是NEARESET(最近邻插值)
img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR)  # BILINEAR 2x2区域的双线性插值
img_BICUBIC = img.resize((width//2, height//2), Image.BICUBIC)  # BICUBIC 4x4区域的双三次插值
img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS)  # ANTIALIAS 高质量下采样滤波
  • skimage
from skimage import io
img = io.imread('01.jpg')
# 直接读出来就是numpy
from skimage.transform import resize

img2 = resize(img, (256,256))
  • cv2
import cv2
 
img = cv2.imread("01.jpg")
cv2.imshow("img",img)
 
img1 = cv2.resize(img,(300,100),cv2.INTER_AREA)
 
hight, width = img.shape[0:2]
img2 = cv2.resize(img,(width,hight))
 
cv2.imshow("img1",img1)
cv2.imshow('img2',img2)
 
cv2.waitKey()

更加完整可见:

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页