python讀取圖片并修改格式與大小的方法
更新時(shí)間:2018年07月24日 15:08:37 作者:alwaysPractice
這篇文章主要為大家詳細(xì)介紹了python讀取圖片并修改格式與大小的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了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) # 寫(xiě)入文件地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python pandas寫(xiě)入excel文件的方法示例
這篇文章主要介紹了python pandas寫(xiě)入excel文件的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python有關(guān)Unicode UTF-8 GBK編碼問(wèn)題詳解
本文主要介紹了Python有關(guān)Unicode UTF-8 GBK編碼問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
在python中寫(xiě)個(gè)自定義數(shù)據(jù)包協(xié)議的打包和解包測(cè)試
這篇文章主要介紹了在python中寫(xiě)個(gè)自定義數(shù)據(jù)包協(xié)議的打包和解包測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

