如何建立与Python中的原始数据串magick棒的图像(How to create an imag

2019-10-30 07:04发布

我有一个原始数据字符串,例如像这样:

raw_data = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3300005CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9500B158DFFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFB200000003FFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFB100000002FFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFF643040B80FFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3300002BFFED843913051B59D1FFFFFFFFFFFF03000184FFFF3300002BAB0F00000000000007BBFFFFFFFFFF03000057FFFF330000080100000000000000001DF8FFFFFFFF03000057FFFF330000000042A9D4D08D0D000000ADFFFFFFFF03000057FFFF330000059DFFFFFFFFFFAA00000070FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFA05000051FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF18000045FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF1B000043FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF1B000043FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF1B000043FFFFFFFF03000057"

我想知道它是如何可能创造一个可视png格式出来。

我尝试使用Image类

with wand.image.Image (???) as img:

但我不知道要放什么东西在括号中。 我试图file=raw_data, width=25,height=25,depth=8

无论我尝试它给了我一个错误。

有任何想法吗?

Answer 1:

您需要将字符串转换为字节,并实现ImageMagick的MagickImportImagePixels C-API方法。

import binascii
import ctypes

from wand.api import library
from wand.image import Image
from wand.color import Color

# Map C-API to Python
# -------------------
# magick-image.h
# WandExport MagickBooleanType MagickImportImagePixels(MagickWand *wand,
#   const ssize_t x,const ssize_t y,const size_t columns,const size_t rows,
#   const char *map,const StorageType storage,const void *pixels)
library.MagickImportImagePixels.argtypes = (ctypes.c_void_p,
                                            ctypes.c_ssize_t,
                                            ctypes.c_ssize_t,
                                            ctypes.c_size_t,
                                            ctypes.c_size_t,
                                            ctypes.c_char_p,
                                            ctypes.c_int,
                                            ctypes.c_void_p)


# Map enum StorageType
StorageType = ('undefined', 'char', 'double', 'float',
               'integer', 'long', 'quantum', 'short')

# Extend wand.image.Image to implement import_pixels method
class MyImage(Image):
    def import_pixels(self, blob, pixel_format='RGBA', pixel_size='char'):
        storage_type = StorageType.index(pixel_size)
        library.MagickImportImagePixels(self.wand,
                                        0,
                                        0,
                                        self.width,
                                        self.height,
                                        pixel_format.encode(),
                                        storage_type,
                                        blob)
# Usage
raw_data_string = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3300005CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9500B158DFFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFB200000003FFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFB100000002FFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFF643040B80FFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF3300002BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3300002BFFED843913051B59D1FFFFFFFFFFFF03000184FFFF3300002BAB0F00000000000007BBFFFFFFFFFF03000057FFFF330000080100000000000000001DF8FFFFFFFF03000057FFFF330000000042A9D4D08D0D000000ADFFFFFFFF03000057FFFF330000059DFFFFFFFFFFAA00000070FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFA05000051FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF18000045FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF1B000043FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF1B000043FFFFFFFF03000057FFFF3300002BFFFFFFFFFFFFFF1B000043FFFFFFFF03000057'
raw_data_bytes = binascii.unhexlify(raw_data_string)

with MyImage(width=25, height=25, background=Color('WHITE')) as img:
    img.import_pixels(raw_data_bytes, pixel_format='R', pixel_size='char')
    img.save(filename='output.png')

正如在评论中指出,你将负责知道/定义通道布局(即RGBABGRARBG &TC),数据类型尺寸(即字符/短/ INT /长,或浮动/双),数据-stream布局(如隔行扫描 ),以及期望图像当然实际宽度/高度。



文章来源: How to create an image with magick wand from raw data string in Python