我的程序要求用户输入 P6 .ppm 图像的文件名,然后我的程序将一个新文件作为 P5 .pgm 图像写入并将其转换为灰度。我的程序运行良好,除非正在打开的图像在标题中有注释。我不确定我的问题是在我的 Main() 还是我的 GetNum 函数中。任何帮助深表感谢!
我的主要开始看起来像这样
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