如何使用magick wand从Python中的原始数据字符串创建图像

时间:2017-10-10 00:39:31

标签: python wand raw-data magickwand

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

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

我想知道怎样才能创建一个可视的.png。

我尝试使用Image class

file=raw_data, width=25,height=25,depth=8

但我不确定括号内放什么。我试过了$(document).scroll(function () { scroll_pos = $(this).scrollTop(); var h = $("#secOne").height() - ($("#secOne").height() / 10); if (scroll_pos > h) { $("#topBar").css({ 'padding': '2.5vh 7.5vw 1vh 7.5vw', }); $("#menuIcon").css({ 'background': 'url("assets/menuBlue.svg") no-repeat', }); $("#menuIcon").hover(function () { $(this).css('background', 'url("assets/menuBlueHover.svg") no-repeat') }); } else { $("#topBar").css({ 'padding': '5vh 7.5vw 2vh 7.5vw', }); $("#menuIcon").css({ 'background': 'url("assets/menu.svg") no-repeat', }); $("#menuIcon").hover(function () { $(this).css('background', 'url("assets/menuHover.svg") no-repeat') }); } });

无论我尝试什么,都会给我一个错误。

有什么想法吗?

1 个答案:

答案 0 :(得分: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')

output.png

正如评论中所指出的,您将负责了解/定义频道布局(即RGBABGRARBG& tc),数据类型大小(即char / short / int / long,或float / double),数据流布局(如interlacing),当然还有预期图像的实际宽度/高度。