python讀取圖片并修改格式與大小的方法
更新時間:2018年07月24日 15:08:37 作者:alwaysPractice
這篇文章主要為大家詳細介紹了python讀取圖片并修改格式與大小的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python讀取圖片并修改文件大小的具體代碼,供大家參考,具體內(nèi)容如下
# Author:NDK # -*- coding:utf-8 -*- from PIL import Image import os import cv2 import numpy as np import glob # old_dir = './test/' # def read_image(cwd, newpath): # for roots, dirs, files in os.walk(cwd): # print(dirs) # for i in dirs: # print(i) # os.chdir(cwd + i) # for pic in glob.glob('*.png'): # _, image = pic.split('_') # img = image.split('.')[0] # print(img) # if len(img) != 0: # if int(img) % 2 != 0: # im = Image.open(pic) # im.save(newpath + i + '/' + pic) # read_image('./num/','./new_img/') # for i in range(10): root_path = r"/test/9/" #操作文件路徑 print(root_path) # dir = root_path+"images"+"/" dir = root_path count = 0 for root,dir,files in os.walk(dir): for file in files: srcImg = cv2.imread(root_path+"/"+str(file)) img = Image.open(root_path+"/"+str(file)) print(root_path+str(file)) newImg = img.resize((50, 50), Image.BILINEAR) #想調(diào)整的大小 cv2.imwrite(r'./img2/'+str(file),newImg) # 寫入文件地址
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python有關(guān)Unicode UTF-8 GBK編碼問題詳解
本文主要介紹了Python有關(guān)Unicode UTF-8 GBK編碼問題詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08在python中寫個自定義數(shù)據(jù)包協(xié)議的打包和解包測試
這篇文章主要介紹了在python中寫個自定義數(shù)據(jù)包協(xié)議的打包和解包測試,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09