【python遍历文件夹中的所有图像(按名称顺序读取)、将生成的新图像存入本地文件夹】

python遍历文件夹中的所有图像(按名称顺序读取)、将生成的新图像存入本地文件夹

0. 前言

高效科研工具系列的第一集,主要讲如何使用chatGPT及各类chatGPT衍生网站进行科研,可以戳👉这里
高效科研工具系列的第二集,主要讲如何使用newBing进行高效阅读paper及检索,可以戳👉这里
高效科研工具系列的第三集,介绍如何使用arXiv网站进行高效的检索和阅读pape,可以戳👉这里

1. python遍历文件夹中的所有图像

## 遍历一个文件夹下的所有图像 
def bianli_pics(path):
	import os
    img_folder = path
    img_list = [os.path.join(nm) for nm in os.listdir(img_folder) if nm[-3:] in ['jpg', 'png', 'gif']]
    ## print(img_list) 将所有图像遍历并存入一个列表
    ## ['test_14.jpg', 'test_15.jpg', 'test_9.jpg', 'test_17.jpg', 'test_16.jpg']
    for i in img_list:
          
        path=os.path.join(path,i)
        ## print(path)
        ## ./input/test_14.jpg
		## ./input/test_15.jpg
        image = cv2.imread(path). ## 逐个读取
if __name__=="__main__":
	path="./input"
	bianli_pics(path)

2. python遍历文件夹中的所有图像(按照文件名称顺序)

# -*- coding: utf-8 -*-
import os
base_path = r'./test_pics'
files = os.listdir(base_path)
files.remove('.DS_Store') ## Mac系统中可能会存在.DS_Store,提前将其删除
files.sort(key=lambda x: int(x.split('.')[0])) ## 使用切片将图片名称单独切开
for path in files:
    full_path = os.path.join(base_path, path)
    # print(full_path)
    with open(full_path) as fp:
        data = fp.read()
        print(data)
 
 

3. 将生成的图像保存至本地文件夹

	import cv2,os
	def save_2_local(image):
		base_name=os.path.basename(image) ## 获取图像的后缀名称
		new_image_path="./output/"+base_name   # 要存入的新路径和名称 >>basename,test_3.jpg
	    cv2.imwrite(new_image_path, image)  ## 存入的图像image
  • 14
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值