python读取图片并修改格式与大小的方法
作者:alwaysPractice 时间:2021-05-13 09:13:25
本文实例为大家分享了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) # 写入文件地址
来源:https://blog.csdn.net/jjjndk1314/article/details/79749752
标签:python,读取图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一列保存多个ID(将多个用逗号隔开的ID转换成用逗号隔开的名称)
2012-08-21 10:37:37
心理模型
2009-05-17 13:45:00
![](https://img.aspxhome.com/file/UploadPic/20095/17/2009-05-12_204505-34s.png)
pytorch中Schedule与warmup_steps的用法说明
2023-07-07 00:18:14
python实现QQ邮箱/163邮箱的邮件发送
2022-12-12 14:07:15
![](https://img.aspxhome.com/file/2023/2/92462_0s.png)
Python多线程即相关理念详解
2021-12-07 12:04:47
Django+Celery实现定时任务的示例
2023-07-27 19:48:18
![](https://img.aspxhome.com/file/2023/4/61704_0s.png)
Python使用剪切板的方法
2022-01-25 02:17:39
python os模块简单应用示例
2021-12-20 13:36:48
web开发以HTML为中心
2008-02-13 08:14:00
![](https://img.aspxhome.com/file/UploadPic/20082/13/200821382345331s.png)
python+Matplotlib 绘制带置信区间的折线图
2022-04-29 06:23:40
![](https://img.aspxhome.com/file/2023/0/80390_0s.jpg)
MySQL Dump/Restore
2010-10-14 13:49:00
如何基于Python按行合并两个txt
2021-03-19 08:24:41
![](https://img.aspxhome.com/file/2023/5/104905_0s.png)
在JScript中使用ADODB.Stream判断文件编码
2008-06-08 13:03:00
iframe框架用JavaScript子页面控制父页面
2009-01-19 13:43:00
在Python中使用M2Crypto模块实现AES加密的教程
2022-09-29 17:43:59
python爬取youtube视频的示例代码
2021-10-02 16:25:26
![](https://img.aspxhome.com/file/2023/9/92499_0s.png)
Python读取txt文件数据的方法(用于接口自动化参数化数据)
2023-12-28 03:21:52
![](https://img.aspxhome.com/file/2023/9/66419_0s.png)
Python应用实现双指数函数及拟合代码实例
2023-04-14 18:17:14
![](https://img.aspxhome.com/file/2023/0/95270_0s.png)
解决python通过cx_Oracle模块连接Oracle乱码的问题
2023-04-30 23:25:45
详解Python如何使用并发模型编程
2021-10-30 06:39:23