Reputation: 1407
I'm trying to read data from an image file and writing it to a new file - to make a copy.
Here is my code to read the data of the original image and to write every byte to the new image:
file = open("image2.png", "w")
with open("image.png", "rb") as f:
while True:
byte = f.read(1)
if not byte:
break
file.write(byte)
Now, it does create a new file named "image2.png" but when I try to open it I get an error that says the image has been corrupted or damaged.
How can I read the data of an image and writing it to a new file?
Upvotes: 2
Views: 25640
Reputation: 24099
Using shutil
:
import shutil
shutil.copy("image.png","image2.png")
Or as you have it:
file = open("image2.png", "wb")
with open("image.png", "rb") as f:
while True:
byte = f.read(1)
if not byte:
break
file.write(byte[0])
Upvotes: 10