[pillow]讀寫二進制圖片

讀入二進制圖片

from PIL import Image
import numpy as np

fd = open("input.raw", 'rb')
image_bytes = fd.read()
fd.close()

nparr = np.asarray(bytearray(image_bytes), dtype="uint8")
image = nparr.reshape((960, 540)) # (height, width)
im = Image.fromarray(image, mode="L")
im.save("out.jpg")

寫入二進制圖片

from PIL import Image
import numpy as np

input_file = "input.jpg"
img = Image.open(input_file).convert("L")
nparr = np.asarray(img)
output_file = input_file.split('.')[0] + ".raw"
with open(output_file, 'wb') as f:
    nparr.astype('uint8').tofile(f)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章