创建一个图像的副本 - python

时间:2014-12-16 23:33:59

标签: python image binary byte

我试图从图像文件中读取数据并将其写入新文件 - 进行复制。

这是我的代码,用于读取原始图像的数据并将每个字节写入新图像:

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)

现在,它确实创建了一个名为" image2.png"的新文件。但当我尝试打开它时,我收到一个错误,表示图像已损坏或损坏。

如何读取图像数据并将其写入新文件?

1 个答案:

答案 0 :(得分:4)

使用shutil

import shutil
shutil.copy("image.png","image2.png")

或者你拥有它:

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])
相关问题