fr14
fr14

Reputation: 167

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.]]

Upvotes: 1

Views: 198

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207818

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)   
...
...

Upvotes: 2

Related Questions