python存储16bit和32bit图像的实例

yipeiwu_com6年前Python基础

笔记:python中存储16bit和32bit图像的方法。

说明:主要是利用scipy库和pillow库,比较其中的不同。

'''
测试16bit和32bit图像的python存储方法
'''
import numpy as np
 
import scipy.misc
from PIL import Image
 
# 用已有的8bit和16bit图作存储测试
path16 = 'D:\Py_exercise\lena16.tif'
path8 = 'D:\Py_exercise\lena8.tif'
tif16 = scipy.misc.imread(path16)  #<class 'numpy.uint16'>
tif8 = scipy.misc.imread(path8)   #<class 'numpy.uint8'>
print(np.shape(tif16),type(tif16[0,0])) 
print(np.shape(tif8),type(tif8[0,0])) 
print()
 
save16 = 'D:\Py_exercise\lena16_save.tif'
save8 = 'D:\Py_exercise\lena8_save.tif'
scipy.misc.imsave(save16, tif16)   #--> 8bit
scipy.misc.imsave(save8, tif8)   #--> 8bit
 
 
# Create a mat which is 64 bit float
nrows = 512
ncols = 512
np.random.seed(12345)
y = np.random.randn(nrows, ncols)*65535 #<class 'numpy.float64'>
print(type(y[0,0]))
print()
 
# Convert y to 16 bit unsigned integers
z16 = (y.astype(np.uint16))    #<class 'numpy.uint16'>
print(type(z16[0,0]))
print()
 
# 用产生的随机矩阵作存储测试
save16 = 'D:\Py_exercise\lena16_save1.tif'
scipy.misc.imsave(save16, z16)     #--> 8bit
 
im = Image.frombytes('I;16', (ncols,nrows), y.tostring())
im.save('D:\Py_exercise\lena16_save21.tif') #--> 16bit
im = Image.fromarray(y)      
im.save('D:\Py_exercise\lena16_save22.tif') #--> 32bit
im = Image.fromarray(z16)      
im.save('D:\Py_exercise\lena16_save23.tif') #--> 16bit
 
# 归一化后的np.float64仍然存成了uint8
zNorm = (z16-np.min(z16))/(np.max(z16)-np.min(z16)) #<class 'numpy.float64'>
print(type(zNorm[0,0]))
save16 = 'D:\Py_exercise\lena16_save11.tif'
scipy.misc.imsave(save16, zNorm)    #--> 8bit
 
# 归一化后的np.float64直接转8bit或16bit都会超出阈值,要*255或*65535
# 如果没有astype的位数设置,会直接存成32bit
zImg = (zNorm*65535).astype(np.uint16) 
im = Image.fromarray(zImg)
im.save('D:\Py_exercise\lena16_save31.tif') #--> 16bit
im = Image.fromarray(zNorm)
im.save('D:\Py_exercise\lena16_save32.tif') #--> 32bit(0~1)

以上这篇python存储16bit和32bit图像的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python通过移动端访问查看电脑界面

python通过移动端访问查看电脑界面

这篇文章主要介绍了python通过移动端访问查看电脑界面,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 看上心意的小姐姐,想看她平时都...

Python列表删除元素del、pop()和remove()的区别小结

前言 在python列表的元素删除操作中, del, pop(), remove()很容易混淆, 下面对三个语句/方法作出解释 del语句 del语句可以删除任何位置处的列表元素, 若...

使用Python对IP进行转换的一些操作技巧小结

Python Socket模块中包含一些有用IP转换函数,说明如下: socket.ntohl(x) // 类似于C语言的ntohl(x) 把32位正整数从网络序转换成...

Python socket实现多对多全双工通信的方法

服务器:#server.py #!/usr/bin/env python #-*-coding:utf-8-*- import sys import struct#将字符串打包为二进...

Win10系统下安装labelme及json文件批量转化方法

Win10系统下安装labelme及json文件批量转化方法

一、安装环境:windows10,anaconda3,python3.6 由于框架maskrcnn需要json数据集,在没安装labelme环境和跑深度学习之前,我安装的是anacond...