淺談python圖片處理Image和skimage的區(qū)別
做cnn的難免要做大量的圖片處理。由于接手項目時間不長,且是新項目,前段時間寫代碼都很趕,現(xiàn)在稍微總結(jié)(恩,總結(jié)是個好習(xí)慣)。
1,首先安裝python-Image和python-skimage、python-matplotlib。
簡單代碼:
import Image as img
import os
from matplotlib import pyplot as plot
from skimage import io,transform
import argparse
def show_data(data):
fig = plot.figure()
ax = fig.add_subplot(121)
ax.imshow(data, cmap='gray')
ax2 = fig.add_subplot(122)
ax2.imshow(data)
plot.show()
if __name__ == "__main__":
parse = argparse.ArgumentParser()
parse.add_argument('--picpath', help = "the picture' path")
args = parse.parse_args()
img_file1 = img.open(args.picpath)#Image讀圖片
one_pixel = img_file1.getpixel((0,0))[0]
print "picture's first pixe: ",one_pixel
print "the picture's size: ", img_file1.size#Image讀出來的size是高寬
show_data(img_file1)
img_file2 = io.imread(args.picpath)#skimage讀圖片
show_data(img_file2)
print "picture's first pixel: ", img_file2[0][0][0]
print "the picture's shape: ", img_file2.shape#skimage讀出來的shape是高,寬, 通道
調(diào)用及輸出:

其實Image讀出來的是PIL什么的類型,而skimage.io讀出來的數(shù)據(jù)是numpy格式的。如果想直接看Image和skimage讀出來圖片的區(qū)別,可以直接輸出它們讀圖片以后的返回結(jié)果。
2.Image和skimage讀圖片:
img_file1 = img.open(args.picpath) img_file2 = io.imread(args.picpath)
3.讀圖片后數(shù)據(jù)的大小:
print "the picture's size: ", img_file1.size print "the picture's shape: ", img_file2.shape
4.得到像素:
one_pixel = img_file1.getpixel((0,0))[0] img_file2[0][0][0]
分析:
1.從3的輸出可以看出img讀圖片的大小是圖片的(height,width);
skimage的是(height,width, channel)[這也是為什么caffe在單獨測試時要要在代碼中設(shè)置:transformer.set_transpose('data',(2,0,1)),因為caffe可以處理的圖片的數(shù)據(jù)格式是(channel,height,width),所以要轉(zhuǎn)換數(shù)據(jù)啊]
2.img讀出來的圖片獲得某點像素用getpixel((h,w))可以直接返回這個點三個通道的像素值
skimage讀出來的圖片可以直接img_file2[0][0][0]獲得,但是一定記住它的格式,并不是你想的(channel,height,width)
關(guān)于matplotlib簡單的畫圖請關(guān)注下篇~
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用TensorFlow直接獲取處理MNIST數(shù)據(jù)方式
今天小編就為大家分享一篇使用TensorFlow直接獲取處理MNIST數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧隊列
這篇文章主要介紹了Python 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧,隊列,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

