python 二进制

2019-05-28  本文已影响0人  小姐姐催我改备注

1.二进制

在电脑的数据存储中任何东西都可以用二进制来表示,这里我们介绍一种用二进制读写的方式。

  import os
import pickle
srcfloder = "./exm.py"
with open(srcfloder,'rb') as fin:
  a = fin.read()
  print(a)
  with open("./exm.bin",'wb') as f:
    pickle.dump(a,f,-1)

with open("exm.bin",'rb') as f:
  print(a)
  b = pickle.load(f)
  print(b)

b'import os\r\nimport pickle\r\nsrcfloder = "./exm.py"\r\nwith open(srcfloder,\'rb\') as fin:\r\n  a = fin.read()\r\n  print(a)\r\n  with open("./exm.bin",\'wb\') as f:\r\n    pickle.dump(a,f,-1)\r\n\r\nwith open("exm.bin",\'rb\') as f:\r\n  print(a)\r\n  b = pickle.load(f)\r\n  print(b)'
b'import os\r\nimport pickle\r\nsrcfloder = "./exm.py"\r\nwith open(srcfloder,\'rb\') as fin:\r\n  a = fin.read()\r\n  print(a)\r\n  with open("./exm.bin",\'wb\') as f:\r\n    pickle.dump(a,f,-1)\r\n\r\nwith open("exm.bin",\'rb\') as f:\r\n  print(a)\r\n  b = pickle.load(f)\r\n  print(b)'
b'import os\r\nimport pickle\r\nsrcfloder = "./exm.py"\r\nwith open(srcfloder,\'rb\') as fin:\r\n  a = fin.read()\r\n  print(a)\r\n  with open("./exm.bin",\'wb\') as f:\r\n    pickle.dump(a,f,-1)\r\n\r\nwith open("exm.bin",\'rb\') as f:\r\n  print(a)\r\n  b = pickle.load(f)\r\n  print(b)'

这里我们可以看到读入和写出是一样的,在这里我们需要注意,我们以什么方式来读取文件,就会以什么方式来写出文件。

import os
import pickle
import mxnet as mx
import cv2 as cv
import numpy as np
from  mxnet import nd
from PIL import Image
with open("./1.jpg",'rb') as fin:
  a = fin.read()
  img = mx.image.imdecode(a,to_rgb=0)
  img = nd.transpose(img, axes=(2, 0, 1))
  xiaowang = float(img.asnumpy())
  cv.imwrite("2.jpg", xiaowang)
  print(a)
  with open("./tupian.bin", 'wb') as f:
    pickle.dump(a, f, protocol=pickle.HIGHEST_PROTOCOL)

with open("tupian.bin",'rb') as f:
  print(a)
  b = pickle.load(f)
  img = mx.image.imdecode(b)
  img = nd.transpose(img, axes=(2, 0, 1))

  print(b)
上一篇 下一篇

猜你喜欢

热点阅读