【发布时间】:2014-10-18 22:24:21
【问题描述】:
我的程序要求用户输入 P6 .ppm 图像的文件名,然后我的程序将新文件作为 P5 .pgm 图像写入并将其转换为灰度。 我的程序运行良好,除非正在打开的图像在标题中有 cmets。我不确定我的问题是在我的 Main() 还是 GetNum 函数中。非常感谢任何帮助!
我的 main 开头是这样的
fileInput = raw_input("Enter the name of the original file including .ppm at the end: ")#P6 = .ppm
fileOutput = raw_input("Enter the file name for the new picture including .pgm at the end: ")#P5 = .pgm
readFile = open(fileInput, "rb")
writeFile = open(fileOutput, 'wb')
magicNumber1 = readFile.read(1)#MagicNumber 1 and 2 grabs the first two bytes for the header, which should be P6
magicNumber2 = readFile.read(1)
或者我的问题是否存在于我的 GetNum 函数中?
def GetNum(f):
currentChar = f.read(1) #Reads through the file 1 byte at a time
while currentChar.isdigit() == False:
while currentChar.isspace(): #Keep reading if the current character is a space
currentChar = f.read(1)
if currentChar == "#": #If a comment or new line keep reading
while currentChar != "\n":
currentChar = f.read(1)
num = currentChar
while currentChar.isdigit(): #If current character is a digit, add it onto Num
currentChar = f.read(1)
num = num + currentChar
num = num.strip()
return num
【问题讨论】:
标签: python header comments grayscale ppm