【轉載】python+opencv解決方案:保存圖像的方法比較之一

https://blog.csdn.net/LiuKejiaHAX/article/details/80711208


學習各種圖片讀入和保存方法缩歪,分析對比其圖像的格式

1.cv2打開和保存圖片:分彩色和灰度圖兩種情況

? ? (1)讀入指令為cv2.imread('house.jpg')峻村,cv2.imread('house.jpg',cv2.IMREAD_GRAYSCALE)

? ? (2)讀入圖像為nd.array格式矩陣污桦,注意顏色頻道BGR的順序,

? ? (3)保存指令為cv2.imwrite('imgsavename.*',imgname)地沮。

? ? ? 如果顯示圖像時做了BGR2RGB竭翠,則在保存前應該做RGB2BGR酌泰,否則存的圖頻道顛倒。

2.PIL打開和保存圖片:

? ? (1)打開文件指令為Image.open('house.jpg')跪削,Image.open('house.jpg').convert('L')

? ? (2)打開后圖像格式為PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=280x200谴仙,

? ? ? dtype為JPEG,mode為RGB切揭;灰度圖則分別為None和mode=L狞甚;

? ? (3)需要使用nd.array()轉換為array格式的矩陣,

? ? (4)保存時再用Image.fromarray()轉換回圖像<PIL.Image.Image image mode=RGB size=280x200廓旬,

? ? ? dtype為None

? ? (5)保存指令為imgname.save('imgsavename.*')哼审。

3.skimage打開和保存圖片:分彩色和灰度圖兩種情況

? ? (1)讀入彩色圖像指令為 io.imread('house.jpg'),

? ? ? 讀入圖片的數據格式為uint8孕豹,三通道RGB格式涩盾,與cv2同

? ? (2)讀入灰度圖像指令為io.imread('house.jpg',as_grey=True),

? ? ? 讀入圖片的數據格式為float,數值在0到1之間

4.plt打開和保存圖片:

? ? (1)讀入指令為plt.imread()励背,

? ? (2)保存指令為plt.savefig('imgsavename.*'),且一定在plt.show()之前春霍;

? ? ? plt.savefig將當前窗口內的內容保存,包括白邊(有無參數設置為不保存白邊叶眉?)址儒。

5.實驗結果:? ?

cv2 open and cvtBGR (shape,dtype):(200, 280, 3),uint8

? ? ? ? ski open? ? ? ? ? ? (shape,dtype):(200, 280, 3),uint8

? ? ? ? plt open? ? ? ? ? ? (shape,dtype):(200, 280, 3),uint8

? ? ? ? pil open? ? ? ? ? ? (size ,dtype):(280, 200),JPEG

? ? ? ? pil open and pil2arr (shape,dtype):(200, 280, 3),uint8

? ? ? ? pil open and arr2pil (size ,dtype):(280, 200),None

? ? ? ? cv2 open and save? ? (shape,dtype):(200, 280, 3),uint8

? ? ? ? pil open and save? ? (shape,dtype):(200, 280, 3),uint8

? ? ? ? ski open and save? ? (shape,dtype):(200, 280, 3),uint8

? ? ? ? plt open and save? ? (shape,dtype):(288, 432, 3),uint8

? ? ? ? cv2 open gray? ? ? ? (shape,dtype):(200, 280),uint8

? ? ? ? pil open gray? ? ? ? (size ,dtype):(280, 200),None

? ? ? ? ski open gray? ? ? ? (shape,dtype):(200, 280),float64

? ? ? ? cv2 open and savegray(shape,dtype):(200, 280),uint8

? ? ? ? pil open and savegray(shape,dtype):(200, 280),uint8

? ? ? ? ski open and savegray(shape,dtype):(200, 280),uint8

總結:

? ? (1)讀入彩圖cv2,plt,ski數據類型為uint8一致,均為三通道uint8(注意cv2是BGR);

? ? ? pil讀入JPEG格式(mode=RGB)且shape不同衅疙,需要轉換為ndarray(shape變化)莲趣,

? ? ? 保存時再轉回圖片,格式為None(shape變化)饱溢。

? ? (2)讀入灰度圖僅對比了cv2,ski和pil喧伞,三者均不同,cv2為uint8,而ski為float64(0和1之間),

? ? ? pil的format為None(mode=L),與彩色圖同操作潘鲫。

? ? (3)保存圖片翁逞,四種方法均為uint8,但是cv2,pil溉仑,ski保存原圖shape不變

? ? ? (注意cv2又存成BGR)挖函,而plt保存窗口且shape改變。


'''

1.cv2打開圖片彼念,顯示后挪圾,用cv2存儲

'''

import cv2

import matplotlib.pyplot as plt

#? cv2讀入和保存彩色圖

img_cv2BGR = cv2.imread('house.jpg')

img_cv2RGB = cv2.cvtColor(img_cv2BGR,cv2.COLOR_BGR2RGB)

cv2.imwrite('house_cv2write.jpg',img_cv2RGB)? ? ? ? ? ? # 圖像占內存略大于原圖

img_cv2write = plt.imread('house_cv2write.jpg')

print(img_cv2BGR.dtype)

print(img_cv2BGR.size)

print(img_cv2BGR.shape)

print(img_cv2BGR)

#? cv2讀入和保存灰度圖

imgray_cv2 = cv2.imread('house.jpg',cv2.IMREAD_GRAYSCALE)

cv2.imwrite('housegray_cv2write.jpg',imgray_cv2)

imgray_cv2write = plt.imread('housegray_cv2write.jpg')

print(imgray_cv2.dtype)

print(imgray_cv2.size)

print(imgray_cv2.shape)

print(imgray_cv2)

'''2.PIL保存'''

from PIL import Image

import numpy as np

#? 用PIL讀入和保存彩色圖,并進行格式轉換以用于不同目的

img_pil = Image.open('house.jpg')? ? ? ? ? ? ? ? ? ? ? # img類逐沙,JPEG格式哲思,mode=RGB

img_pil2arr = np.array(img_pil)? ? ? ? ? ? ? ? ? ? ? ? # 轉成ndarray

img_arr2pil = Image.fromarray(img_pil2arr)? ? ? ? ? ? ? # 矩陣再轉為圖像

img_arr2pil.save('house_pilsave.jpg')? ? ? ? ? ? ? ? ? # PIL保存圖像

img_pilsave = plt.imread('house_pilsave.jpg')

print(img_pil.format)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # JPEG; 'Image' object has no attribute 'shape' and 'dtype'

print(img_pil.size)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # (280, 200)

print(img_pil)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB...

print(img_arr2pil.format)

print(img_arr2pil.size)

print(img_arr2pil)

#? 用PIL讀入和保存灰度圖

imgray_pil = Image.open('house.jpg').convert('L')? ? ? # L為灰度圖,RGB為真彩色,RGBA為加了透明頻道

imgray_pil.save('house_gray_pilsave.jpg')

print(imgray_pil.format)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # None;format為None吩案,mode為L棚赔,注意與彩色圖不同

print(imgray_pil.size)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # (280, 200)

print(imgray_pil)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # <PIL.Image.Image image mode=L size=280x200 at...

imgray_pilsave = plt.imread('house_gray_pilsave.jpg')

'''

3.skimage保存:分彩色和灰度圖兩種情況

'''

from skimage import io

#? ski讀入彩色圖

img_ski = io.imread('house.jpg')

io.imshow(img_ski)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # io.imshow顯示圖像

io.imsave('house_skisave.jpg',img_ski)? ? ? ? ? ? ? ? ? # io.save保存圖像

img_skisave = plt.imread('house_skisave.jpg')? ? ? ? ? # plt讀入圖像

print(img_ski.dtype)

print(img_ski.size)

print(img_ski.shape)

print(img_ski)

#? ski讀入灰度圖

imgray_ski = io.imread('house.jpg',as_grey=True)

print(imgray_ski.dtype)

print(imgray_ski.size)

print(imgray_ski.shape)

print(imgray_ski)

io.imsave('house_gray_ski.jpg',imgray_ski)

imgray_skisave = plt.imread('house_gray_ski.jpg')

'''

4.plt.save:將當前窗口內的內容保存,包括白邊

'''

import matplotlib.pyplot as plt

fig = plt.figure('house')

img_plt = plt.imread('house.jpg')

plt.imshow(img_plt)

plt.savefig('house_pltsave.jpg')? ? ? ? ? ? ? ? ? ? ? ? # 一定在plt.show()之前

plt.show()

img_pltsave = plt.imread('house_pltsave.jpg')

'''

打印圖片格式

'''

#? 分類顯示:方法結果對比

print("cv2 open and cvtBGR? (shape,dtype):{},{}".format(img_cv2RGB.shape,img_cv2RGB.dtype))

print("cv2 open and save? ? (shape,dtype):{},{}".format(img_cv2write.shape,img_cv2write.dtype))

print("cv2 open gray? ? ? ? (shape,dtype):{},{}".format(imgray_cv2.shape,imgray_cv2.dtype))

print("cv2 open and savegray(shape,dtype):{},{}".format(imgray_cv2write.shape,imgray_cv2write.dtype))

print("pil open gray? ? ? ? (size ,dtype):{},{}".format(imgray_pil.size,imgray_pil.format))

print("pil open and savegray(shape,dtype):{},{}".format(imgray_pilsave.shape,imgray_pilsave.dtype))

print("pil open? ? ? ? ? ? (size ,dtype):{},{}".format(img_pil.size,img_pil.format))

print("pil open and pil2arr (shape,dtype):{},{}".format(img_pil2arr.shape,img_pil2arr.dtype))

print("pil open and arr2pil (size ,dtype):{},{}".format(img_arr2pil.size,img_arr2pil.format))

print("pil open and save? ? (shape,dtype):{},{}".format(img_pilsave.shape,img_pilsave.dtype))

print("ski open? ? ? ? ? ? (shape,dtype):{},{}".format(img_ski.shape,img_ski.dtype))

print("ski open and save? ? (shape,dtype):{},{}".format(img_skisave.shape,img_skisave.dtype))

print("ski open gray? ? ? ? (shape,dtype):{},{}".format(imgray_ski.shape,imgray_ski.dtype))

print("ski open and savegray(shape,dtype):{},{}".format(imgray_skisave.shape,imgray_skisave.dtype))

print("plt open? ? ? ? ? ? (shape,dtype):{},{}".format(img_plt.shape,img_plt.dtype))

print("plt open and save? ? (shape,dtype):{},{}".format(img_pltsave.shape,img_pltsave.dtype))

#? 分類顯示:相同操作結果對比

print("cv2 open and cvtBGR? (shape,dtype):{},{}".format(img_cv2RGB.shape,img_cv2RGB.dtype))

print("ski open? ? ? ? ? ? (shape,dtype):{},{}".format(img_ski.shape,img_ski.dtype))

print("plt open? ? ? ? ? ? (shape,dtype):{},{}".format(img_plt.shape,img_plt.dtype))

print("pil open? ? ? ? ? ? (size ,dtype):{},{}".format(img_pil.size,img_pil.format))

print("pil open and pil2arr (shape,dtype):{},{}".format(img_pil2arr.shape,img_pil2arr.dtype))

print("pil open and arr2pil (size ,dtype):{},{}".format(img_arr2pil.size,img_arr2pil.format))

print("cv2 open and save? ? (shape,dtype):{},{}".format(img_cv2write.shape,img_cv2write.dtype))

print("pil open and save? ? (shape,dtype):{},{}".format(img_pilsave.shape,img_pilsave.dtype))

print("ski open and save? ? (shape,dtype):{},{}".format(img_skisave.shape,img_skisave.dtype))

print("plt open and save? ? (shape,dtype):{},{}".format(img_pltsave.shape,img_pltsave.dtype))

print("cv2 open gray? ? ? ? (shape,dtype):{},{}".format(imgray_cv2.shape,imgray_cv2.dtype))

print("pil open gray? ? ? ? (size ,dtype):{},{}".format(imgray_pil.size,imgray_pil.format))

print("ski open gray? ? ? ? (shape,dtype):{},{}".format(imgray_ski.shape,imgray_ski.dtype))

print("cv2 open and savegray(shape,dtype):{},{}".format(imgray_cv2write.shape,imgray_cv2write.dtype))

print("pil open and savegray(shape,dtype):{},{}".format(imgray_pilsave.shape,imgray_pilsave.dtype))

print("ski open and savegray(shape,dtype):{},{}".format(imgray_skisave.shape,imgray_skisave.dtype))

'''

顯示圖像

'''

fig = plt.figure(figsize=(20,10))

plt.axis('off')

ax = fig.add_subplot(241)

ax.imshow(img_cv2RGB)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 用cv2打開原始彩色圖像進行顯示? ?

ax.set_title('Source image')

ax = fig.add_subplot(243)

ax.imshow(img_cv2write)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開cv2保存的彩色圖像進行顯示? ?

ax.set_title('cv2write image')? ? ? ? ? ?

ax = fig.add_subplot(247)

ax.imshow(imgray_cv2write)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開cv2保存的灰度圖像進行顯示

ax.set_title('cv2write gray image')

ax = fig.add_subplot(244)

ax.imshow(img_skisave)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開ski保存的彩色圖像進行顯示

ax.set_title('skisave image')

ax = fig.add_subplot(248)

ax.imshow(imgray_skisave)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開ski保存的灰度圖像進行顯示

ax.set_title('skisave gray image')

ax = fig.add_subplot(242)

ax.imshow(img_pilsave)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開pil保存的彩色圖像進行顯示

ax.set_title('pilsave image')

ax = fig.add_subplot(246)

ax.imshow(imgray_pilsave)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開pil保存的灰度圖像進行顯示

ax.set_title('pilsave gray image')

ax = fig.add_subplot(245)

ax.imshow(img_pltsave)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 打開plt保存的彩色圖像進行顯示

ax.set_title('pltsave image')

plt.savefig('house_save_img_method_compare.jpg')


?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末徘郭,一起剝皮案震驚了整個濱河市靠益,隨后出現的幾起案子,更是在濱河造成了極大的恐慌残揉,老刑警劉巖胧后,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異抱环,居然都是意外死亡壳快,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門镇草,熙熙樓的掌柜王于貴愁眉苦臉地迎上來眶痰,“玉大人,你說我怎么就攤上這事梯啤∈” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵因宇,是天一觀的道長七婴。 經常有香客問我,道長察滑,這世上最難降的妖魔是什么本姥? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮杭棵,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己魂爪,他們只是感情好先舷,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著滓侍,像睡著了一般蒋川。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上撩笆,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天捺球,我揣著相機與錄音,去河邊找鬼夕冲。 笑死氮兵,一個胖子當著我的面吹牛,可吹牛的內容都是我干的歹鱼。 我是一名探鬼主播泣栈,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼弥姻!你這毒婦竟也來了南片?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤庭敦,失蹤者是張志新(化名)和其女友劉穎疼进,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體秧廉,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡伞广,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了定血。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赔癌。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖澜沟,靈堂內的尸體忽然破棺而出灾票,到底是詐尸還是另有隱情,我是刑警寧澤茫虽,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布刊苍,位于F島的核電站,受9級特大地震影響濒析,放射性物質發(fā)生泄漏正什。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一号杏、第九天 我趴在偏房一處隱蔽的房頂上張望婴氮。 院中可真熱鬧斯棒,春花似錦、人聲如沸主经。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽罩驻。三九已至穗酥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間惠遏,已是汗流浹背砾跃。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留节吮,地道東北人抽高。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像课锌,于是被迫代替她去往敵國和親厨内。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容