99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python中讀取圖片的6種方式
python中讀取圖片的6種方式
2020-11-06
收藏

作者:小伍哥

來源:AI入門學(xué)習(xí)

python進(jìn)行圖片處理,第一步就是讀取圖片,這里給大家整理了6種圖片的讀取方式,并將讀取的圖片裝換成numpy.ndarray()格式。

首先需要準(zhǔn)備一張照片,假如你有女朋友的話,可以用女朋友的,沒有的話,那還學(xué)啥python,趕緊找對象去吧!

圖片路徑:dirpath = "C:/Users/wuzhengxiang/Desktop/beatgirl.jpg"    

一、OpenCV讀取圖片

OpenCV讀取的圖片,直接就是numpy.ndarray格式,無需轉(zhuǎn)換

import cv2
img_cv   = cv2.imread(dirpath)#讀取數(shù)據(jù)
print("img_cv:",img_cv.shape)
img_cv: (1856, 2736, 3)
print("img_cv:",type(img_cv))
img_cv: numpy.ndarray'="">
  #看下讀取的數(shù)據(jù)怎么樣
img_cv
array([[[  0,   3,   0], 
        [ 11,  20,  17], 
        ...,
        [  5,  23,  16]], 
        [[  0,   2,   0],  
         ..., 
         [  5,  23,  16]]]

 二、PIL讀取圖片

PIL讀取的圖片并不是直接的numpy.ndarray格式,需要進(jìn)行轉(zhuǎn)換

from PIL import Image
import numpy as np
img_PIL = Image.open(dirpath)#讀取數(shù)據(jù)
print("img_PIL:",img_PIL)
img_PIL: 
print("img_PIL:",type(img_PIL))
img_PIL: 
#將圖片轉(zhuǎn)換成np.ndarray格式
img_PIL = np.array(img_PIL)
print("img_PIL:",img_PIL.shape)
img_PIL: (1856, 2736, 3)
print("img_PIL:",type(img_PIL))
img_PIL: numpy.ndarray<="" pre="">

三、keras讀取圖片

keras深度學(xué)習(xí)的框架,里面也是內(nèi)置了讀取圖片的模塊,該模塊讀取的也不是數(shù)組格式,需要進(jìn)行轉(zhuǎn)換。

from keras.preprocessing.image import array_to_img, img_to_array
load_imgimg_keras = load_img(dirpath)#讀取數(shù)據(jù)
print("img_keras:",img_keras)
img_keras: 
print("img_keras:",type(img_keras))
img_keras: 
#使用keras里的img_to_array()
img_keras = img_to_array(img_keras)
print("img_keras:",img_keras.shape)
img_keras: (1856, 2736, 3)
print("img_keras:",type(img_keras))
img_keras: numpy.ndarray'="">
#可以使用使用np.array()進(jìn)行轉(zhuǎn)換
mg_keras= np.array(img_keras)

 四、skimage讀取圖片

scikit-image是基于scipy的一款圖像處理包,它將圖片作為numpy數(shù)組進(jìn)行處理,讀取的數(shù)據(jù)正好是numpy.ndarray格式。

import skimage.io as io
img_io = io.imread(dirpath)#讀取數(shù)據(jù)
print("img_io :",img_io .shape)
img_io : (1856, 2736, 3)
print("img_io :",type(img_io ))
img_io : numpy.ndarray'<="" pre="">

五、matplotlib.image讀取圖片

利用matplotlib.image讀取的圖片,直接就生成了數(shù)組格式

import matplotlib.image as mpig

img_mpig = mpig.imread(dirpath)#讀取數(shù)據(jù)
print("img_mpig :",img_mpig .shape)
img_mpig : (1856, 2736, 3)
print("img_mpig :",type(img_mpig ))
img_mpig : numpy.ndarray'<="" pre="">

六、matplotlib.pyplot讀取圖片

利用matplotlib.pyplot讀取的圖片,同樣也是直接就生成了數(shù)組格式

import matplotlib.pyplot as plt
img_plt = plt.imread(dirpath)
print("img_plt :",img_plt .shape)
img5: (1856, 2736, 3)
print("img_plt :",type(img_plt ))
img5: numpy.ndarray'="">

七、顯示讀取的圖片

同樣,使用matplotlib 包可以打印出來讀取的照片,要打印上述案例中讀取的照片,只需要下面兩行代碼就行了。

plt.imshow(img_plt , cmap=plt.cm.binary)

plt.show()       

圖片三通道的,打印其中一個通道

plt.imshow(img_plt[:,:,1] , cmap=plt.cm.binary)

plt.show()        

當(dāng)然,我們可以隨便構(gòu)造一個數(shù)組,可以顯示出來

digit  =  [[135,26,33,12],[14,27,43,190],[120,124,134,205]]

plt.imshow(digit, cmap=plt.cm.binary)

plt.show()


——熱門課程推薦:

想學(xué)習(xí)PYTHON數(shù)據(jù)分析與金融數(shù)字化轉(zhuǎn)型精英訓(xùn)練營,您可以點(diǎn)擊>>>“人才轉(zhuǎn)型”了解課程詳情;

想從事業(yè)務(wù)型數(shù)據(jù)分析師,您可以點(diǎn)擊>>>“數(shù)據(jù)分析師”了解課程詳情;

想從事大數(shù)據(jù)分析師,您可以點(diǎn)擊>>>“大數(shù)據(jù)就業(yè)”了解課程詳情;

想成為人工智能工程師,您可以點(diǎn)擊>>>“人工智能就業(yè)”了解課程詳情;

想了解Python數(shù)據(jù)分析,您可以點(diǎn)擊>>>“Python數(shù)據(jù)分析師”了解課程詳情;

想咨詢互聯(lián)網(wǎng)運(yùn)營,你可以點(diǎn)擊>>>“互聯(lián)網(wǎng)運(yùn)營就業(yè)班”了解課程詳情; 

想了解更多優(yōu)質(zhì)課程,請點(diǎn)擊>>>

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }