Shaooly
Shaooly

Reputation: 1

Is there a way to open an image as bytes and return it to a jpg file?

image = open(filepath, 'rb')
new_image = open("new_image_path.jpg", 'w', encoding="ISO-8859-1")
new_image.write(image.read().decode(encoding="ISO-8859-1"))

the new image I get is exactly the same if I open it in notepad++ but the new image is corrupted and can't be open for some reason.

Upvotes: 0

Views: 742

Answers (1)

Extrawdw
Extrawdw

Reputation: 339

You have to also use 'wb' because it's not a text file. You should not decode and encode because it's not a text file. The functional code could be:

image = open(filepath, 'rb')
new_image = open("new_image_path.jpg", 'wb')
new_image.write(image.read())

Upvotes: 1

Related Questions