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)       #  写入文件地址
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值