Python 魔杖对象颜色创建非常缓慢

Python 魔杖对象颜色创建非常缓慢,python,imagemagick,wand,Python,Imagemagick,Wand,我有一个numpy像素阵列,我想将其转换为TIFF图像,但对象颜色创建非常缓慢;对于1280x2440的阵列/图像大小,创建具有像素分配的魔杖图像大约需要4分钟。示例代码: # print a.pixel_array.shape # (1280, 2440) with Image( width = a.pixel_array.shape[ 1 ], height = a.pixel_array.shape[ 0 ] ) as i1: i1.type = 'grayscale'

我有一个numpy像素阵列,我想将其转换为TIFF图像,但对象颜色创建非常缓慢;对于1280x2440的阵列/图像大小,创建具有像素分配的魔杖图像大约需要4分钟。示例代码:

# print a.pixel_array.shape
#  (1280, 2440)

with Image( width = a.pixel_array.shape[ 1 ], height = a.pixel_array.shape[ 0 ] ) as i1:
    i1.type = 'grayscale'
    i1.format = 'tiff'
    i1.compression = 'undefined'
    i1.resolution = ( 300, 300 )
    i1.alpha_channel = False

    for idxy, y in enumerate( a.pixel_array ):
        for idxx, x in enumerate( y ):
            hex_color = hex( x )[ 2: ].zfill( 4 )
            with Color( '#' + hex_color + hex_color + hex_color ) as color:
                i1[ idxx, idxy ] = color
创建颜色对象的过程本质上是缓慢的还是有更快的实现来执行相同的过程

提前感谢

使用方法一次导入所有像素

将numpy导入为np
从wand.image导入图像
像素数组=np.random.randint(255,大小=(12802440),数据类型='u1')
带图像(宽度=像素_数组.shape[0],
高度=像素_数组。形状[1])为i1:
i1.type='灰度'
i1.format='tiff'
i1.compression='未定义'
i1.分辨率=(300300)
i1.alpha_通道=假
i1.导入像素(宽度=像素数组.shape[0],
高度=像素_数组。形状[1],
通道_map='I',
存储='char',
data=pixel_array.flatte().tolist())

请注意,数组数据类型是一个无符号字符(颜色值介于0到255之间)。

Wow,现在图像转换只需不到1秒!