Python讀取圖片的方法詳解
一、Python學習兩大道具
1. dir()工具
作用:支持打開package,看到里面的工具函數(shù)
示例:
(1) 輸出torch庫包含的函數(shù)
dir(torch)
(2) 輸出torch.AVG函數(shù)中的參數(shù)
dir(torch.AVG)
2. help()工具
作用:說明書,查看庫中函數(shù)某個參數(shù)的說明或使用方法
示例:
(1) 輸出torch庫中AVG函數(shù)的AVG參數(shù)使用方法
help(torch.AVG.AVG)
二、Pytorch讀取圖像數(shù)據(jù)
0. 寫在前面
(1)PIL庫中Image函數(shù)的基本使用方法
PIL的安裝:win+r → cmd → 選擇環(huán)境 → pip install Pillow
使用方法:
from PIL import Image #選擇圖像路徑 #注意:在復制圖像路徑后,在windows環(huán)境下,需要將\變?yōu)閈\ img_path="E:\\Desktop\\hymenoptera_data\\hymenoptera_data\\train\\ants\\0013035.jpg" #打開并標識給定的圖像文件。 #image.open()是一個懶惰的操作;此函數(shù)可識別文件,但文件保持打開狀態(tài),直到嘗試處理數(shù)據(jù)(或調用load()方法),才會從文件中讀取實際圖像數(shù)據(jù)。 img=image.open(img_path) #輸出圖像大小 print(img.size) #打開圖像(指的是直接根據(jù)該地址打開一個窗口顯示這個圖像) img.show()
(2)os庫中函數(shù)的基本使用方法
os.path.join(path1, path2, ... ,pathn):合并路徑,在一定程度上可以避免因python語法問題,造成的\t或\n之類的錯誤。
比如說,圖片路徑為:”hymenoptera_data\hymenoptera_data\train“,如果直接輸入路徑,那么會出現(xiàn)以下結果:
path="hymenoptera_data\hymenoptera_data\train" print(path) ''' [Run] hymenoptera_data\hymenoptera_data rain '''
此時的處理方法有兩種:
方法一:在\后面加個\
path="hymenoptera_data\\hymenoptera_data\\train" print(path) ''' [Run] hymenoptera_data\hymenoptera_data\train '''
方法二:os.path.join
import os path1="hymenoptera_data\hymenoptera_data" path2="train" path=os.path.join(path1,path2) print(path) ''' [Run] hymenoptera_data\hymenoptera_data\train '''
os.listdir(path):將path中包含的圖片名稱變?yōu)橐粋€列表。
比如說,路徑為”hymenoptera_data\hymenoptera_data\train\ants“的文件夾中有0013035.jpg、5650366_e22b7e1065.jpg、6240329_72c01e663e.jpg三張圖片,那么此時有:
import os path="hymenoptera_data\\hymenoptera_data\\train\\ants" path_list=os.listdir(path) print(path_list) ''' [Run] ['0013035.jpg', '5650366_e22b7e1065.jpg', '6240329_72c01e663e.jpg'] '''
1. Dataset
作用:提供一種方式去獲取數(shù)據(jù)及其label
功能:
- 如何獲取每一個數(shù)據(jù)及其label
- 告訴我們總共有多少數(shù)據(jù)(作用:神經(jīng)網(wǎng)絡要對同一個數(shù)據(jù)迭代多次,只有當我們知道總共有多少個數(shù)據(jù),訓練的時候我們才知道要訓練多少次,才能把這個數(shù)據(jù)集迭代完然后進行下一次的迭代)
詳解:
- getitem:獲取數(shù)據(jù)對應的label
- len:返回數(shù)據(jù)的size
使用示例:
數(shù)據(jù)下載地址:百度網(wǎng)盤 請輸入提取碼 (baidu.com)
提取碼:2hby
(1)讀取、簡單處理圖像數(shù)據(jù)
from torch.utils.data import Dataset from PIL import Image import os class MyData(Dataset): #參數(shù)說明: #root_dir:數(shù)據(jù)集的路徑,如"E:\Desktop\hymenoptera_data\hymenoptera_data\train"(最好還是都加上\\,像其中的\train由于\t的存在會導致錯誤) #label_dir:數(shù)據(jù)的標簽,如"ants" def __init__(self,root_dir,label_dir): self.root_dir=root_dir self.label_dir=label_dir # 使用os.path.join的方法是為了避免\和\\錯誤的問題 # self.path='E:\\Desktop\\hymenoptera_data\\hymenoptera_data\\train\\ants' self.path=os.path.join(self.root_dir,self.label_dir) #將文件夾內所有圖片的地址變成一個列表,并按先后順序排列 self.img_path=os.listdir(self.path) #輸入:圖像對應的索引 #返回:idx索引對應的打開并標識過后的圖像文件img;圖像對應的標簽label def __getitem__(self,idx): img_name=self.img_path[idx] #根據(jù)索引idx,讀取列表self.img_path中的圖像名稱 img_item_path=os.path.join(self.root_dir,self.label_dir,img_name) #讀取該索引對應圖像的存儲路徑 img=Image.open(img_item_path) #打開并標識給定的圖像文件 label=self.label_dir #label=數(shù)據(jù)標簽,在該數(shù)據(jù)集中為ant或bee return img,label #返回圖像數(shù)據(jù)集的長度,也就是說所讀取的數(shù)據(jù)集中有多少張圖片 def __len__(self): return len(self.img_path) root_dir="E:\\Desktop\\hymenoptera_data\\hymenoptera_data\\train" ants_label_dir="ants" bees_label_dir="bees" ants_dataset=MyData(root_dir,ants_label_dir) bees_dataset=MyData(root_dir,ants_label_dir) img,label=ants_dataset[0] print(img,label) #根據(jù)索引,獲取標識過后的圖像img,以及對應的標簽 img.show() #顯示對應索引的圖像 train_dataset=ants_dataset+bees_dataset #將兩個數(shù)據(jù)集進行拼接 print(len(ants_dataset),len(bees_dataset),len(train_dataset))
(2)存儲圖像數(shù)據(jù)
首先新建一個文件夾,在該文件夾中:.txt文件名表示.jpg的圖片名稱,.txt文件中存儲的數(shù)據(jù)為對應圖像的label。用下面代碼將文件名、label實現(xiàn)寫入:
import os root_dir="E:\\Desktop\\hymenoptera_data\\hymenoptera_data\\train" target_dir="ants" img_path=os.listdir(os.path.join(root_dir,target_dir)) #將target_dir文件夾中的圖片名稱存到img_path的列表中 label=target_dir #標簽為target_dir(根據(jù)實際情況讀取標簽) out_dir="ants_label" #輸出圖片的地址 for i in img_path: #遍歷每一張圖片的名字 file_name=i.split(".jpg")[0] #除去.jpg后綴,取出圖片名字 with open(os.path.join(root_dir,out_dir,"{}.txt".format(file_name)),'w') as f: #以圖片名命名.txt文件 f.write(label) #將相應圖片的標簽,寫入.txt文件中
2. Dataloder
作用:為后面的網(wǎng)絡提供不同的數(shù)據(jù)形式(對其中幾條數(shù)據(jù)進行打包)
到此這篇關于Python讀取圖片的方法詳解的文章就介紹到這了,更多相關Python讀取圖片內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決pyecharts運行后產(chǎn)生的html文件用瀏覽器打開空白
這篇文章主要介紹了解決pyecharts運行后產(chǎn)生的html文件用瀏覽器打開空白,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03Python實現(xiàn)字符串模糊匹配的兩種實現(xiàn)方法
本文主要介紹了Python實現(xiàn)字符串模糊匹配的兩種實現(xiàn)方法,Python中通過re.search()方法實現(xiàn),對于首位起始的內容匹配,也可通過re.match()方法實現(xiàn),感興趣的可以了解一下2023-11-11