У меня есть файл PNG (прикреплен к этому сообщению). Все, что я хочу сделать, это нарисовать прямоугольник на изображении с помощью Python и сохранить изображение в новый файл. Вот код, который не работает:
Код: Выделить всё
import png
org_path = './arragon.png'
altered_path = './altered.png'
f = open(org_path, 'rb')
image = png.Reader(file=f)
width, height, rows, metadata = image.read()
for row in rows:
for i in range(len(row)):
row[i] = 255
writer = png.Writer(
width=width,
height=height,
bitdepth=metadata['bitdepth'],
greyscale=metadata['greyscale'],
alpha=metadata['alpha']
)
writer.write(open(altered_path, 'wb'), rows)
Код: Выделить всё
Traceback (most recent call last):
File "/home/miko/tmp/alter-image/./edit.py", line 23, in
writer.write(open(altered_path, 'wb'), rows)
File "/home/miko/.local/lib/python3.10/site-packages/png.py", line 670, in write
raise ProtocolError(
png.ProtocolError: ProtocolError: rows supplied (0) does not match height (450)
Код: Выделить всё
import png
f = open(org_path, 'rb')
image = png.Reader(file=f)
width, height, rows, metadata = image.read()
writer = png.Writer(
width=width,
height=height,
bitdepth=metadata['bitdepth'],
greyscale=metadata['greyscale'],
alpha=metadata['alpha']
)
writer.write(open(altered_path, 'wb'), rows)
Код: Выделить всё
Traceback (most recent call last):
File "/home/miko/tmp/alter-image/./edit.py", line 23, in
writer.write(open(altered_path, 'wb'), rows)
File "/home/miko/.local/lib/python3.10/site-packages/png.py", line 668, in write
nrows = self.write_passes(outfile, check_rows(rows))
File "/home/miko/.local/lib/python3.10/site-packages/png.py", line 703, in write_passes
return self.write_packed(outfile, rows)
File "/home/miko/.local/lib/python3.10/site-packages/png.py", line 738, in write_packed
for i, row in enumerate(rows):
File "/home/miko/.local/lib/python3.10/site-packages/png.py", line 658, in check_rows
raise ProtocolError(
png.ProtocolError: ProtocolError: Expected 633 values but got 211 values, in row 0
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-png-file
Мобильная версия