私のプログラムは、ユーザーに 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 関数に問題がありますか?
デフォルト 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