numpy.array of an I;16 Image file

2024/11/15 10:48:41

I want to use TIFF images to effectively save large arrays of measurement data. With setting them to mode="I;16" (corresponding to my 16 bit data range), they yield 2MB files (~1000x1000 "pixel"). Which is good.

However I am having troubles reconverting them into arrays when it comes to analysing them. For 32bit data (-> "I") the numpy.array command works fine. In case of "I;16" the result is a 0D numpy array with the TIFF as the [0,0] entry.

Is there a way to get that to work? I would really like to avoid using 32bit images, as I don't need the range and it doubles the HDD space required (lots and lots of those measurements planned...)

Answer

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

from PIL import Image
import numpy as npdata = 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 tifffilefp = r'path\to\image\image.tif'with tifffile.TIFFfile(fp) as tif:data = tif.asarray()
https://en.xdnf.cn/q/71963.html

Related Q&A

Namespace packages and pip install -e

I have a ns.pkg2 package that depends on ns.pkg1 package. I make a fork of it, publish it to git and want to install my version into my virtualenv. I use pip install -e mygit and end up with ns.pkg in …

Python sys.argv out of range, dont understand why

I have a script that Ive been using for a some time to easily upload files to my server. It has been working great for a long time, but I cant get it to work on my new desktop computer. The code is sim…

Error calling BashOperator: Bash command failed

Here are my dag file and BashOperator task:my_dag = { dag_id = my_dag, start_date = datetime(year=2017, month=3, day=28), schedule_interval=01***, }my_bash_task = BashOperator( task_id="my_bash_t…

Match unescaped quotes in quoted csv

Ive looked at several of the Stack Overflow posts with similar titles, and none of the accepted answers have done the trick for me.I have a CSV file where each "cell" of data is delimited by …

Creating RDF file using csv file as input

I need to convert a csv file to rdf with rdflib, I already have the code that reads the csv but I do not know how to convert it to rdf.I have the following code:import csv from rdflib.graph import Grap…

Explicit vertex position in python graph-tool

I am using python graph-tool. To draw graphs, it uses graph_draw function. I want to send vertex positions explicitly to dot engine. It turns out that I can pass a property map named pos. I tried defin…

Add jar to pyspark when using notebook

Im trying the mongodb hadoop integration with spark but cant figure out how to make the jars accessible to an IPython notebook.Here what Im trying to do:# set up parameters for reading from MongoDB via…

how do I create a python list with a negative index

Im new to python and need to create a list with a negative index but have not been successful so far.Im using this code:a = [] for i in xrange( -20, 0, -1 ):a[i] = -(i)log.info(a[{i}]={v}.format(i=i, v…

Select subset of Data Frame rows based on a list in Pandas

I have a data frame df1 and list x:In [22] : import pandas as pd In [23]: df1 = pd.DataFrame({C: range(5), "B":range(10,20,2), "A":list(abcde)}) In [24]: df1 Out[24]:A B C 0 a …

convert csv to json (nested objects)

I am new to python, and I am having to convert a csv file to json in following format:CSV File :firstname, lastname, email, customerid, dateadded, customerstatus john, doe, [email protected], 124,26/11…