from PIL import Image
import numpy as np
import binascii
import cv2
def gray2jpg():
w,h = 256, 256
data = np.ravel(np.empty((1,h,w,1),dtype="int8"))
file = open("grey.bin","rb")
for i in range(w*h):
data[i] = int(binascii.b2a_hex(file.read(1)).decode(),16)
file.close()
img = Image.fromarray(np.ravel(data).reshape(h,w),mode="L")
img.save(path)
img.show()
def rgb2jpg():
w = 256
H = 256
file = "rgb888.bin"
f = open(file, "rb+")
content_bytes = f.read()
content_bytearray = bytearray(content_bytes)
c = np.reshape( content_bytearray,[H,W,3])
data = np.zeros((H,W,3),dtype=np.uint8)
data[:,:,0] = c[:,:,2]
data[:,:,1] = c[:,:,1]
data[:,:,2] = c[:,:,0]
img = Image.fromarray(data,"RGB")
img.save("demp.jpg")
图像数据转换
于 2022-02-10 17:18:17 首次发布
这篇博客介绍了如何使用Python的PIL和numpy库将灰度图像转换为JPEG格式,并展示了从RGB二进制文件读取并转换为JPEG图片的过程。通过读取二进制数据,重塑数组,然后利用Image模块保存和显示图像。
摘要由CSDN通过智能技术生成