python讀取圖片并修改格式與大小的方法
更新時間:2018年07月24日 15:08:37 作者:alwaysPractice
這篇文章主要為大家詳細介紹了python讀取圖片并修改格式與大小的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python讀取圖片并修改文件大小的具體代碼,供大家參考,具體內容如下
# 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) #想調整的大小
cv2.imwrite(r'./img2/'+str(file),newImg) # 寫入文件地址
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在Python dataframe中出生日期轉化為年齡的實現(xiàn)方法
這篇文章主要介紹了在Python dataframe中出生日期轉化為年齡的實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10
python中翻譯功能translate模塊實現(xiàn)方法
在本篇文章中小編給各位整理了一篇關于python中翻譯功能translate模塊實現(xiàn)方法,有需要的朋友們可以參考下。2020-12-12
Python上下文管理器類和上下文管理器裝飾器contextmanager用法實例分析
這篇文章主要介紹了Python上下文管理器類和上下文管理器裝飾器contextmanager用法,結合實例形式分析了上下文管理器類定義、使用、sqlalchemy實現(xiàn)數(shù)據(jù)庫的自動提交和回滾相關操作技巧,需要的朋友可以參考下2019-11-11
python3 使用openpyxl將mysql數(shù)據(jù)寫入xlsx的操作
這篇文章主要介紹了python3 使用openpyxl將mysql數(shù)據(jù)寫入xlsx的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

