简体   繁体   中英

What is this data format? and how to convert from numpy array?

I tried to do a pattern recognition assignment using this data format in txtfile.

3.700000000000000000e+01

But i only managed to wrote this.

import numpy
import PIL

# Convert Image to array
img = PIL.Image.open("imagefilename.png").convert("L")
arr = numpy.array(img)

and the output i get in array is this.

[[ 85  85  86 ..., 194 196 194]
 [ 84  84  85 ..., 194 196 194]
 [ 84  85  86 ..., 193 195 195]
 ..., 
 [177 177 177 ..., 162 162 162]
 [174 174 173 ..., 163 163 163]
 [  1   1   1 ...,   0   0   0]]

so my question is what is this format? 3.700000000000000000e+01 which follow my lecturer's sample in txt file.

and how to convert it from an RGB image?

That is most probably scientific notation:

3.7e+01 = 3.7 * 10**1
2.8e+03 = 2.8 * 10**3 

etc. See here for the other way round: Display a decimal in scientific notation and here for more explanation: https://en.wikipedia.org/wiki/Scientific_notation

If you put it into a variable, python will know it:

d = 3.7e6
print(d)

Output:

3700000.0  

As for how to convert it to an image: it is a number - unless you have much more of them they won't make an image.

Most image formats I know of use triplets/quadruplets of ints (RGB/RGBA) or/and discrete values for widht*height.

You can transform the byte values into floats if you prefer to work with them that way, but my hedged guess is they only do value/255.0 to achive this.

Read more here: http://scikit-image.org/docs/dev/user_guide/data_types.html


To convert from np array on ints to np array of floats:

import numpy as np

arr = np.array([5,7,200,255])
print(arr)

arr = arr / 255.0
print (arr)

Output:

[  5   7 200 255]
[ 0.01960784  0.02745098  0.78431373  1.        ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM