魔杖对象颜色创建非常慢

Wand object Color creation very slow

我有一个 numpy 像素数组,我想将其转换为 TIFF 图像,但对象颜色创建速度非常慢;对于尺寸为 1280x2440 的 array/image,创建带有像素分配的魔杖图像大约需要 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

创建 Color 对象的过程本质上是缓慢的还是有一些更快的实现来执行相同的过程?

提前致谢

使用Image.import_pixels方法一次导入所有像素。

import numpy as np
from wand.image import Image

pixel_array = np.random.randint(255, size=(1280, 2440), dtype='u1')

with Image(width=pixel_array.shape[0],
           height=pixel_array.shape[1]) as i1:
    i1.type = 'grayscale'
    i1.format = 'tiff'
    i1.compression = 'undefined'
    i1.resolution = ( 300, 300 )
    i1.alpha_channel = False
    i1.import_pixels(width=pixel_array.shape[0],
                     height=pixel_array.shape[1],
                     channel_map='I',
                     storage='char',
                     data=pixel_array.flatten().tolist())

注意数组数据类型为unsigned char(颜色值在0~255之间)