-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76_first_last_channels_image.py
More file actions
51 lines (38 loc) · 1.19 KB
/
76_first_last_channels_image.py
File metadata and controls
51 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Channels first is when image is represented as
# [channels][rows][columns]
# Channels last is when image is represented as
# [rows][colums][channels]
from numpy import expand_dims
from numpy import asarray
from PIL import Image
# load the image
img = Image.open('penguin_arade.jpg')
# convert the image to grayscale
img = img.convert(mode='L')
# convert to numpy array
data = asarray(img)
print(data.shape)
# add channels first
# Insert a new axis that will appear at the axis position in the expanded array shape.
# Position in the expanded axes where the new axis (or axes) is placed.
data_first = expand_dims(data, axis=0)
print(data_first.shape)
# add channels last
data_last = expand_dims(data, axis=2)
print(data_last.shape)
# change channels in image
from numpy import moveaxis
from numpy import asarray
from PIL import Image
# load the color image
img = Image.open('penguin_arade.jpg')
# convert to numpy array
data = asarray(img)
print(data.shape)
# change channels last to channels first format
# moveaxis: Moves the axis from source to destination
data = moveaxis(data, 2, 0)
print(data.shape)
# change channels first to channels last format
data = moveaxis(data, 0, 2)
print(data.shape)