How to write bytes as a b' string to a file?
12:34 23 Nov 2025
from PIL import Image
img = Image.open('some/file.png')
img = img.convert(mode='1')
imgbytes = img.tobytes()
with open('filepath', 'w') as file_out:
file_out.write(imgbytes)

I would like to write the sequence of bytes in imgbytes, as a string preceded with b', to a file. I don't need to encode it.

write() doesn't accept bytes nor bytearray parameters.

python byte python-bytearray