图像数据转换

这篇博客介绍了如何使用Python的PIL和numpy库将灰度图像转换为JPEG格式,并展示了从RGB二进制文件读取并转换为JPEG图片的过程。通过读取二进制数据,重塑数组,然后利用Image模块保存和显示图像。
摘要由CSDN通过智能技术生成

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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值