I am trying to read 100 images into filenames[:100],and for every image
(1) crop
(2) Resize to 100 x 100
我试图将100个图像读入文件名[:100],并为每个图像(1)裁剪(2)调整为100 x 100
Convert all these images into np array. and plot a montage of that.
将所有这些图像转换为np数组。并绘制一个蒙太奇。
I am quite new to python, please let me know if "plt.imread(fname)[...,3]" is nothing but appending.
我是python的新手,如果“plt.imread(fname)[...,3]”只是附加,请告诉我。
thanks.
EDIT:
I want to create montage of 100 specific pictures. so my code will be something like this:-
编辑:我想创建100张特定图片的蒙太奇。所以我的代码将是这样的: -
import os
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import resize
dirname = "Images/n02087046-toy_terrier"
filenames = [os.path.join(dirname, fname)
for fname in os.listdir(dirname)
if '.jpg' in fname]
filenames = filenames[:100]
assert(len(filenames) == 100)
# Read every filename as an RGB image
imgs = [plt.imread(fname)[...,:3] for fname in filenames]
# Crop
imgs = [utils.imcrop_tosquare(img_i) for img_i in imgs]
# Then resize the square image to 100 x 100 pixels
imgs = [resize(img_i, (100, 100)) for img_i in imgs]
imgs = np.array(imgs).astype(np.float32)
imgs.shape
plt.figure(figsize=(10, 10))
#montage is a utility function.
plt.imshow(utils.montage(imgs, saveto='dataset.png'))
so wrt "plt.imread(fname)[...,3]",
what does it do and how to decompose it to more understandable way.
所以wrt“plt.imread(fname)[...,3]”,它做了什么以及如何将其分解为更容易理解的方式。
Thanks.
1 个解决方案
#1
1
The line
imgs = [plt.imread(fname)[...,:3] for fname in filenames]
creates a list of 100 images.
创建一个包含100个图像的列表。
plt.imread(fname)
reads an image and returns a numpy array. You may look at the shape of the array, which should be something like (n,m,3)
or (n,m,4)
, where n
and m
are whole numbers.
plt.imread(fname)读取一个图像并返回一个numpy数组。你可以看一下数组的形状,它应该是(n,m,3)或(n,m,4),其中n和m是整数。
The last axis of dimension 3 or 4 denotes the 3 colorchannels plus possibly a fouth channel which is the alpha (transparency).
尺寸3或4的最后一个轴表示3个颜色通道加上可能是α通道(透明度)的第四个通道。
The slicing [...,:3]
is then equivalent to [:,:,:3]
, which translates in words into "Take all values of the first two dimensions and take the first three values of the third dimension". I.e. you neglect the alpha channel, if present. The reason for doing this is most probably that you want to combine different images here and not all may have an alpha channel. So by neglecting it, you make sure not to run into problems later on.
切片[...,:3]等同于[:,:,:3],它将单词翻译成“获取前两个维度的所有值并获取第三个维度的前三个值”。即你忽略了alpha通道,如果存在的话。这样做的原因很可能是你想在这里组合不同的图像而不是所有图像都有alpha通道。因此,忽略它,你确保以后不会遇到问题。
In this example, you only take jpeg images. Jpeg images do not have an alpha channel and thus you may actually use
在此示例中,您只拍摄jpeg图像。 Jpeg图像没有alpha通道,因此您可能实际使用
imgs = [plt.imread(fname) for fname in filenames]
which should give you the same result.
哪个应该给你相同的结果。
#1
1
The line
imgs = [plt.imread(fname)[...,:3] for fname in filenames]
creates a list of 100 images.
创建一个包含100个图像的列表。
plt.imread(fname)
reads an image and returns a numpy array. You may look at the shape of the array, which should be something like (n,m,3)
or (n,m,4)
, where n
and m
are whole numbers.
plt.imread(fname)读取一个图像并返回一个numpy数组。你可以看一下数组的形状,它应该是(n,m,3)或(n,m,4),其中n和m是整数。
The last axis of dimension 3 or 4 denotes the 3 colorchannels plus possibly a fouth channel which is the alpha (transparency).
尺寸3或4的最后一个轴表示3个颜色通道加上可能是α通道(透明度)的第四个通道。
The slicing [...,:3]
is then equivalent to [:,:,:3]
, which translates in words into "Take all values of the first two dimensions and take the first three values of the third dimension". I.e. you neglect the alpha channel, if present. The reason for doing this is most probably that you want to combine different images here and not all may have an alpha channel. So by neglecting it, you make sure not to run into problems later on.
切片[...,:3]等同于[:,:,:3],它将单词翻译成“获取前两个维度的所有值并获取第三个维度的前三个值”。即你忽略了alpha通道,如果存在的话。这样做的原因很可能是你想在这里组合不同的图像而不是所有图像都有alpha通道。因此,忽略它,你确保以后不会遇到问题。
In this example, you only take jpeg images. Jpeg images do not have an alpha channel and thus you may actually use
在此示例中,您只拍摄jpeg图像。 Jpeg图像没有alpha通道,因此您可能实际使用
imgs = [plt.imread(fname) for fname in filenames]
which should give you the same result.
哪个应该给你相同的结果。