3

I have this byte object in string format inside a .txtfile and I'm lost on how to convert it back into byte object, like img=b'\x00\x00\x00' so that I can write it back into an image.

The image is supposed to be a black square when back to image format.

b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xdd\x00\x00\x00\xdd\x08\x06\x00\x00\x00?\xc4w\x1a\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x00!tEXtCreation Time\x002019:07:22 19:10:08\xa0\x9d\xf4\xb4\x00\x00\x02\x80IDATx^\xed\xd3\xb1\r\xc00\x00\xc30\xa7\xff\xff\xdcf\xe8\r\x9aH\xc0\xf0\x05:\xdb\xde; \xf2\xfc\x0fDD\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x10\x13\x1d\xc4D\x071\xd1ALt\x90\xda>\xddB\x02\xb9L3W\xe8\x00\x00\x00\x00IEND\xaeB`\x82'

Opening the file .txt file with the byte object

file=open('filename.txt','r').read()

I tried this, but the file is in a string format

with open('new_pic.png','wb')as img:
    img.write(file)
2
  • Possible duplicate of PIL: Convert Bytearray to Image Commented Jul 27, 2019 at 10:52
  • @TAHASULTANTEMURI I looked into the link and I don't think it's a duplicate because mine is not a bytearray and I'm trying to convert in from a string to a byte format. Commented Jul 27, 2019 at 11:05

1 Answer 1

8

You should be reading the file as binary ("rb").

file = open('filename.txt', 'rb').read()
print(type(file))  # <class 'bytes'>
print(file)        # b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x06\x0

with open("image.png", "wb") as img:
    img.write(file)

Now, I don't know why you have image bytes stored in a text file, but a better implementation would be to use image-related modules like Pillow to read-write image files:

from PIL import Image

file = Image.open("input.png")
file.save("output.png")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.