내 프로그램에서 사용자에게 P6 .ppm 이미지의 파일 이름을 입력하도록 요청하면 내 프로그램이 새로운 파일을 그레이 스케일로 변환하는 P5 .pgm 이미지로 씁니다. 열려있는 이미지에 헤더에 주석이있는 경우를 제외하고는 프로그램이 완벽하게 작동합니다. 내 문제가 내 Main() 또는 GetNum 기능에 있는지 확신 할 수 없습니다. 어떤 도움을 많이 주시면 감사하겠습니다!Python : .ppm 파일을 여는 동안 주석을 건너 뛸 필요가 있음
이
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