我知道没有足够的参数来格式化字符串,有什么建议吗?它在 else f.write 行 (52) 中。这是取平均像素值并将其替换为第二张图像。如您所见,该文件读取两个图像并写入它们。在写入过程中,我收到一条错误消息,指出没有足够的参数来格式化字符串。
def main():
f = open("BoardHappy.ppm","r")
lines = f.readlines()
kinds = lines[0]
size=lines[1].split()
width=int(size[0])
height=int(size[1])
depth = int(lines[2])
red=[]
green=[]
blue=[]
for i in range(3, len(lines)):
temp = lines[i].split()
for j in range(0, len(temp) ,3):
red.append(int(temp[j]))
green.append(int(temp[j+1]))
blue.append(int(temp[j+2]))
f.close()
f = open("Godzilla.ppm","r")
lines = f.readlines()
kinds = lines[0]
size=lines[1].split()
width=int(size[0])
height=int(size[1])
depth = int(lines[2])
red1=[]
green1=[]
blue1=[]
for i in range(3, len(lines)):
temp = lines[i].split()
for j in range(0, len(temp), 3):
red1.append(int(temp[j]))
green1.append(int(temp[j+1]))
blue1.append(int(temp[j+2]))
f.close()
f = open("lab5.ppm","w")
f.write("P3 \n")
width=720
height=540
f.write("%d %d \n"%(width,height))
f.write("255 \n")
for i in range(width*height):
new = (red[i]+green[i]+blue[i])/3.0
if new >=100:
f.write("%d %d %d/n"% red[i], green[i],blue[i])
else:
f.write("%d %d %d/n"% red1[i], green1[i], blue1[i])
f.close()
main()