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

功能介绍:从一个文件夹中读取图片,获得图片的像素最大值,并记录在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)

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

时间: 2024-07-31 21:17:26

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

[matlab]改变矩阵的大小并保存到txt文件

要完成的任务是,加载一个保存在txt文件中的矩阵, 并把它扩大10倍,并且要再次保存回去 %加载txt文件 >load('Matrix.txt'); %扩大10倍 repmat(Matrix,row column) % 这里的matrix 参数是要对其进行修改的matrix, 其中row是要新建的一个矩阵的行数, 而column是新建矩阵的列数 >Matrix = repmat(Matrix,10,1); % 这个就相当于  将Matrix矩阵 复制了10份,并且是按列排列的, 等同于 Mat

JTextArea内容保存到txt文件上,如何实现换行

解决办法: 如果是采用流的方式一行一行写到文件中,可以把FileWriter换为PrintWriter,然后调用PrintWriter的println()方法. byte fileContent[] = getJTextArea().getText().replaceAll("\n", "\r\n").getBytes();//此处主要是为了在Windows中实现换行操作 FileWriter fw=new FileWriter(file); String str=

np.savetxt()——将array保存到txt文件,并保持原格式

问题:1.如何将array保存到txt文件中?2.如何将存到txt文件中的数据读出为ndarray类型? 需求:科学计算中,往往需要将运算结果(array类型)保存到本地,以便进行后续的数据分析. 解决:直接用numpy中的方法. 1:numpy.savetxt(fname,X):第一个参数为文件名,第二个参数为需要存的数组(一维或者二维). 2.numpy.loadtxt(fname):将数据读出为array类型. 示例 >>> import numpy as np >>&

php下载图片生成原始路径 并将下载过的图片路径保存到txt文件中

<?php //header("content-Type: text/html; charset=utf-8"); @ini_set('date.timezone', 'Asia/Shanghai'); @set_time_limit(0); @ini_set('memory_limit','100M'); $headurl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]; $headurl

【转】delphi 保存到txt文件

procedure TForm1.btn1Click(Sender: TObject); var astr: string; sList: TStrings; path: string; begin sList := TStringList.Create; path := 'C:\a\b.txt'; try if not DirectoryExists(ExtractFilePath(path)) then begin ForceDirectories(ExtractFilePath(path)

osql执行数据库查询命令并保存到txt文件

osql -Usa -P123 -d AppBox -Q "select * from Menus where sortindex > 1000" -o e:\xxx.txt osql -Usa -P123 -dAppBox -Q "insert into Menus (Name,ImageUrl,NavigateUrl,Remark,SortIndex,ParentID,ViewPowerID) values('系统管理','~/res/icon/applicatio

Opencv读取图片像素值并保存为txt文件

#include <opencv2/opencv.hpp>#include<vector>#include <fstream> using namespace std;using namespace cv; int main(int argc, char* argv[]){ const char* imagename = "2.jpg"; //从文件中读入图像 Mat img = imread(imagename); ofstream outfile

C#从数据库读取数据到DataSet并保存到xml文件

using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.IO; public class TestWriteXML { public static void Main() { String strFileName = c:/temp/out.xml; SqlConnection conn = new SqlConnection(server=localhost;uid

java将已有的字符串保存到txt文件中

python网络爬虫-通过互联网采集 RMQ算法的学习(区间最值问题)NYOJ119士兵杀敌(三) JAVA常用设计模式 Java多线程知识要点 9fw谀泄牡http://p.baidu.com/itopic/main/center?uid=15fe616263346630323931e4ac&qizj夹餐智p7k9e6律犹媒http://p.baidu.com/itopic/main/center?uid=6bfe616263386334303438e5ac&8q90潦卓嚷v5uuq9轮