在windows上使用Python读取和写入PGM文件
我的脚本打开一个pgm(P5)文件,读取它并将其复制到一个新的pgm文件,最后添加一行。结果是相同的图像 我的问题是,该脚本在Linux上运行良好,但当我在windows 10上使用它时,输出的pgm出现缺陷,只有一些白色垂直线 请注意,我还不允许上传图片在windows上使用Python读取和写入PGM文件,python,Python,我的脚本打开一个pgm(P5)文件,读取它并将其复制到一个新的pgm文件,最后添加一行。结果是相同的图像 我的问题是,该脚本在Linux上运行良好,但当我在windows 10上使用它时,输出的pgm出现缺陷,只有一些白色垂直线 请注意,我还不允许上传图片 #!/usr/bin/python import sys import os # End of function ################################################# def parseA
#!/usr/bin/python
import sys
import os
# End of function
#################################################
def parseArgs( argv):
expectedInputLen = 3
if( len( argv) != expectedInputLen):
print "Failure: bad number of input argument = %d (expectedInputLen=%d)\n" % (len(argv), expectedInputLen)
print ("usage %s sensorID pgmFn \n")
sys.exit(1)
argI = 1
sensorID = argv\[argI\]; argI = argI + 1
pgmFn = argv\[argI\]; argI = argI + 1
if( pgmFn\[-4:\] != '.pgm'):
print("Failure: not a pgm file\n")
sys.exit(1)
print "input: "
print "\t sensorID = <%s>" % sensorID
print "\t pgmFn = <%s>" % pgmFn
assert( argI == len( argv))
return { 'sensorID':sensorID,
'pgmFn':pgmFn}
# End of function
#################################################
def implantSID( pgmFileIn, pgmFileOut, sensorID):
#read header and asserts
implantedStr = "sensorID" + sensorID + "sensorID"
assert( pgmFileIn.read(2) == 'P5')
pgmFileIn.read(1)
width = int(pgmFileIn.read(4))
assert( width == 1280)
pgmFileIn.read(1)
height = pgmFileIn.read(3)
if( height != '960'):
height = height + pgmFileIn.read(1)
assert( height == '1080')
height = int(height)
pgmFileIn.read(1)
depth = int(pgmFileIn.read(5))
assert depth <= 65535
pgmFileIn.read(1)
pgmFileOut.write('P5 ' + str(width) + ' ' + str(height) + ' ' + str(depth) + '\n')
for row in range(height-1):
for col in range(width):
pgmFileOut.write(pgmFileIn.read(2))
for col in range(width-len(implantedStr)):
pgmFileOut.write(pgmFileIn.read(2))
for char in implantedStr:
pgmFileOut.write('\0'+char)
# End of function
#################################################
def main( argv):
'''Main entry point, start the application's main loop.'''
params = parseArgs( argv)
sensorID = params\['sensorID'\]
pgmFn = params\['pgmFn'\]
pgmFileIn = open( pgmFn, 'r')
pgmFileOut = open( pgmFn.replace(".pgm", "_SID.pgm"), 'w')
implantSID( pgmFileIn, pgmFileOut, [enter image description here][1]sensorID)
pgmFileIn.close()
pgmFileOut.close()
# End of function
#################################################
if __name__ == "__main__":
main( sys.argv)
# End of function
#################################################
#/usr/bin/python
导入系统
导入操作系统
#功能结束
#################################################
def解析args(argv):
预期输入=3
如果(len(argv)!=expectedInputLen):
打印“失败:输入参数数目错误=%d(expectedInputLen=%d)\n”%(len(argv),expectedInputLen)
打印(“使用%s传感器ID pgmFn\n”)
系统出口(1)
argI=1
sensorID=argv\[argI\];argI=argI+1
pgmFn=argv\[argI\];argI=argI+1
如果(pgmFn\[-4:\]!='.pgm'):
打印(“失败:不是pgm文件\n”)
系统出口(1)
打印“输入:”
打印“\t sensorID=“%sensorID
打印“\t pgmFn=“%pgmFn”
断言(argI==len(argv))
返回{'sensorID':sensorID,
“pgmFn”:pgmFn}
#功能结束
#################################################
def植入SID(pgmFileIn、pgmFileOut、sensorID):
#读取标题并断言
implantedStr=“传感器ID”+传感器ID+“传感器ID”
断言(pgmFileIn.read(2)='P5')
pgmFileIn.read(1)
宽度=int(pgmFileIn.read(4))
断言(宽度=1280)
pgmFileIn.read(1)
高度=pgmFileIn.read(3)
如果(高度!=“960”):
高度=高度+pgmFileIn.read(1)
断言(高度='1080')
高度=整数(高度)
pgmFileIn.read(1)
深度=int(pgmFileIn.read(5))
断言深度