Convert fp16 to png

# fp16_to_png.py
import numpy as np
import cv2
import scipy.misc


image_dir='./'

img_height = 604  #1208  #604 # Height of the model input images
img_width = 960   #1920  #960  # Width of the model input images

image_file = "./0.fp16"

image = np.fromfile(image_file, dtype=np.float32)  #change np.float16 to np.float32
print("The fp16 format image is:\n{}".format(image))
print("The fp16 format image size is:\n{}".format(image.size))

image = np.reshape(image,[3,img_height, img_width]).astype(np.float32)

image = image * 255.0
print("image is:\n{}".format(image))
print("image size is:\n{}".format(image.size))

image=np.transpose(image, (1,2,0))
print("image is:\n{}".format(image))
print("image size is:\n{}".format(image.size))

# use cv2
cv2.imwrite("./0.png" , image)

#use scipy
#scipy.misc.imsave("./0.png", image)

 

Advanced version:

 

$ sudo cat fp16_to_png.py
import numpy as np
import cv2
import scipy.misc
import glob


image_dir='./'

img_height = 64  #1208  #604 # Height of the model input images
img_width = 64   #1920  #960  # Width of the model input images

#image_file = "./5935ccbef51cd200a32ab0eb.fp16"

for image_file in glob.glob("./*fp16"):
    png_file = image_file.replace('.fp16','.png')
    print(png_file)

    image = np.fromfile(image_file, dtype=np.float16)  #change np.float16 to np.float32
    print("The fp16 format image is:\n{}".format(image))
    print("The fp16 format image size is:\n{}".format(image.size))

    image = np.reshape(image,[3,img_height, img_width]).astype(np.float32)

    image = image * 255.0
    print("image is:\n{}".format(image))
    print("image size is:\n{}".format(image.size))

    image=np.transpose(image, (1,2,0))
    print("image is:\n{}".format(image))
    print("image size is:\n{}".format(image.size))

    # use cv2
    #cv2.imwrite("./0.png" , image)
    cv2.imwrite(png_file , image)

  

 

posted on 2019-01-30 17:47  cdekelon  阅读(327)  评论(0)    收藏  举报

导航