Python Read/Write Binary Image Files

python day 32

We always use “copy” and “paste” buttons to duplicate images. Today let’s use Python to code to do so.

  • Overwrite the picture “b2” as “binary”
  • Copy and Paste the picture “binary” with an empty picture “b2”

*rb: read as binary
*wb: write as binary

def r_binary():
    try:
        with open("C:/Users/Desktop/binary.jpg" ,"rb") as fs1:
            data=fs1.read()
            print(type(data))
        with open("C:/Users/Desktop/b2.jpg" ,"wb") as fs2:
            fs2.write(data)
    except FileNotFoundError as e:
        print('No file existed -_-|||')
    except IOError as e:
        print('Something is wrong @_@') 
    print("Done! ~_~")

r_binary()
<class 'bytes'>
Done! ~_~

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: