0

I have an image which I want to get the RGB matrix for, and since I'm kinda new to OpenCV and Python I was looking how to do it, and I found the following code:

img_file = 'baboon.png'

img = cv2.imread(img_file, cv2.IMREAD_COLOR)           # rgb
alpha_img = cv2.imread(img_file, cv2.IMREAD_UNCHANGED) # rgba
gray_img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)  # grayscale


print type(img)
print 'RGB shape: ', img.shape        # Rows, cols, channels
print 'ARGB shape:', alpha_img.shape
print 'Gray shape:', gray_img.shape
print 'img.dtype: ', img.dtype
print 'img.size: ', img.size

After reading the code, and kinda understanding it, I'm getting the following error:

File "vision.py", line 10
    print type(img)
             ^
SyntaxError: invalid syntax

Could someone explain that error? Or if there is another better way to get the RGB matrix?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Are you using Python 2 or 3? That code is in Python 2. – AMC Feb 04 '20 at 01:24
  • This is a duplicate of: https://stackoverflow.com/q/826948/11301900, https://stackoverflow.com/q/937491/11301900, https://stackoverflow.com/q/25445439/11301900. – AMC Feb 04 '20 at 01:26
  • 3
    Does this answer your question? [Syntax error on print with Python 3](https://stackoverflow.com/questions/826948/syntax-error-on-print-with-python-3) – AMC Feb 04 '20 at 01:26

2 Answers2

0

The syntax for print changed with Python3, and now requires parenthesis. Try

print(type(img))

Also, you'll soon discover that OpenCV/cv2 orders the layers BGR, not the RGB the comments suggest you expect.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • Huh, I see, thanks a lot, but now I get this issue when running the program I don't get any information, only blank. RGB shape: ARGB shape: Gray shape: img.dtype: img.size: Is there any reason to that? – Sergio Flores Feb 04 '20 at 00:07
0

if you are using python3 then replace your all print statements to print()

img_file = 'baboon.png'
img = cv2.imread(img_file,cv2.IMREAD_COLOR) 
alpha_img = cv2.imread(img_file,cv2.IMREAD_UNCHANGED) # rgba   
gray_img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE) # grayscale

print(type(img))
print('RGB shape: ', img.shape) # Rows, cols, channels
print('ARGB shape:', alpha_img.shape)
print('Gray shape:', gray_img.shape)   
print('img.dtype: ', img.dtype)
print('img.size: ', img.size)