Create a copy of an image - python

17,452

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])
Share:
17,452
David Lasry
Author by

David Lasry

I'm an Android Developer.

Updated on June 04, 2022

Comments

  • David Lasry
    David Lasry almost 2 years

    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?