When I run this script, it gets an type error, TypeError: Writer.__init__() got an unexpected keyword argument 'physical'
When I run this script, it gets an type error, TypeError: Writer.__init__() got an unexpected keyword argument 'physical'
这是我用于代码的 png
test.png
它适用于一些图片而不适用于其他图片,这是给出错误的图片之一
import png
import array
point = (7, 10) # coordinates of pixel to be painted red
reader = png.Reader(filename='test.png')
w, h, pixels, metadata = reader.read_flat()
print(w, h, pixels, metadata)
pixel_byte_width = 4 if metadata['alpha'] else 3
pixel_position = point[0] + point[1] * w
new_pixel_value = (255, 0, 0, 0) if metadata['alpha'] else (255, 0, 0)
pixels[pixel_position * pixel_byte_width : (pixel_position + 1) * pixel_byte_width] = array.array('B', new_pixel_value)
output = open('img2.txt', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()
当我 运行 它时,它打印:
32 32 array('B', [255, 233, 229, 255, 229, 233, 238, 255, 165, 227, 255, 255, 229, 232, 236, 255, 255, 233, 228, 255, 229, 233, 238, 255, 173, 229, 255, 255, 178, 229, 252, 255, 170, 228, 254, 255, 228, 234, 240, 255, 255, 229, 222, 255, 231, 231, 233, 255, 168, 235, 255, 255, 231, 168, 76, 255, 166, 241, 255, 255, 167, 243, 255, 255, 232, 168, 75, 255, 166, 241, 255, 255, 165, 234, 255, 255, 226, 229, 233, 255, 255, 230, 225, 255, 231, 235, 239, 255, 176, 225, 244, 255, 175, 227, 249, 255, 208, 235, 255, 255, 229, 211, 166, 255, 255, 200, 110, 255, 242, 213, 171, 255, 215, 214, 183, 255, 255, 202, 119, 255, 255, 201, 117, 255, 250, 213, 163, 255, 227, 233, 238, 255, 217, 230, 240, 255, 209, 231, 245, 255, 158, 227, 255, 255, 231, 235, 239, 255, 221, 232, 242, 255, 174, 222, 243, 255, 170, 225, 250, 255, 220, 233, 244, 255, 165, 232, 255, 255, 255, 142, 107, 255, 159, 245, 255, 255, 165, 245, 255, 255, 230, 171, 81, 255, 255, 122, 0, 255, 233, 166, 71, 255, 226, 173, 94, 255, 255, 118, 0, 255, 255, 219, 163, 255, 221, 141, 122, 255, 223, 250, 255, 255, 154, 242, 255, 255, 236, 163, 60, 255, 163, 242, 255, 255, 212, 221, 202, 255, 163, 234, 255, 255, 231, 208, 159, 255, 247, 223, 198, 255, 255, 222, 195, 255, 255, 205, 135, 255, 248, 215, 170, 255, 153, 235, 255, 255, 163, 229, 255, 255, 245, 232, 232, 255, 255, 233, 226, 255, 236, 235, 237, 255, 165, 223, 246, 255, 250, 238, 241, 255, 255, 138, 51, 255, 232, 253, 255, 255, 159, 243, 255, 255, 229, 175, 75, 255, 218, 163, 168, 255, 255, 85, 30, 255, 255, 140, 86, 255, 219, 172, 130, 255, 243, 167, 35, 255, 240, 98, 49, 255, 153, 255, 255, 255, 244, 189, 84, 255, 70, 52, 58, 255, 255, 143, 79, 255, 255, 89, 40, 255, 249, 150, 132, 255, 230, 193, 111, 255, 255, 127, 0, 255, 235, 162, 56, 255, 170, 233, 255, 255, 213, 235, 252, 255, 217, 212, 172, 255, 218, 240, 255, 255, 244, 212, 162, 255, 255, 200, 109, 255, 255, 205, 126, 255, 255, 233, 226, 255, 224, 233, 238, 255, 231, 235, 239, 255, 157, 236, 255, 255, 242, 137, 55, 255, 154, 255, 255, 255, 238, 157, 89, 255, 245, 75, 0, 255, 211, 136, 67, 255, 245, 152, 97, 255, 255, 142, 0, 255, 255, 133, 67, 255, 94, 74, 59, 255, 205, 90, 14, 255, 255, 109, 0, 255, 255, 76, 0, 255, 255, 26, 0, 255, 239, 65, 0, 255, 216, 108, 49, 255, 220, 199, 116, 255, 255, 94, 34, 255, 255, 142, 1, 255, 84, 70, 73, 255, 205, 111, 4, 255, 226, 183, 110, 255, 167, 241, 255, 255, 169, 235, 255, 255, 167, 232, 255, 255, 240, 216, 179, 255, 210, 236, 255, 255, 255, 209, 145, 255, 154, 234, 255, 255, 231, 233, 236, 255, 162, 228, 255, 255, 175, 223, 243, 255, 224, 243, 255, 255, 214, 145, 80, 255, 255, 77, 0, 255, 216, 147, 89, 255, 231, 137, 62, 255, 255, 255, 255, 255, 181, 87, 0, 255, 203, 106, 1, 255, 156, 255, 255, 255, 255, 112, 23, 255, 147, 106, 42, 255, 242, 118, 72, 255, 244, 110, 32, 255, 221, 136, 122, 255, 192, 125, 18, 255, 255, 143, 0, 255, 225, 190, 103, 255, 247, 93, 19, 255, 255, 79, 40, 255, 255, 63, 0, 255, 224, 161, 115, 255, 241, 161, 42, 255, 255, 129, 0, 255, 247, 177, 69, 255, 176, 231, 255, 255, 168, 228, 255, 255, 229, 231, 238, 255, 161, 232, 255, 255, 248, 214, 165, 255, 173, 229, 255, 255, 228, 240, 253, 255, 216, 141, 73, 255, 201, 179, 151, 255, 168, 252, 255, 255, 229, 159, 90, 255, 233, 106, 41, 255, 170, 250, 255, 255, 226, 107, 7, 255, 223, 204, 135, 255, 254, 109, 0, 255, 249, 158, 25, 255, 244, 137, 35, 255, 255, 105, 0, 255, 236, 111, 0, 255, 254, 90, 23, 255, 255, 44, 0, 255, 255, 61, 7, 255, 229, 123, 11, 255, 213, 70, 0, 255, 195, 217, 203, 255, 255, 52, 0, 255, 243, 80, 6, 255, 255, 150, 0, 255, 226, 192, 112, 255, 213, 119, 12, 255, 76, 46, 60, 255, 243, 181, 82, 255, 161, 236, 255, 255, 223, 239, 255, 255, 255, 233, 226, 255, 225, 235, 244, 255, 178, 228, 252, 255, 167, 243, 255, 255, 221, 144, 75, 255, 249, 84, 0, 255, 252, 126, 10, 255, 79, 72, 79, 255, 237, 84, 8, 255, 235, 53, 3, 255, 235, 94, 21, 255, 242, 102, 0, 255, 239, 98, 0, 255, 240, 97, 0, 255, 204, 168, 124, 255, 248, 109, 0, 255, 198, 181, 211, 255, 187, 121, 15, 255, 255, 86, 0, 255, 247, 119, 0, 255, 255, 107, 0, 255, 255, 106, 0, 255, 255, 124, 0, 255, 255, 67, 0, 255, 255, 95, 50, 255, 255, 42, 0, 255, 233, 54, 0, 255, 191, 111, 18, 255, 224, 210, 157, 255, 222, 178, 100, 255, 255, 173, 71, 255, 230, 153, 36, 255, 231, 241, 255, 255, 163, 227, 255, 255, 176, 234, 255, 255, 214, 159, 107, 255, 166, 252, 255, 255, 218, 139, 75, 255, 224, 193, 116, 255, 255, 104, 0, 255, 235, 119, 1, 255, 224, 74, 27, 255, 229, 66, 24, 255, 233, 95, 31, 255, 205, 37, 7, 255, 249, 96, 0, 255, 255, 80, 0, 255, 235, 142, 38, 255, 250, 94, 0, 255, 187, 89, 19, 255, 252, 122, 0, 255, 232, 70, 3, 255, 251, 119, 17, 255, 220, 170, 119, 255, 255, 96, 28, 255, 140, 63, 47, 255, 255, 103, 39, 255, 255, 118, 82, 255, 252, 135, 75, 255, 255, 67, 0, 255, 255, 119, 0, 255, 226, 182, 89, 255, 233, 161, 57, 255, 163, 243, 255, 255, 173, 230, 255, 255, 228, 231, 236, 255, 178, 229, 254, 255, 179, 230, 252, 255, 175, 231, 255, 255, 216, 193, 131, 255, 207, 161, 122, 255, 227, 189, 96, 255, 236, 73, 34, 255, 255, 122, 0, 255, 227, 67, 11, 255, 201, 194, 200, 255, 224, 74, 9, 255, 217, 19, 0, 255, 248, 92, 43, 255, 178, 114, 18, 255, 212, 161, 111, 255, 240, 169, 111, 255, 81, 61, 74, 255, 253, 182, 57, 255, 255, 141, 0, 255, 255, 112, 51, 255, 216, 99, 0, 255, 213, 112, 50, 255, 231, 138, 36, 255, 252, 107, 46, 255, 230, 113, 14, 255, 242, 97, 48, 255, 255, 42, 0, 255, 255, 76, 1, 255, 218, 189, 119, 255, 220, 183, 111, 255, 167, 240, 255, 255, 174, 228, 253, 255, 173, 235, 255, 255, 236, 163, 59, 255, 167, 242, 255, 255, 165, 255, 255, 255, 229, 114, 89, 255, 198, 207, 181, 255, 240, 110, 46, 255, 255, 38, 0, 255, 255, 92, 0, 255, 233, 80, 23, 255, 225, 215, 135, 255, 193, 102, 100, 255, 244, 136, 102, 255, 255, 103, 64, 255, 255, 123, 36, 255, 247, 79, 0, 255, 255, 110, 0, 255, 221, 166, 108, 255, 226, 112, 53, 255, 227, 118, 52, 255, 255, 115, 82, 255, 254, 111, 44, 255, 228, 143, 41, 255, 246, 115, 0, 255, 255, 96, 45, 255, 233, 148, 0, 255, 225, 54, 11, 255, 252, 166, 15, 255, 255, 136, 0, 255, 255, 129, 0, 255, 241, 158, 44, 255, 173, 235, 255, 255, 169, 239, 255, 255, 233, 169, 75, 255, 255, 125, 0, 255, 204, 109, 0, 255, 228, 127, 108, 255, 255, 25, 0, 255, 240, 105, 46, 255, 228, 123, 100, 255, 232, 109, 0, 255, 181, 116, 32, 255, 192, 112, 15, 255, 255, 137, 24, 255, 227, 60, 0, 255, 221, 174, 179, 255, 236, 167, 53, 255, 235, 43, 5, 255, 255, 53, 0, 255, 255, 63, 0, 255, 213, 129, 109, 255, 228, 62, 14, 255, 238, 76, 30, 255, 242, 84, 0, 255, 255, 49, 0, 255, 226, 205, 123, 255, 247, 102, 0, 255, 255, 107, 0, 255, 255, 131, 8, 255, 223, 123, 105, 255, 249, 155, 19, 255, 227, 175, 91, 255, 162, 246, 255, 255, 179, 229, 252, 255, 218, 183, 117, 255, 162, 251, 255, 255, 242, 126, 13, 255, 215, 80, 16, 255, 229, 137, 0, 255, 223, 120, 115, 255, 216, 183, 126, 255, 201, 141, 133, 255, 212, 155, 155, 255, 255, 164, 12, 255, 255, 123, 0, 255, 248, 116, 41, 255, 202, 174, 129, 255, 242, 94, 0, 255, 255, 99, 48, 255, 166, 128, 40, 255, 255, 50, 0, 255, 241, 61, 9, 255, 255, 75, 0, 255, 238, 74, 19, 255, 253, 118, 40, 255, 251, 80, 41, 255, 227, 82, 17, 255, 150, 89, 39, 255, 255, 122, 11, 255, 255, 104, 0, 255, 254, 109, 2, 255, 255, 146, 0, 255, 236, 187, 103, 255, 242, 162, 48, 255, 227, 176, 92, 255, 174, 234, 255, 255, 175, 233, 255, 255, 171, 244, 255, 255, 216, 149, 88, 255, 244, 99, 0, 255, 248, 112, 0, 255, 255, 149, 0, 255, 255, 81, 0, 255, 247, 81, 1, 255, 234, 49, 0, 255, 202, 130, 132, 255, 248, 142, 22, 255, 236, 79, 28, 255, 255, 111, 67, 255, 245, 124, 77, 255, 222, 200, 124, 255, 162, 51, 37, 255, 216, 114, 13, 255, 255, 55, 5, 255, 255, 125, 53, 255, 229, 90, 0, 255, 150, 70, 49, 255, 229, 41, 0, 255, 227, 58, 13, 255, 238, 63, 13, 255, 234, 77, 27, 255, 255, 121, 0, 255, 204, 195, 177, 255, 208, 118, 11, 255, 74, 40, 46, 255, 234, 255, 255, 255, 163, 233, 255, 255, 173, 228, 253, 255, 176, 234, 255, 255, 210, 162, 117, 255, 207, 205, 162, 255, 243, 111, 0, 255, 223, 188, 113, 255, 251, 114, 0, 255, 196, 89, 31, 255, 195, 92, 17, 255, 207, 134, 129, 255, 202, 206, 178, 255, 253, 121, 50, 255, 237, 85, 11, 255, 226, 58, 12, 255, 199, 139, 134, 255, 246, 97, 0, 255, 255, 138, 0, 255, 248, 167, 28, 255, 255, 148, 0, 255, 255, 105, 73, 255, 255, 112, 70, 255, 237, 138, 114, 255, 221, 154, 85, 255, 224, 62, 25, 255, 202, 151, 142, 255, 255, 101, 1, 255, 255, 43, 0, 255, 255, 57, 0, 255, 205, 105, 0, 255, 228, 198, 121, 255, 222, 237, 252, 255, 255, 233, 225, 255, 231, 234, 236, 255, 178, 228, 252, 255, 179, 236, 255, 255, 175, 233, 255, 255, 170, 247, 255, 255, 234, 109, 11, 255, 210, 208, 163, 255, 255, 156, 9, 255, 255, 148, 0, 255, 255, 147, 29, 255, 195, 160, 169, 255, 243, 64, 1, 255, 255, 121, 0, 255, 236, 84, 16, 255, 255, 126, 32, 255, 218, 132, 121, 255, 255, 40, 0, 255, 255, 59, 0, 255, 248, 137, 56, 255, 255, 101, 60, 255, 255, 116, 69, 255, 255, 101, 53, 255, 255, 101, 30, 255, 246, 101, 7, 255, 237, 62, 14, 255, 216, 60, 5, 255, 255, 66, 0, 255, 226, 193, 97, 255, 161, 252, 255, 255, 211, 237, 255, 255, 220, 230, 239, 255, 224, 233, 239, 255, 160, 227, 255, 255, 173, 228, 253, 255, 234, 231, 235, 255, 167, 228, 255, 255, 167, 244, 255, 255, 218, 148, 82, 255, 249, 81, 0, 255, 216, 152, 100, 255, 210, 112, 1, 255, 73, 58, 78, 255, 248, 112, 0, 255, 243, 102, 50, 255, 255, 42, 0, 255, 255, 80, 0, 255, 187, 61, 33, 255, 222, 115, 76, 255, 195, 60, 20, 255, 247, 166, 56, 255, 242, 86, 58, 255, 211, 87, 1, 255, 255, 116, 70, 255, 237, 76, 22, 255, 133, 69, 57, 255, 207, 131, 9, 255, 181, 42, 8, 255, 168, 255, 255, 255, 175, 237, 255, 255, 255, 72, 0, 255, 224, 181, 95, 255, 161, 233, 255, 255, 228, 232, 236, 255, 255, 233, 229, 255, 251, 234, 231, 255, 170, 228, 254, 255, 229, 232, 236, 255, 255, 239, 237, 255, 247, 161, 95, 255, 167, 253, 255, 255, 228, 129, 45, 255, 161, 255, 255, 255, 218, 150, 93, 255, 223, 218, 156, 255, 238, 122, 61, 255, 255, 148, 0, 255, 255, 104, 34, 255, 201, 95, 3, 255, 243, 124, 0, 255, 165, 46, 28, 255, 229, 34, 0, 255, 213, 33, 0, 255, 202, 119, 47, 255, 82, 84, 91, 255, 223, 61, 0, 255, 243, 85, 42, 255, 241, 58, 0, 255, 214, 118, 101, 255, 226, 212, 143, 255, 211, 121, 101, 255, 173, 240, 255, 255, 173, 244, 255, 255, 180, 236, 255, 255, 219, 231, 241, 255, 168, 229, 255, 255, 236, 234, 235, 255, 161, 227, 255, 255, 217, 231, 242, 255, 164, 227, 255, 255, 237, 234, 235, 255, 156, 233, 255, 255, 233, 232, 237, 255, 163, 239, 255, 255, 212, 157, 101, 255, 217, 152, 91, 255, 248, 81, 0, 255, 215, 152, 94, 255, 248, 137, 27, 255, 82, 73, 81, 255, 198, 81, 61, 255, 213, 79, 15, 255, 242, 71, 0, 255, 222, 69, 8, 255, 107, 121, 134, 255, 209, 119, 47, 255, 196, 67, 17, 255, 139, 77, 67, 255, 223, 47, 0, 255, 213, 136, 115, 255, 159, 255, 255, 255, 255, 237, 243, 255, 171, 240, 255, 255, 165, 227, 255, 255, 226, 231, 237, 255, 252, 233, 232, 255, 159, 228, 255, 255, 230, 231, 236, 255, 162, 229, 255, 255, 222, 231, 238, 255, 174, 228, 253, 255, 178, 228, 252, 255, 173, 229, 255, 255, 225, 231, 239, 255, 224, 232, 238, 255, 255, 240, 238, 255, 244, 168, 111, 255, 161, 251, 255, 255, 244, 129, 40, 255, 159, 255, 255, 255, 214, 144, 78, 255, 255, 100, 0, 255, 233, 104, 0, 255, 110, 121, 133, 255, 231, 64, 0, 255, 227, 33, 0, 255, 214, 36, 0, 255, 204, 120, 42, 255, 255, 112, 0, 255, 217, 111, 25, 255, 106, 88, 79, 255, 231, 86, 25, 255, 197, 131, 115, 255, 255, 241, 240, 255, 255, 233, 228, 255, 249, 235, 231, 255, 215, 231, 240, 255, 255, 233, 228, 255, 255, 234, 227, 255, 255, 235, 228, 255, 255, 233, 226, 255, 227, 233, 238, 255, 186, 231, 251, 255, 180, 228, 252, 255, 171, 228, 253, 255, 248, 232, 232, 255, 155, 227, 255, 255, 231, 234, 236, 255, 162, 233, 255, 255, 168, 229, 255, 255, 247, 247, 253, 255, 254, 105, 0, 255, 223, 255, 255, 255, 229, 146, 69, 255, 85, 73, 81, 255, 193, 78, 46, 255, 97, 102, 96, 255, 238, 67, 6, 255, 162, 100, 42, 255, 121, 110, 118, 255, 217, 101, 9, 255, 103, 92, 99, 255, 111, 65, 48, 255, 176, 255, 255, 255, 229, 241, 253, 255, 253, 233, 227, 255, 252, 232, 230, 255, 253, 234, 230, 255, 228, 232, 241, 255, 222, 233, 238, 255, 255, 235, 230, 255, 151, 226, 255, 255, 237, 234, 235, 255, 163, 227, 255, 255, 252, 235, 232, 255, 169, 230, 255, 255, 164, 227, 255, 255, 227, 232, 237, 255, 255, 233, 225, 255, 228, 233, 238, 255, 223, 231, 239, 255, 208, 231, 245, 255, 223, 232, 239, 255, 255, 245, 247, 255, 227, 234, 239, 255, 173, 248, 255, 255, 197, 80, 0, 255, 140, 123, 120, 255, 76, 53, 47, 255, 58, 44, 45, 255, 192, 58, 26, 255, 151, 131, 128, 255, 127, 106, 105, 255, 205, 101, 19, 255, 99, 47, 31, 255, 179, 246, 255, 255, 255, 234, 234, 255, 159, 228, 255, 255, 255, 238, 228, 255, 236, 231, 236, 255, 165, 228, 255, 255, 175, 230, 254, 255, 221, 232, 241, 255, 255, 232, 229, 255, 166, 229, 255, 255, 228, 231, 236, 255, 250, 231, 230, 255, 255, 233, 228, 255, 255, 235, 227, 255, 159, 227, 255, 255, 236, 234, 235, 255, 158, 227, 255, 255, 226, 231, 237, 255, 206, 231, 246, 255, 158, 227, 255, 255, 234, 234, 236, 255, 217, 230, 240, 255, 181, 247, 255, 255, 99, 47, 31, 255, 116, 78, 65, 255, 157, 125, 114, 255, 55, 33, 29, 255, 147, 119, 113, 255, 154, 129, 112, 255, 95, 71, 62, 255, 108, 66, 54, 255, 193, 255, 255, 255, 179, 229, 252, 255, 166, 228, 255, 255, 233, 231, 235, 255, 164, 231, 255, 255, 255, 233, 228, 255, 165, 228, 255, 255, 233, 231, 235, 255, 162, 227, 255, 255, 173, 229, 254, 255, 179, 228, 252, 255, 174, 228, 253, 255, 220, 232, 239, 255, 255, 233, 228, 255, 223, 233, 239, 255, 172, 228, 253, 255, 175, 231, 255, 255, 222, 231, 238, 255, 224, 232, 238, 255, 255, 233, 226, 255, 237, 234, 235, 255, 167, 229, 255, 255, 228, 231, 236, 255, 174, 228, 253, 255, 193, 255, 255, 255, 111, 63, 46, 255, 146, 116, 105, 255, 120, 81, 67, 255, 159, 131, 119, 255, 154, 123, 111, 255, 61, 37, 33, 255, 119, 71, 52, 255, 186, 247, 255, 255, 179, 229, 252, 255, 170, 228, 254, 255, 229, 232, 236, 255, 255, 233, 225, 255, 228, 233, 237, 255, 164, 227, 255, 255, 229, 232, 236, 255, 255, 233, 226, 255, 255, 234, 229, 255, 165, 228, 255, 255, 173, 228, 253, 255, 167, 228, 255, 255, 233, 234, 237, 255, 245, 232, 234, 255, 166, 228, 255, 255, 234, 230, 236, 255, 206, 232, 247, 255, 158, 227, 255, 255, 235, 234, 236, 255, 164, 227, 255, 255, 178, 228, 252, 255, 174, 228, 253, 255, 179, 229, 252, 255, 186, 247, 255, 255, 111, 63, 46, 255, 145, 115, 104, 255, 113, 74, 61, 255, 154, 125, 111, 255, 59, 34, 29, 255, 72, 47, 42, 255, 119, 71, 52, 255, 186, 247, 255, 255, 175, 228, 252, 255, 221, 232, 241, 255, 164, 227, 255, 255, 235, 234, 235, 255, 172, 230, 255, 255, 220, 231, 241, 255, 164, 227, 255, 255, 228, 233, 237, 255, 216, 231, 240, 255, 255, 233, 225, 255, 231, 234, 236, 255, 178, 228, 252, 255, 172, 229, 255, 255, 225, 231, 237, 255, 162, 228, 255, 255, 255, 233, 228, 255, 255, 233, 228, 255, 224, 233, 238, 255, 220, 230, 240, 255, 220, 231, 239, 255, 168, 228, 255, 255, 179, 229, 252, 255, 179, 229, 252, 255, 193, 255, 255, 255, 110, 63, 45, 255, 154, 124, 115, 255, 141, 112, 101, 255, 159, 127, 120, 255, 99, 74, 70, 255, 72, 49, 44, 255, 118, 70, 52, 255, 193, 255, 255, 255, 179, 229, 252, 255, 175, 228, 252, 255, 178, 228, 252, 255, 169, 229, 255, 255, 255, 235, 227, 255, 165, 230, 255, 255, 166, 227, 255, 255, 213, 232, 244, 255, 217, 230, 239, 255, 232, 234, 236, 255, 163, 227, 255, 255, 178, 228, 252, 255, 182, 230, 252, 255, 171, 228, 254, 255, 209, 232, 247, 255, 248, 233, 231, 255, 255, 234, 229, 255, 245, 234, 232, 255, 221, 232, 238, 255, 255, 233, 226, 255, 237, 234, 235, 255, 173, 228, 253, 255, 186, 247, 255, 255, 106, 49, 27, 255, 120, 83, 70, 255, 125, 91, 76, 255, 141, 110, 100, 255, 156, 127, 111, 255, 87, 61, 53, 255, 70, 45, 40, 255, 127, 89, 74, 255, 106, 48, 26, 255, 186, 247, 255, 255, 179, 229, 252, 255, 179, 229, 252, 255, 164, 228, 255, 255, 255, 232, 228, 255, 255, 233, 227, 255, 255, 235, 229, 255, 166, 227, 255, 255, 180, 231, 255, 255, 174, 230, 255, 255, 228, 231, 236, 255, 173, 228, 253, 255, 234, 231, 235, 255, 163, 228, 255, 255, 232, 227, 228, 255, 155, 225, 254, 255, 255, 237, 231, 255, 231, 231, 237, 255, 162, 228, 255, 255, 237, 234, 235, 255, 163, 227, 255, 255, 179, 229, 252, 255, 193, 255, 255, 255, 111, 64, 45, 255, 120, 87, 80, 255, 118, 82, 69, 255, 143, 112, 102, 255, 147, 115, 104, 255, 64, 39, 35, 255, 69, 44, 39, 255, 111, 85, 83, 255, 116, 72, 54, 255, 193, 255, 255, 255, 186, 247, 255, 255, 173, 228, 253, 255, 240, 233, 240, 255, 220, 232, 239, 255, 255, 233, 228, 255, 225, 234, 241, 255, 171, 230, 255, 255, 217, 218, 209, 255, 162, 224, 249, 255, 169, 228, 255, 255, 171, 229, 255, 255, 229, 233, 237, 255, 255, 240, 239, 255, 122, 164, 91, 255, 190, 241, 255, 255, 179, 238, 255, 255, 255, 233, 232, 255, 168, 224, 243, 255, 170, 228, 251, 255, 233, 232, 236, 255, 189, 255, 255, 255, 104, 46, 24, 255, 145, 110, 103, 255, 143, 114, 103, 255, 116, 83, 69, 255, 142, 111, 100, 255, 153, 122, 113, 255, 105, 72, 61, 255, 108, 74, 63, 255, 152, 120, 106, 255, 119, 84, 70, 255, 118, 70, 52, 255, 106, 49, 27, 255, 191, 255, 255, 255, 207, 226, 232, 255, 161, 226, 253, 255, 235, 235, 239, 255, 237, 226, 224, 255, 174, 235, 255, 255, 75, 134, 30, 255, 255, 246, 252, 255, 240, 244, 255, 255, 210, 227, 234, 255, 161, 226, 253, 255, 249, 245, 255, 255, 117, 176, 137, 255, 63, 117, 0, 255, 99, 155, 82, 255, 198, 253, 255, 255, 106, 162, 96, 255, 193, 242, 255, 255, 188, 254, 255, 255, 106, 49, 27, 255, 121, 84, 71, 255, 125, 95, 80, 255, 123, 92, 76, 255, 131, 95, 84, 255, 141, 112, 101, 255, 146, 116, 104, 255, 147, 116, 105, 255, 102, 69, 58, 255, 130, 96, 80, 255, 125, 92, 84, 255, 62, 41, 37, 255, 131, 92, 77, 255, 113, 55, 41, 255, 113, 187, 128, 255, 193, 242, 255, 255, 188, 243, 255, 255, 120, 162, 91, 255, 241, 254, 255, 255, 85, 142, 58, 255, 60, 123, 14, 255, 84, 145, 60, 255, 115, 173, 122, 255, 197, 246, 255, 255, 145, 201, 184, 255, 73, 127, 18, 255, 95, 151, 71, 255, 212, 255, 255, 255, 58, 113, 0, 255, 141, 192, 162, 255, 65, 120, 3, 255, 120, 195, 131, 255, 122, 75, 66, 255, 104, 72, 61, 255, 122, 88, 80, 255, 134, 103, 99, 255, 129, 97, 83, 255, 144, 114, 103, 255, 143, 112, 100, 255, 158, 127, 120, 255, 105, 75, 65, 255, 122, 84, 71, 255, 134, 99, 83, 255, 96, 70, 66, 255, 63, 42, 39, 255, 131, 82, 78, 255, 92, 114, 44, 255, 66, 129, 10, 255, 120, 175, 124, 255, 133, 185, 153, 255, 62, 119, 2, 255, 132, 188, 150, 255, 103, 157, 80, 255, 81, 135, 40, 255, 72, 125, 32, 255, 76, 130, 23, 255, 71, 128, 28, 255, 58, 116, 34, 255, 77, 131, 40, 255, 75, 130, 22, 255, 84, 139, 45, 255, 35, 91, 14, 255, 75, 148, 42, 255, 67, 23, 32, 255, 129, 90, 77, 255, 106, 73, 63, 255, 116, 85, 73, 255, 141, 109, 90, 255, 101, 68, 58, 255, 123, 89, 81, 255, 99, 67, 56, 255, 163, 135, 122, 255, 100, 68, 57, 255, 105, 73, 63, 255, 122, 84, 71, 255, 120, 89, 76, 255, 111, 73, 66, 255, 91, 109, 51, 255, 133, 78, 79, 255, 97, 125, 55, 255, 60, 125, 26, 255, 54, 114, 23, 255, 81, 137, 45, 255, 56, 114, 24, 255, 55, 110, 27, 255, 92, 146, 50, 255, 55, 112, 31, 255, 78, 133, 43, 255, 56, 110, 31, 255, 73, 127, 40, 255, 54, 111, 31, 255, 79, 134, 43, 255, 51, 106, 29, 255, 46, 108, 27, 255, 82, 53, 48, 255, 78, 104, 42, 255, 111, 72, 66, 255, 107, 74, 64, 255, 122, 86, 72, 255, 120, 84, 71, 255, 106, 73, 63, 255, 119, 88, 75, 255, 103, 69, 58, 255, 149, 119, 107, 255, 148, 117, 106, 255, 103, 70, 59, 255, 122, 86, 72, 255, 121, 84, 71, 255, 124, 87, 73, 255, 112, 74, 67, 255, 63, 40, 37, 255, 129, 83, 76, 255, 101, 123, 57, 255, 56, 114, 32, 255, 59, 113, 33, 255, 76, 130, 42, 255, 53, 109, 32, 255, 46, 100, 27, 255]) {'greyscale': False, 'alpha': True, 'planes': 4, 'bitdepth': 8, 'interlace': 0, 'size': (32, 32), 'gamma': 0.45455, 'physical': _Resolution(x=3780, y=3780, unit_is_meter=True)}
以及打印后的错误:
Traceback (most recent call last):
File "C:\Users\user\Desktop\img EPIC compiler\img compiler.py", line 18, in <module>
writer = png.Writer(w, h, **metadata)
TypeError: Writer.__init__() got an unexpected keyword argument 'physical'
导致错误的原因是什么?
因此无法识别元数据中的值 'physical'
,您需要做的就是对该值使用 .pop()
将其删除。所以如果你像这样使用它:
import array
point = (2, 10) # coordinates of pixel to be painted red
reader = png.Reader(filename='testt.png')
w, h, pixels, metadata = reader.read_flat()
metadata.pop('physical')
print(w, h, pixels, metadata)
pixel_byte_width = 4 if metadata['alpha'] else 3
pixel_position = point[0] + point[1] * w
new_pixel_value = (255, 0, 0, 0) if metadata['alpha'] else (255, 0, 0)
pixels[pixel_position * pixel_byte_width : (pixel_position + 1) * pixel_byte_width] = array.array('B', new_pixel_value)
output = open('img2.txt', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()
它不再引发错误,并完成了工作。
这是我用于代码的 png test.png
它适用于一些图片而不适用于其他图片,这是给出错误的图片之一
import png
import array
point = (7, 10) # coordinates of pixel to be painted red
reader = png.Reader(filename='test.png')
w, h, pixels, metadata = reader.read_flat()
print(w, h, pixels, metadata)
pixel_byte_width = 4 if metadata['alpha'] else 3
pixel_position = point[0] + point[1] * w
new_pixel_value = (255, 0, 0, 0) if metadata['alpha'] else (255, 0, 0)
pixels[pixel_position * pixel_byte_width : (pixel_position + 1) * pixel_byte_width] = array.array('B', new_pixel_value)
output = open('img2.txt', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()
当我 运行 它时,它打印:
32 32 array('B', [255, 233, 229, 255, 229, 233, 238, 255, 165, 227, 255, 255, 229, 232, 236, 255, 255, 233, 228, 255, 229, 233, 238, 255, 173, 229, 255, 255, 178, 229, 252, 255, 170, 228, 254, 255, 228, 234, 240, 255, 255, 229, 222, 255, 231, 231, 233, 255, 168, 235, 255, 255, 231, 168, 76, 255, 166, 241, 255, 255, 167, 243, 255, 255, 232, 168, 75, 255, 166, 241, 255, 255, 165, 234, 255, 255, 226, 229, 233, 255, 255, 230, 225, 255, 231, 235, 239, 255, 176, 225, 244, 255, 175, 227, 249, 255, 208, 235, 255, 255, 229, 211, 166, 255, 255, 200, 110, 255, 242, 213, 171, 255, 215, 214, 183, 255, 255, 202, 119, 255, 255, 201, 117, 255, 250, 213, 163, 255, 227, 233, 238, 255, 217, 230, 240, 255, 209, 231, 245, 255, 158, 227, 255, 255, 231, 235, 239, 255, 221, 232, 242, 255, 174, 222, 243, 255, 170, 225, 250, 255, 220, 233, 244, 255, 165, 232, 255, 255, 255, 142, 107, 255, 159, 245, 255, 255, 165, 245, 255, 255, 230, 171, 81, 255, 255, 122, 0, 255, 233, 166, 71, 255, 226, 173, 94, 255, 255, 118, 0, 255, 255, 219, 163, 255, 221, 141, 122, 255, 223, 250, 255, 255, 154, 242, 255, 255, 236, 163, 60, 255, 163, 242, 255, 255, 212, 221, 202, 255, 163, 234, 255, 255, 231, 208, 159, 255, 247, 223, 198, 255, 255, 222, 195, 255, 255, 205, 135, 255, 248, 215, 170, 255, 153, 235, 255, 255, 163, 229, 255, 255, 245, 232, 232, 255, 255, 233, 226, 255, 236, 235, 237, 255, 165, 223, 246, 255, 250, 238, 241, 255, 255, 138, 51, 255, 232, 253, 255, 255, 159, 243, 255, 255, 229, 175, 75, 255, 218, 163, 168, 255, 255, 85, 30, 255, 255, 140, 86, 255, 219, 172, 130, 255, 243, 167, 35, 255, 240, 98, 49, 255, 153, 255, 255, 255, 244, 189, 84, 255, 70, 52, 58, 255, 255, 143, 79, 255, 255, 89, 40, 255, 249, 150, 132, 255, 230, 193, 111, 255, 255, 127, 0, 255, 235, 162, 56, 255, 170, 233, 255, 255, 213, 235, 252, 255, 217, 212, 172, 255, 218, 240, 255, 255, 244, 212, 162, 255, 255, 200, 109, 255, 255, 205, 126, 255, 255, 233, 226, 255, 224, 233, 238, 255, 231, 235, 239, 255, 157, 236, 255, 255, 242, 137, 55, 255, 154, 255, 255, 255, 238, 157, 89, 255, 245, 75, 0, 255, 211, 136, 67, 255, 245, 152, 97, 255, 255, 142, 0, 255, 255, 133, 67, 255, 94, 74, 59, 255, 205, 90, 14, 255, 255, 109, 0, 255, 255, 76, 0, 255, 255, 26, 0, 255, 239, 65, 0, 255, 216, 108, 49, 255, 220, 199, 116, 255, 255, 94, 34, 255, 255, 142, 1, 255, 84, 70, 73, 255, 205, 111, 4, 255, 226, 183, 110, 255, 167, 241, 255, 255, 169, 235, 255, 255, 167, 232, 255, 255, 240, 216, 179, 255, 210, 236, 255, 255, 255, 209, 145, 255, 154, 234, 255, 255, 231, 233, 236, 255, 162, 228, 255, 255, 175, 223, 243, 255, 224, 243, 255, 255, 214, 145, 80, 255, 255, 77, 0, 255, 216, 147, 89, 255, 231, 137, 62, 255, 255, 255, 255, 255, 181, 87, 0, 255, 203, 106, 1, 255, 156, 255, 255, 255, 255, 112, 23, 255, 147, 106, 42, 255, 242, 118, 72, 255, 244, 110, 32, 255, 221, 136, 122, 255, 192, 125, 18, 255, 255, 143, 0, 255, 225, 190, 103, 255, 247, 93, 19, 255, 255, 79, 40, 255, 255, 63, 0, 255, 224, 161, 115, 255, 241, 161, 42, 255, 255, 129, 0, 255, 247, 177, 69, 255, 176, 231, 255, 255, 168, 228, 255, 255, 229, 231, 238, 255, 161, 232, 255, 255, 248, 214, 165, 255, 173, 229, 255, 255, 228, 240, 253, 255, 216, 141, 73, 255, 201, 179, 151, 255, 168, 252, 255, 255, 229, 159, 90, 255, 233, 106, 41, 255, 170, 250, 255, 255, 226, 107, 7, 255, 223, 204, 135, 255, 254, 109, 0, 255, 249, 158, 25, 255, 244, 137, 35, 255, 255, 105, 0, 255, 236, 111, 0, 255, 254, 90, 23, 255, 255, 44, 0, 255, 255, 61, 7, 255, 229, 123, 11, 255, 213, 70, 0, 255, 195, 217, 203, 255, 255, 52, 0, 255, 243, 80, 6, 255, 255, 150, 0, 255, 226, 192, 112, 255, 213, 119, 12, 255, 76, 46, 60, 255, 243, 181, 82, 255, 161, 236, 255, 255, 223, 239, 255, 255, 255, 233, 226, 255, 225, 235, 244, 255, 178, 228, 252, 255, 167, 243, 255, 255, 221, 144, 75, 255, 249, 84, 0, 255, 252, 126, 10, 255, 79, 72, 79, 255, 237, 84, 8, 255, 235, 53, 3, 255, 235, 94, 21, 255, 242, 102, 0, 255, 239, 98, 0, 255, 240, 97, 0, 255, 204, 168, 124, 255, 248, 109, 0, 255, 198, 181, 211, 255, 187, 121, 15, 255, 255, 86, 0, 255, 247, 119, 0, 255, 255, 107, 0, 255, 255, 106, 0, 255, 255, 124, 0, 255, 255, 67, 0, 255, 255, 95, 50, 255, 255, 42, 0, 255, 233, 54, 0, 255, 191, 111, 18, 255, 224, 210, 157, 255, 222, 178, 100, 255, 255, 173, 71, 255, 230, 153, 36, 255, 231, 241, 255, 255, 163, 227, 255, 255, 176, 234, 255, 255, 214, 159, 107, 255, 166, 252, 255, 255, 218, 139, 75, 255, 224, 193, 116, 255, 255, 104, 0, 255, 235, 119, 1, 255, 224, 74, 27, 255, 229, 66, 24, 255, 233, 95, 31, 255, 205, 37, 7, 255, 249, 96, 0, 255, 255, 80, 0, 255, 235, 142, 38, 255, 250, 94, 0, 255, 187, 89, 19, 255, 252, 122, 0, 255, 232, 70, 3, 255, 251, 119, 17, 255, 220, 170, 119, 255, 255, 96, 28, 255, 140, 63, 47, 255, 255, 103, 39, 255, 255, 118, 82, 255, 252, 135, 75, 255, 255, 67, 0, 255, 255, 119, 0, 255, 226, 182, 89, 255, 233, 161, 57, 255, 163, 243, 255, 255, 173, 230, 255, 255, 228, 231, 236, 255, 178, 229, 254, 255, 179, 230, 252, 255, 175, 231, 255, 255, 216, 193, 131, 255, 207, 161, 122, 255, 227, 189, 96, 255, 236, 73, 34, 255, 255, 122, 0, 255, 227, 67, 11, 255, 201, 194, 200, 255, 224, 74, 9, 255, 217, 19, 0, 255, 248, 92, 43, 255, 178, 114, 18, 255, 212, 161, 111, 255, 240, 169, 111, 255, 81, 61, 74, 255, 253, 182, 57, 255, 255, 141, 0, 255, 255, 112, 51, 255, 216, 99, 0, 255, 213, 112, 50, 255, 231, 138, 36, 255, 252, 107, 46, 255, 230, 113, 14, 255, 242, 97, 48, 255, 255, 42, 0, 255, 255, 76, 1, 255, 218, 189, 119, 255, 220, 183, 111, 255, 167, 240, 255, 255, 174, 228, 253, 255, 173, 235, 255, 255, 236, 163, 59, 255, 167, 242, 255, 255, 165, 255, 255, 255, 229, 114, 89, 255, 198, 207, 181, 255, 240, 110, 46, 255, 255, 38, 0, 255, 255, 92, 0, 255, 233, 80, 23, 255, 225, 215, 135, 255, 193, 102, 100, 255, 244, 136, 102, 255, 255, 103, 64, 255, 255, 123, 36, 255, 247, 79, 0, 255, 255, 110, 0, 255, 221, 166, 108, 255, 226, 112, 53, 255, 227, 118, 52, 255, 255, 115, 82, 255, 254, 111, 44, 255, 228, 143, 41, 255, 246, 115, 0, 255, 255, 96, 45, 255, 233, 148, 0, 255, 225, 54, 11, 255, 252, 166, 15, 255, 255, 136, 0, 255, 255, 129, 0, 255, 241, 158, 44, 255, 173, 235, 255, 255, 169, 239, 255, 255, 233, 169, 75, 255, 255, 125, 0, 255, 204, 109, 0, 255, 228, 127, 108, 255, 255, 25, 0, 255, 240, 105, 46, 255, 228, 123, 100, 255, 232, 109, 0, 255, 181, 116, 32, 255, 192, 112, 15, 255, 255, 137, 24, 255, 227, 60, 0, 255, 221, 174, 179, 255, 236, 167, 53, 255, 235, 43, 5, 255, 255, 53, 0, 255, 255, 63, 0, 255, 213, 129, 109, 255, 228, 62, 14, 255, 238, 76, 30, 255, 242, 84, 0, 255, 255, 49, 0, 255, 226, 205, 123, 255, 247, 102, 0, 255, 255, 107, 0, 255, 255, 131, 8, 255, 223, 123, 105, 255, 249, 155, 19, 255, 227, 175, 91, 255, 162, 246, 255, 255, 179, 229, 252, 255, 218, 183, 117, 255, 162, 251, 255, 255, 242, 126, 13, 255, 215, 80, 16, 255, 229, 137, 0, 255, 223, 120, 115, 255, 216, 183, 126, 255, 201, 141, 133, 255, 212, 155, 155, 255, 255, 164, 12, 255, 255, 123, 0, 255, 248, 116, 41, 255, 202, 174, 129, 255, 242, 94, 0, 255, 255, 99, 48, 255, 166, 128, 40, 255, 255, 50, 0, 255, 241, 61, 9, 255, 255, 75, 0, 255, 238, 74, 19, 255, 253, 118, 40, 255, 251, 80, 41, 255, 227, 82, 17, 255, 150, 89, 39, 255, 255, 122, 11, 255, 255, 104, 0, 255, 254, 109, 2, 255, 255, 146, 0, 255, 236, 187, 103, 255, 242, 162, 48, 255, 227, 176, 92, 255, 174, 234, 255, 255, 175, 233, 255, 255, 171, 244, 255, 255, 216, 149, 88, 255, 244, 99, 0, 255, 248, 112, 0, 255, 255, 149, 0, 255, 255, 81, 0, 255, 247, 81, 1, 255, 234, 49, 0, 255, 202, 130, 132, 255, 248, 142, 22, 255, 236, 79, 28, 255, 255, 111, 67, 255, 245, 124, 77, 255, 222, 200, 124, 255, 162, 51, 37, 255, 216, 114, 13, 255, 255, 55, 5, 255, 255, 125, 53, 255, 229, 90, 0, 255, 150, 70, 49, 255, 229, 41, 0, 255, 227, 58, 13, 255, 238, 63, 13, 255, 234, 77, 27, 255, 255, 121, 0, 255, 204, 195, 177, 255, 208, 118, 11, 255, 74, 40, 46, 255, 234, 255, 255, 255, 163, 233, 255, 255, 173, 228, 253, 255, 176, 234, 255, 255, 210, 162, 117, 255, 207, 205, 162, 255, 243, 111, 0, 255, 223, 188, 113, 255, 251, 114, 0, 255, 196, 89, 31, 255, 195, 92, 17, 255, 207, 134, 129, 255, 202, 206, 178, 255, 253, 121, 50, 255, 237, 85, 11, 255, 226, 58, 12, 255, 199, 139, 134, 255, 246, 97, 0, 255, 255, 138, 0, 255, 248, 167, 28, 255, 255, 148, 0, 255, 255, 105, 73, 255, 255, 112, 70, 255, 237, 138, 114, 255, 221, 154, 85, 255, 224, 62, 25, 255, 202, 151, 142, 255, 255, 101, 1, 255, 255, 43, 0, 255, 255, 57, 0, 255, 205, 105, 0, 255, 228, 198, 121, 255, 222, 237, 252, 255, 255, 233, 225, 255, 231, 234, 236, 255, 178, 228, 252, 255, 179, 236, 255, 255, 175, 233, 255, 255, 170, 247, 255, 255, 234, 109, 11, 255, 210, 208, 163, 255, 255, 156, 9, 255, 255, 148, 0, 255, 255, 147, 29, 255, 195, 160, 169, 255, 243, 64, 1, 255, 255, 121, 0, 255, 236, 84, 16, 255, 255, 126, 32, 255, 218, 132, 121, 255, 255, 40, 0, 255, 255, 59, 0, 255, 248, 137, 56, 255, 255, 101, 60, 255, 255, 116, 69, 255, 255, 101, 53, 255, 255, 101, 30, 255, 246, 101, 7, 255, 237, 62, 14, 255, 216, 60, 5, 255, 255, 66, 0, 255, 226, 193, 97, 255, 161, 252, 255, 255, 211, 237, 255, 255, 220, 230, 239, 255, 224, 233, 239, 255, 160, 227, 255, 255, 173, 228, 253, 255, 234, 231, 235, 255, 167, 228, 255, 255, 167, 244, 255, 255, 218, 148, 82, 255, 249, 81, 0, 255, 216, 152, 100, 255, 210, 112, 1, 255, 73, 58, 78, 255, 248, 112, 0, 255, 243, 102, 50, 255, 255, 42, 0, 255, 255, 80, 0, 255, 187, 61, 33, 255, 222, 115, 76, 255, 195, 60, 20, 255, 247, 166, 56, 255, 242, 86, 58, 255, 211, 87, 1, 255, 255, 116, 70, 255, 237, 76, 22, 255, 133, 69, 57, 255, 207, 131, 9, 255, 181, 42, 8, 255, 168, 255, 255, 255, 175, 237, 255, 255, 255, 72, 0, 255, 224, 181, 95, 255, 161, 233, 255, 255, 228, 232, 236, 255, 255, 233, 229, 255, 251, 234, 231, 255, 170, 228, 254, 255, 229, 232, 236, 255, 255, 239, 237, 255, 247, 161, 95, 255, 167, 253, 255, 255, 228, 129, 45, 255, 161, 255, 255, 255, 218, 150, 93, 255, 223, 218, 156, 255, 238, 122, 61, 255, 255, 148, 0, 255, 255, 104, 34, 255, 201, 95, 3, 255, 243, 124, 0, 255, 165, 46, 28, 255, 229, 34, 0, 255, 213, 33, 0, 255, 202, 119, 47, 255, 82, 84, 91, 255, 223, 61, 0, 255, 243, 85, 42, 255, 241, 58, 0, 255, 214, 118, 101, 255, 226, 212, 143, 255, 211, 121, 101, 255, 173, 240, 255, 255, 173, 244, 255, 255, 180, 236, 255, 255, 219, 231, 241, 255, 168, 229, 255, 255, 236, 234, 235, 255, 161, 227, 255, 255, 217, 231, 242, 255, 164, 227, 255, 255, 237, 234, 235, 255, 156, 233, 255, 255, 233, 232, 237, 255, 163, 239, 255, 255, 212, 157, 101, 255, 217, 152, 91, 255, 248, 81, 0, 255, 215, 152, 94, 255, 248, 137, 27, 255, 82, 73, 81, 255, 198, 81, 61, 255, 213, 79, 15, 255, 242, 71, 0, 255, 222, 69, 8, 255, 107, 121, 134, 255, 209, 119, 47, 255, 196, 67, 17, 255, 139, 77, 67, 255, 223, 47, 0, 255, 213, 136, 115, 255, 159, 255, 255, 255, 255, 237, 243, 255, 171, 240, 255, 255, 165, 227, 255, 255, 226, 231, 237, 255, 252, 233, 232, 255, 159, 228, 255, 255, 230, 231, 236, 255, 162, 229, 255, 255, 222, 231, 238, 255, 174, 228, 253, 255, 178, 228, 252, 255, 173, 229, 255, 255, 225, 231, 239, 255, 224, 232, 238, 255, 255, 240, 238, 255, 244, 168, 111, 255, 161, 251, 255, 255, 244, 129, 40, 255, 159, 255, 255, 255, 214, 144, 78, 255, 255, 100, 0, 255, 233, 104, 0, 255, 110, 121, 133, 255, 231, 64, 0, 255, 227, 33, 0, 255, 214, 36, 0, 255, 204, 120, 42, 255, 255, 112, 0, 255, 217, 111, 25, 255, 106, 88, 79, 255, 231, 86, 25, 255, 197, 131, 115, 255, 255, 241, 240, 255, 255, 233, 228, 255, 249, 235, 231, 255, 215, 231, 240, 255, 255, 233, 228, 255, 255, 234, 227, 255, 255, 235, 228, 255, 255, 233, 226, 255, 227, 233, 238, 255, 186, 231, 251, 255, 180, 228, 252, 255, 171, 228, 253, 255, 248, 232, 232, 255, 155, 227, 255, 255, 231, 234, 236, 255, 162, 233, 255, 255, 168, 229, 255, 255, 247, 247, 253, 255, 254, 105, 0, 255, 223, 255, 255, 255, 229, 146, 69, 255, 85, 73, 81, 255, 193, 78, 46, 255, 97, 102, 96, 255, 238, 67, 6, 255, 162, 100, 42, 255, 121, 110, 118, 255, 217, 101, 9, 255, 103, 92, 99, 255, 111, 65, 48, 255, 176, 255, 255, 255, 229, 241, 253, 255, 253, 233, 227, 255, 252, 232, 230, 255, 253, 234, 230, 255, 228, 232, 241, 255, 222, 233, 238, 255, 255, 235, 230, 255, 151, 226, 255, 255, 237, 234, 235, 255, 163, 227, 255, 255, 252, 235, 232, 255, 169, 230, 255, 255, 164, 227, 255, 255, 227, 232, 237, 255, 255, 233, 225, 255, 228, 233, 238, 255, 223, 231, 239, 255, 208, 231, 245, 255, 223, 232, 239, 255, 255, 245, 247, 255, 227, 234, 239, 255, 173, 248, 255, 255, 197, 80, 0, 255, 140, 123, 120, 255, 76, 53, 47, 255, 58, 44, 45, 255, 192, 58, 26, 255, 151, 131, 128, 255, 127, 106, 105, 255, 205, 101, 19, 255, 99, 47, 31, 255, 179, 246, 255, 255, 255, 234, 234, 255, 159, 228, 255, 255, 255, 238, 228, 255, 236, 231, 236, 255, 165, 228, 255, 255, 175, 230, 254, 255, 221, 232, 241, 255, 255, 232, 229, 255, 166, 229, 255, 255, 228, 231, 236, 255, 250, 231, 230, 255, 255, 233, 228, 255, 255, 235, 227, 255, 159, 227, 255, 255, 236, 234, 235, 255, 158, 227, 255, 255, 226, 231, 237, 255, 206, 231, 246, 255, 158, 227, 255, 255, 234, 234, 236, 255, 217, 230, 240, 255, 181, 247, 255, 255, 99, 47, 31, 255, 116, 78, 65, 255, 157, 125, 114, 255, 55, 33, 29, 255, 147, 119, 113, 255, 154, 129, 112, 255, 95, 71, 62, 255, 108, 66, 54, 255, 193, 255, 255, 255, 179, 229, 252, 255, 166, 228, 255, 255, 233, 231, 235, 255, 164, 231, 255, 255, 255, 233, 228, 255, 165, 228, 255, 255, 233, 231, 235, 255, 162, 227, 255, 255, 173, 229, 254, 255, 179, 228, 252, 255, 174, 228, 253, 255, 220, 232, 239, 255, 255, 233, 228, 255, 223, 233, 239, 255, 172, 228, 253, 255, 175, 231, 255, 255, 222, 231, 238, 255, 224, 232, 238, 255, 255, 233, 226, 255, 237, 234, 235, 255, 167, 229, 255, 255, 228, 231, 236, 255, 174, 228, 253, 255, 193, 255, 255, 255, 111, 63, 46, 255, 146, 116, 105, 255, 120, 81, 67, 255, 159, 131, 119, 255, 154, 123, 111, 255, 61, 37, 33, 255, 119, 71, 52, 255, 186, 247, 255, 255, 179, 229, 252, 255, 170, 228, 254, 255, 229, 232, 236, 255, 255, 233, 225, 255, 228, 233, 237, 255, 164, 227, 255, 255, 229, 232, 236, 255, 255, 233, 226, 255, 255, 234, 229, 255, 165, 228, 255, 255, 173, 228, 253, 255, 167, 228, 255, 255, 233, 234, 237, 255, 245, 232, 234, 255, 166, 228, 255, 255, 234, 230, 236, 255, 206, 232, 247, 255, 158, 227, 255, 255, 235, 234, 236, 255, 164, 227, 255, 255, 178, 228, 252, 255, 174, 228, 253, 255, 179, 229, 252, 255, 186, 247, 255, 255, 111, 63, 46, 255, 145, 115, 104, 255, 113, 74, 61, 255, 154, 125, 111, 255, 59, 34, 29, 255, 72, 47, 42, 255, 119, 71, 52, 255, 186, 247, 255, 255, 175, 228, 252, 255, 221, 232, 241, 255, 164, 227, 255, 255, 235, 234, 235, 255, 172, 230, 255, 255, 220, 231, 241, 255, 164, 227, 255, 255, 228, 233, 237, 255, 216, 231, 240, 255, 255, 233, 225, 255, 231, 234, 236, 255, 178, 228, 252, 255, 172, 229, 255, 255, 225, 231, 237, 255, 162, 228, 255, 255, 255, 233, 228, 255, 255, 233, 228, 255, 224, 233, 238, 255, 220, 230, 240, 255, 220, 231, 239, 255, 168, 228, 255, 255, 179, 229, 252, 255, 179, 229, 252, 255, 193, 255, 255, 255, 110, 63, 45, 255, 154, 124, 115, 255, 141, 112, 101, 255, 159, 127, 120, 255, 99, 74, 70, 255, 72, 49, 44, 255, 118, 70, 52, 255, 193, 255, 255, 255, 179, 229, 252, 255, 175, 228, 252, 255, 178, 228, 252, 255, 169, 229, 255, 255, 255, 235, 227, 255, 165, 230, 255, 255, 166, 227, 255, 255, 213, 232, 244, 255, 217, 230, 239, 255, 232, 234, 236, 255, 163, 227, 255, 255, 178, 228, 252, 255, 182, 230, 252, 255, 171, 228, 254, 255, 209, 232, 247, 255, 248, 233, 231, 255, 255, 234, 229, 255, 245, 234, 232, 255, 221, 232, 238, 255, 255, 233, 226, 255, 237, 234, 235, 255, 173, 228, 253, 255, 186, 247, 255, 255, 106, 49, 27, 255, 120, 83, 70, 255, 125, 91, 76, 255, 141, 110, 100, 255, 156, 127, 111, 255, 87, 61, 53, 255, 70, 45, 40, 255, 127, 89, 74, 255, 106, 48, 26, 255, 186, 247, 255, 255, 179, 229, 252, 255, 179, 229, 252, 255, 164, 228, 255, 255, 255, 232, 228, 255, 255, 233, 227, 255, 255, 235, 229, 255, 166, 227, 255, 255, 180, 231, 255, 255, 174, 230, 255, 255, 228, 231, 236, 255, 173, 228, 253, 255, 234, 231, 235, 255, 163, 228, 255, 255, 232, 227, 228, 255, 155, 225, 254, 255, 255, 237, 231, 255, 231, 231, 237, 255, 162, 228, 255, 255, 237, 234, 235, 255, 163, 227, 255, 255, 179, 229, 252, 255, 193, 255, 255, 255, 111, 64, 45, 255, 120, 87, 80, 255, 118, 82, 69, 255, 143, 112, 102, 255, 147, 115, 104, 255, 64, 39, 35, 255, 69, 44, 39, 255, 111, 85, 83, 255, 116, 72, 54, 255, 193, 255, 255, 255, 186, 247, 255, 255, 173, 228, 253, 255, 240, 233, 240, 255, 220, 232, 239, 255, 255, 233, 228, 255, 225, 234, 241, 255, 171, 230, 255, 255, 217, 218, 209, 255, 162, 224, 249, 255, 169, 228, 255, 255, 171, 229, 255, 255, 229, 233, 237, 255, 255, 240, 239, 255, 122, 164, 91, 255, 190, 241, 255, 255, 179, 238, 255, 255, 255, 233, 232, 255, 168, 224, 243, 255, 170, 228, 251, 255, 233, 232, 236, 255, 189, 255, 255, 255, 104, 46, 24, 255, 145, 110, 103, 255, 143, 114, 103, 255, 116, 83, 69, 255, 142, 111, 100, 255, 153, 122, 113, 255, 105, 72, 61, 255, 108, 74, 63, 255, 152, 120, 106, 255, 119, 84, 70, 255, 118, 70, 52, 255, 106, 49, 27, 255, 191, 255, 255, 255, 207, 226, 232, 255, 161, 226, 253, 255, 235, 235, 239, 255, 237, 226, 224, 255, 174, 235, 255, 255, 75, 134, 30, 255, 255, 246, 252, 255, 240, 244, 255, 255, 210, 227, 234, 255, 161, 226, 253, 255, 249, 245, 255, 255, 117, 176, 137, 255, 63, 117, 0, 255, 99, 155, 82, 255, 198, 253, 255, 255, 106, 162, 96, 255, 193, 242, 255, 255, 188, 254, 255, 255, 106, 49, 27, 255, 121, 84, 71, 255, 125, 95, 80, 255, 123, 92, 76, 255, 131, 95, 84, 255, 141, 112, 101, 255, 146, 116, 104, 255, 147, 116, 105, 255, 102, 69, 58, 255, 130, 96, 80, 255, 125, 92, 84, 255, 62, 41, 37, 255, 131, 92, 77, 255, 113, 55, 41, 255, 113, 187, 128, 255, 193, 242, 255, 255, 188, 243, 255, 255, 120, 162, 91, 255, 241, 254, 255, 255, 85, 142, 58, 255, 60, 123, 14, 255, 84, 145, 60, 255, 115, 173, 122, 255, 197, 246, 255, 255, 145, 201, 184, 255, 73, 127, 18, 255, 95, 151, 71, 255, 212, 255, 255, 255, 58, 113, 0, 255, 141, 192, 162, 255, 65, 120, 3, 255, 120, 195, 131, 255, 122, 75, 66, 255, 104, 72, 61, 255, 122, 88, 80, 255, 134, 103, 99, 255, 129, 97, 83, 255, 144, 114, 103, 255, 143, 112, 100, 255, 158, 127, 120, 255, 105, 75, 65, 255, 122, 84, 71, 255, 134, 99, 83, 255, 96, 70, 66, 255, 63, 42, 39, 255, 131, 82, 78, 255, 92, 114, 44, 255, 66, 129, 10, 255, 120, 175, 124, 255, 133, 185, 153, 255, 62, 119, 2, 255, 132, 188, 150, 255, 103, 157, 80, 255, 81, 135, 40, 255, 72, 125, 32, 255, 76, 130, 23, 255, 71, 128, 28, 255, 58, 116, 34, 255, 77, 131, 40, 255, 75, 130, 22, 255, 84, 139, 45, 255, 35, 91, 14, 255, 75, 148, 42, 255, 67, 23, 32, 255, 129, 90, 77, 255, 106, 73, 63, 255, 116, 85, 73, 255, 141, 109, 90, 255, 101, 68, 58, 255, 123, 89, 81, 255, 99, 67, 56, 255, 163, 135, 122, 255, 100, 68, 57, 255, 105, 73, 63, 255, 122, 84, 71, 255, 120, 89, 76, 255, 111, 73, 66, 255, 91, 109, 51, 255, 133, 78, 79, 255, 97, 125, 55, 255, 60, 125, 26, 255, 54, 114, 23, 255, 81, 137, 45, 255, 56, 114, 24, 255, 55, 110, 27, 255, 92, 146, 50, 255, 55, 112, 31, 255, 78, 133, 43, 255, 56, 110, 31, 255, 73, 127, 40, 255, 54, 111, 31, 255, 79, 134, 43, 255, 51, 106, 29, 255, 46, 108, 27, 255, 82, 53, 48, 255, 78, 104, 42, 255, 111, 72, 66, 255, 107, 74, 64, 255, 122, 86, 72, 255, 120, 84, 71, 255, 106, 73, 63, 255, 119, 88, 75, 255, 103, 69, 58, 255, 149, 119, 107, 255, 148, 117, 106, 255, 103, 70, 59, 255, 122, 86, 72, 255, 121, 84, 71, 255, 124, 87, 73, 255, 112, 74, 67, 255, 63, 40, 37, 255, 129, 83, 76, 255, 101, 123, 57, 255, 56, 114, 32, 255, 59, 113, 33, 255, 76, 130, 42, 255, 53, 109, 32, 255, 46, 100, 27, 255]) {'greyscale': False, 'alpha': True, 'planes': 4, 'bitdepth': 8, 'interlace': 0, 'size': (32, 32), 'gamma': 0.45455, 'physical': _Resolution(x=3780, y=3780, unit_is_meter=True)}
以及打印后的错误:
Traceback (most recent call last):
File "C:\Users\user\Desktop\img EPIC compiler\img compiler.py", line 18, in <module>
writer = png.Writer(w, h, **metadata)
TypeError: Writer.__init__() got an unexpected keyword argument 'physical'
导致错误的原因是什么?
因此无法识别元数据中的值 'physical'
,您需要做的就是对该值使用 .pop()
将其删除。所以如果你像这样使用它:
import array
point = (2, 10) # coordinates of pixel to be painted red
reader = png.Reader(filename='testt.png')
w, h, pixels, metadata = reader.read_flat()
metadata.pop('physical')
print(w, h, pixels, metadata)
pixel_byte_width = 4 if metadata['alpha'] else 3
pixel_position = point[0] + point[1] * w
new_pixel_value = (255, 0, 0, 0) if metadata['alpha'] else (255, 0, 0)
pixels[pixel_position * pixel_byte_width : (pixel_position + 1) * pixel_byte_width] = array.array('B', new_pixel_value)
output = open('img2.txt', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()
它不再引发错误,并完成了工作。