File Processing by Python

这篇博客介绍了如何使用Python进行文件处理,包括读取文件并计算ASCII值。同时,讲解了如何计算峰值信噪比(PSNR),用于衡量图像处理后的失真程度。提供了多个示例和参考资料链接,帮助理解文件操作和类型转换,并警告了使用read(1)可能导致的效率问题。
摘要由CSDN通过智能技术生成

Go through all the file in destination path:

import os
import sys

def GetFileList(dir, fileList):
    newDir = dir
    if os.path.isfile(dir):
        fileList.append(dir.decode('gbk'))
    elif os.path.isdir(dir):  
        for s in os.listdir(dir):
            newDir=os.path.join(dir,s)
            GetFileList(newDir, fileList)  
    return fileList
def MyGetFileList(dir, filelist):
    if os.path.isdir(dir):
        for s in os.listdir(dir):
            if re.match(r"([\s\S]*)\.yuv", s):
                filelist.append(os.path.join(dir, s))
    return filelist 

ValueError: invalid literal for int() with base 10
When read file, we just want to calculate the ASCII value to achieve our goal. Here MAP is very important.

def CalculatePSNRForTwoYUVs(f1, f2, dir1, dir2):                
        d1 = dir1 + '\\' + f1
        d2 = dir2 + '\\' + f2
        if (os.path.isfile(d1) and os.path.isfile(d2)):
            file1Object = open(d1, 'r')
            file2Object = open(d2, 'r')
            while True:
                chunk1 = file1Object.read(1)
                chunk2 = file2Object.read(1)
                if chunk1 == '':
                    break
                #print string.atoi(chunk1)  
                ck1 = map(ord, chunk1)
                print ck1[0]
def CalculatePSNRForTwoYUVs(f1, f2, dir1, dir2):                
        d1 = dir1 + '\\' + f1
        d2 = dir2 + '\\' + f2
        sumOfPSNR = 0
        if (os.path.isfile(d1) and os.path.isfile(d2)):
            file1Object = open(d1, 'r')
            file2Object = open(d2, 'r')
            print d1
            print d2
            while True:
                chunk1 = file1Object.read(1)
                chunk2 = file2Object.read(1)
                if chunk1 == '':
                    break
                if chunk2 == '':
                    break
                ck1 = map(ord, chunk1)
              
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值