In [1]:
import cv2

In [2]:
im = cv2.imread('data/src/lena.jpg')

In [3]:
print(type(im))


<class 'numpy.ndarray'>

In [4]:
print(im.shape)


(225, 400, 3)

In [5]:
print(im.dtype)


uint8

In [6]:
im[:, :, (0, 1)] = 0

In [7]:
cv2.imwrite('data/dst/lena_opencv_red.jpg', im)


Out[7]:
True

In [8]:
cv2.imwrite('data/dst/lena_opencv_red_high.jpg', im, [cv2.IMWRITE_JPEG_QUALITY, 100])


Out[8]:
True

In [9]:
cv2.imwrite('data/dst/lena_opencv_red_low.jpg', im, [cv2.IMWRITE_JPEG_QUALITY, 50])


Out[9]:
True

In [10]:
im_gray = cv2.imread('data/src/lena.jpg', cv2.IMREAD_GRAYSCALE)
# im_gray = cv2.imread('data/src/lena.jpg', 0)

In [11]:
print(type(im_gray))


<class 'numpy.ndarray'>

In [12]:
print(im_gray.shape)


(225, 400)

In [13]:
print(im_gray.dtype)


uint8

In [14]:
cv2.imwrite('data/dst/lena_opencv_gray.jpg', im_gray)


Out[14]:
True

In [15]:
im_gray_read = cv2.imread('data/dst/lena_opencv_gray.jpg')

In [16]:
print(im_gray_read.shape)


(225, 400, 3)

In [17]:
import numpy as np

In [18]:
print(np.array_equal(im_gray_read[:, :, 0], im_gray_read[:, :, 1]))


True

In [19]:
print(np.array_equal(im_gray_read[:, :, 1], im_gray_read[:, :, 2]))


True

In [20]:
im = cv2.imread('xxxxxxx')

In [21]:
print(im)


None

In [22]:
# print(im.shape)
# AttributeError: 'NoneType' object has no attribute 'shape'

In [23]:
im = cv2.imread('data/src/sample.csv')

In [24]:
print(im)


None

In [25]:
im = cv2.imread('xxxxxxx')

In [26]:
if im:
    print('Image is read.')
else:
    print('Image is not read.')


Image is not read.

In [27]:
im = cv2.imread('xxxxxxx')

In [28]:
if not im:
    print('Image is not read.')
else:
    print('Image is read.')


Image is not read.