numpy.array of an “I;16” Image file

情到浓时终转凉″ 提交于 2019-12-04 06:49:37

This should work (pillow/PIL solution, slow for 16-bit image, see below).

from PIL import Image
import numpy as np

data = np.random.randint(0,2**16-1,(1000,1000))
im = Image.fromarray(data)
im.save('test.tif')

im2 = Image.open('test.tif')
data2 = np.array(im2.getdata()).reshape(im2.size[::-1])

Another solution using tifffile by C. Gohlke (very fast):

import tifffile

fp = r'path\to\image\image.tif'

with tifffile.TIFFfile(fp) as tif:
    data = tif.asarray()

You could use GDAL + Numpy/Scipy to read raster images with 16bit channel data:

import gdal
tif = gdal.Open('path.tif')
arr = tif.ReadAsArray()

Convert an (ImageJ) TIFF to an 8 bit numpy array

im = numpy.array(Image.open('my.tiff'))
n = (im / numpy.amax(im) * 255).astype(numpy.uint8)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!