【转载】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阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容