what does [:, :, ::-1] mean in python?
Question:
I am running a program and I have the code below. But I do not know what does that [:, :, ::-1]
do exactly. I get the following error when I run the program so understanding the function of [:, :, ::-1]
will help me to debug. Thanks.
while True:
ix = np.random.choice(np.arange(len(lists)), batch_size)
imgs = []
labels = []
for i in ix:
# images
img_path = img_dir + lists.iloc[i, 0] + '.png'
original_img = cv2.imread(img_path)[:, :, ::-1]
resized_img = cv2.resize(original_img, dims+[3])
array_img = img_to_array(resized_img)/255
imgs.append(array_img)
error:
original_img = cv2.imread(img_path)[:, :, ::-1]
TypeError: 'NoneType' object is not subscriptable
Answers:
Let’s say your image has three planes – R, G and B. Then the command [:, :, ::-1]
will reverse the order of the color planes, making them B, G and R. This is done because by convention, OpenCV used BGR format (see here). So you are converting BGR to RGB simply because we like RGB nowadays.
However, your error has nothing do with the understanding of the command. The problem is that the cv2.imread()
command cannot read the image and it is returning None
. It is possible that you have given the wrong path.
This is numpy-specific and will not work for most python objects. The :
means “take everything in this dimension” and the ::-1
means “take everything in this dimension but backwards.” Your matrix has three dimensions: height, width and color. Here you’re flipping the color from BGR to RGB. This is necessary because OpenCV has colors in BGR (blue/green/red) order, while most other imaging libraries have them in RGB order. This code will switch the image from OpenCV format to whatever format you’ll display it in.
I am running a program and I have the code below. But I do not know what does that [:, :, ::-1]
do exactly. I get the following error when I run the program so understanding the function of [:, :, ::-1]
will help me to debug. Thanks.
while True:
ix = np.random.choice(np.arange(len(lists)), batch_size)
imgs = []
labels = []
for i in ix:
# images
img_path = img_dir + lists.iloc[i, 0] + '.png'
original_img = cv2.imread(img_path)[:, :, ::-1]
resized_img = cv2.resize(original_img, dims+[3])
array_img = img_to_array(resized_img)/255
imgs.append(array_img)
error:
original_img = cv2.imread(img_path)[:, :, ::-1]
TypeError: 'NoneType' object is not subscriptable
Let’s say your image has three planes – R, G and B. Then the command [:, :, ::-1]
will reverse the order of the color planes, making them B, G and R. This is done because by convention, OpenCV used BGR format (see here). So you are converting BGR to RGB simply because we like RGB nowadays.
However, your error has nothing do with the understanding of the command. The problem is that the cv2.imread()
command cannot read the image and it is returning None
. It is possible that you have given the wrong path.
This is numpy-specific and will not work for most python objects. The :
means “take everything in this dimension” and the ::-1
means “take everything in this dimension but backwards.” Your matrix has three dimensions: height, width and color. Here you’re flipping the color from BGR to RGB. This is necessary because OpenCV has colors in BGR (blue/green/red) order, while most other imaging libraries have them in RGB order. This code will switch the image from OpenCV format to whatever format you’ll display it in.