0
votes

I have a black and white image that I am loading into python. If I use pillow or cv2 I get two different answers for the dimension of the NumPy array that is created. I understand that channel ordering (RGB vs BGR) is different from openCV and pillow but I don't think that's what's going on here

Is my image 2 dimensions as pillow represents it. Does this mean that openCV duplicates the values into a 3D array?

import cv2
from PIL import Image
import numpy as np

path = 'path/to/file.png'

#using pillow
img = Image.open(path)
img.size #(500,500)
img.mode # L
arr = np.array(img)
arr.shape #(500,500)

#using cv2
image = cv2.imread(path)
image.shape #(500,500,3)

If I run file in a bash terminal

$ file 'path/to/image.png'
path/to/image: PNG image data, 500 x 500, 8-bit grayscale, non-interlaced
1
What are the // doing? Are they supposed to be comments? - Mercury
Sorry, fixed it. Forgot which language I was in for a second. The comments show what the result of the line is - theastronomist
use imread(..., cv2.IMREAD_UNCHANGED) - Miki
mode "L" is equivalent to grayscale, so one channel, cv2.imread("pathimage", cv2.IMREAD_GRAYSCALE) is equivalent - Winux_RashidLadj

1 Answers

1
votes

The cv2.imread function takes two arguments: filename, and flag. The flag is set to cv2.IMREAD_COLOR (or 1) by default. PIL on the other hand automatically loads your image in grayscale mode, 'L'. If you want your cv2 image to work the same way, do:

image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

Or

image = cv2.imread(path, 0)

You can also convert your grayscale PIL image to 3 channels:

img = Image.open(path).convert('RGB')