1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to write bytes as a b' string to a file?

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página