while True:

プログラミング演習(備瀬 竜馬)

第15回

小問番号はコメント(#)を参照
with open("test.ppm", mode ="rb") as f:
    output = open("output.ppm", mode="wb")
    file_line = f.readline()

    # ppm check
    if b"P6" not in file_line:
        exit("No PPM Files Error")
    else:
        output.write(file_line)

    # get a comment
    while True:
        file_line = f.readline()
        if b"#" in file_line:
            pass
        else:  # get width and height
            output.write(file_line)
            width_b, height_b = file_line.replace(b"\n", b"").split(b" ")
            width = int(width_b)
            height = int(height_b)
            break

        output.write(file_line)

    output.write(f.readline())  # write 255
    # read color and write color
    color_data = f.read()
    for x in range(0, width * height * 3, 3):
        red = int(color_data[x])
        green = int(color_data[x + 1])
        blue = int(color_data[x + 2])
        if red > 200 and green < 100 and blue < 100:
            red_out, green_out, blue_out = 0, 0, 0
        else:
            red_out, green_out, blue_out = 255, 255, 255
        output.write(red_out.to_bytes(1, "big"))
        output.write(green_out.to_bytes(1, "big"))
        output.write(blue_out.to_bytes(1, "big"))
    output.close()
  
ページのトップへ戻る