gpt4 book ai didi

python - 128x512 数组列表中的图像列表可提高效率

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:59 24 4
gpt4 key购买 nike

我有一个 128x512 数组的列表,如下所示:

        [ 0.,  0.,  0., ...,  0.,  0.,  0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),
array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),
array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,

我正在将此数组列表转换为 RGB 图像列表,到目前为止我的代码是:

#makes an array of all the images 

image_out = [[] for i in range(len(blue_Rad_list))]

for i in range(len(blue_Rad_list)):

startTime = time.time()

image_arr = [np.int_(np.float_(x/np.amax(blue_Rad_list))*256) for x in blue_Rad_list[i]]
image_out[i] = Image.new('RGB', (width, height))
image_out[i].putdata(np.asarray(image_arr).ravel())


del image_arr[:]

stopTime = time.time()

print(stopTime - startTime)

运行我的代码后,我得到如下信息:

 <PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CDCE90>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CDCED0>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CDCF10>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CDCF50>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CDCF90>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CDCFD0>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE9050>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE9090>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE90D0>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE9110>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE9150>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE9190>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE91D0>,
<PIL.Image.Image image mode=RGB size=128x512 at 0x7F47D4CE9210>]

在上面的代码中,blue_Rad_list是128x512数组的列表。这段代码可以工作,但是当有大约 180 个元素时,它需要花费很多时间才能为我提供整个图像列表。有没有更有效的方法可以做到这一点。谢谢你的帮助。

最佳答案

本着一旦进入循环就执行更少工作的想法,特别是那些计算繁重的工作,这里有一种使用多维数组而不是数组列表作为输入的方法。在此过程中,我们将利用 NumPy 支持的向量化运算来覆盖所有元素 -

# Convert to 3D array. If you already have the multi-dim array that was used to
# create the list of arrays. Use that instead of imgs
imgs = np.array(blue_Rad_list)

# Perfomr the image conversion operation formerly done within loop
imgs1 = np.int_(np.float_(imgs/np.amax(imgs))*256).reshape(imgs.shape[0],-1)

# Loop through and create list of PIL images
image_out = [[] for i in range(len(blue_Rad_list))]
for i in range(len(imgs)):
image_out[i] = Image.new('RGB', (width, height))
image_out[i].putdata(imgs1[i])

看来我们可以通过在进入循环之前初始化数据存储来进一步优化一步,就像这样 -

image_out = [Image.new('RGB', (width, height))]*len(imgs)
for i in range(len(imgs)):
image_out[i].putdata(imgs1[i])

关于python - 128x512 数组列表中的图像列表可提高效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44658744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com