简体   繁体   中英

Using Python's pillow/PIL library:

In the following code I am trying to do the following by using Python's pillow/PIL library:

  1. Read in an image file, store it as a matrix
  2. Access the red, blue, green channel which is an unsigned integer unit8 and convert each channel to float64

This is my first time using Python's pillow/PIL library and I just wanted to clarify if I have achieved these 2 things correctly.

Here my is my code I have produced:

import numpy as np
from PIL import Image
img = Image.open('house.jpg')
image = np.array(img)
arr[20,30]
red = np.float64(image[:,:, 0])
green = np.float64(image[:,:,1])
blue = np.float64(image[:,:,2])

and for instance, when I use

print(red)

I get the following output:

[[ 34.  41.  49. ...  22.  22.  22.]
 [ 28.  34.  41. ...  23.  23.  23.]
 [ 23.  26.  30. ...  24.  24.  24.]
 ...
 [ 32.  45.  57. ... 105.  97. 109.]
 [ 34.  32.  41. ... 100.  94. 113.]
 [ 33.  36.  52. ...  99.  90. 113.]]

You could, more simply, use:

import numpy as np
from PIL import Image

img = Image.open('house.jpg')
image=np.array(img,dtype=np.float64)   
...
...

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