在Windows

时间:2017-07-12 09:47:47

标签: python

我的脚本打开一个pgm(P5)文件读取它并将其复制到一个新的pgm文件,最后添加一行,结果是相同的图像 我的问题是该脚本在Linux上工作正常但是当我在Windows 10上使用它时输出的pgm是缺陷的,只是一些白色的垂直线而不是

请注意,我暂时不能上传图片

 #!/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

#################################################

0 个答案:

没有答案