python将字符串编码的12位图像转换为8位png [英] python convert 12 bit image encoded in a string to 8 bit png

查看:557
本文介绍了python将字符串编码的12位图像转换为8位png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从usb apogee相机读取的字符串,这是一个12位灰度图像,每个12位占据16位字的最低12位。
我想通过忽略最低的4位来从这个字符串创建一个8位的png。

I have a string that is read from a usb apogee camera that is a 12-bit grayscale image with the 12-bits each occupying the lowest 12 bits of 16-bits words. I want to create a 8-bit png from this string by ignoring the lowest 4 bits.

我可以将它转换为16位图像,其中使用PIL时最高4位始终为零

I can convert it to a 16-bit image where the highest 4 bits are always zero using PIL with

import Image

#imageStr is the image string
#imageSize is the image size 

img=Image.fromstring("I", imageSize, imageStr, "raw", "I;16", 0,1)
img.save("MyImage.png", "PNG")

无论如何我可以做类似于创建的事情一个8位的图像,没有完全解包字符串做算术和创建一个新的字符串?

Anyway can I do something similar to create a 8-bit image without completely unpacking the string doing arithmetic and making a new string?

编辑:Wumps关于转换图像的评论给了我一个想法,我做了它by

Wumps comment about converting an image gave me an idea, and I did it by

img = img.point(lambda i: i * 16, "L") #shifts by 4 bits and converts to 8-bit image.

谢谢Wump

推荐答案

Wump关于转换图片的评论给了我一个想法,我做到了

Wump's comment about converting an image gave me an idea, and I did it by

#shifts by 4 bits and converts to 8-bit image
img = img.point(lambda i: i * 16, "L") 

谢谢Wump

这篇关于python将字符串编码的12位图像转换为8位png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆