When I run this code, from original image (blue ring attached), at first show I see circle but after if I convert to RGB and do any manipulation(I tried selecting different columns, etc.), I always get a straight line (hopefully attached here too). Eventually I would like to be able to manipulate RGB channels, but for now just display every color channel as same matrix as original blue channel.
[
Code is:
from PIL import Image
import numpy as np
img = Image.open(r"test.jpg") # creating image object
img1 = img.convert("RGB") # using convert method for img1
img1.show()
def magColBlue(x):
x=np.array(x)
print("x first shape is ", np.shape(x))
x=x[:,:,0]
print("x 2nd shape is ", np.shape(x))
print(x)
return x
blueAmount =magColBlue(img1)
print("blue shape",np.shape(blueAmount))
imgBlue = Image.fromarray(np.asarray([blueAmount,blueAmount,blueAmount]), 'RGB')
imgBlue.show()
When I run this code, from original image (blue ring attached), at first show I see circle but after if I convert to RGB and do any manipulation(I tried selecting different columns, etc.), I always get a straight line (hopefully attached here too). Eventually I would like to be able to manipulate RGB channels, but for now just display every color channel as same matrix as original blue channel.
[
Code is:
from PIL import Image
import numpy as np
img = Image.open(r"test.jpg") # creating image object
img1 = img.convert("RGB") # using convert method for img1
img1.show()
def magColBlue(x):
x=np.array(x)
print("x first shape is ", np.shape(x))
x=x[:,:,0]
print("x 2nd shape is ", np.shape(x))
print(x)
return x
blueAmount =magColBlue(img1)
print("blue shape",np.shape(blueAmount))
imgBlue = Image.fromarray(np.asarray([blueAmount,blueAmount,blueAmount]), 'RGB')
imgBlue.show()
Share
edited Mar 11 at 14:24
Christoph Rackwitz
15.9k5 gold badges39 silver badges51 bronze badges
asked Mar 10 at 20:57
Gad11ngGad11ng
231 silver badge6 bronze badges
2 Answers
Reset to default 1I believe the issue lies in the following line:
Image.fromarray(np.asarray([blueAmount,blueAmount,blueAmount])
The numpy array you use here would have shape (3, x, y) rather than (x, y, 3) which may have been your intention.
Instead of
np.asarray([blueAmount,blueAmount,blueAmount])
you should use
np.dstack([blueAmount, blueAmount, blueAmount])
or np.stack()
with axis=2
argument.
That will have the right order of dimensions.