首页 > 其他 > 详细

读取图片的最大值,并保存到txt文件

时间:2018-03-08 20:04:06      阅读:238      评论:0      收藏:0      [点我收藏+]

功能介绍:从一个文件夹中读取图片,获得图片的像素最大值,并记录在txt文件中保存,同时应保存对应的文件名。
特别说明:图片文件为png格式,8bit的单层图(即灰度图),不确定此代码是否适用于其他文件类型,未做测试。

import numpy as np
import os
from PIL import Image

def get_path_list(file_dir, fname):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in sorted(files):    # 遍历文件目录下每一个文件
            if fname in file:  # 判断是否包含指定字符串
                L.append(os.path.join(root, file))

if __name__ == '__main__':
    dir = './sparse'  # 文件夹名
    dirs = os.listdir(dir)

    sparse_data_file = os.path.join('./depth_errors.txt')

    for dir1 in dirs:

        imgpath = dir + '/' +dir1

        image = Image.open(imgpath)
        image_max = np.max(image)
        image_min = np.min(image)

        message = 'image: %s, max: %s\n' % (dir1, image_max)

        with open(sparse_data_file, "a") as file:
            file.write('%s' % message)

        print(message)

读取图片的最大值,并保存到txt文件

原文:https://www.cnblogs.com/huangtao36/p/8530438.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!