简体   繁体   English

如何在python中用魔杖创建图像

[英]How to create an image with wand in python

I am trying to create an image with 4 pixels:我正在尝试创建一个 4 像素的图像:

1 pixel in red color, 1 pixel in blue color, 1 pixel in green color, 1 pixel in white color红色 1 个像素,蓝色 1 个像素,绿色 1 个像素,白色 1 个像素

Code:代码:

import wand.image

red = wand.image.Color('rgb(255,0,0)')
green = wand.image.Color('rgb(0,255,0)')
blue = wand.image.Color('rgb(0,0,255)')
white = wand.image.Color('rgb(255,255,255)')

myImage = wand.image.Image(width=2,height=2)

with wand.image.Image (myImage) as img:
    img[0][0] = red
    img[0][1] = blue
    img[1][0] = green
    img[1][1] = white
    img.save(filename='out.png')

But it only creates a transparent png .但它只创建一个透明的png What am I doing wrong?我究竟做错了什么?

Wand's pixel iterators lack the ability to "sync" the color data back into ImageMagick's "authentic" pixels data-steam. Wand 的像素迭代器缺乏将颜色数据“同步”回 ImageMagick 的“真实”像素数据流的能力。

You can implement an import-pixel-data stream, like this question (similar questions get asked a lot).你可以实现一个导入像素数据流, 就像这个问题(类似的问题经常被问到)。

Or use wand.drawing.Drawing API.或者使用wand.drawing.Drawing API。

from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color


with Drawing() as ctx:
    colors = ["RED", "GREEN", "BLUE", "WHITE"]
    for index, color_name in enumerate(colors):
        ctx.push()                         # Grow context stack
        ctx.fill_color = Color(color_name) # Allocated color
        ctx.point(index % 2, index / 2)    # Draw pixel
        ctx.pop()                          # Reduce context stack
    with Image(width=2, height=2, background=Color("NONE")) as img:
        ctx.draw(img)
        img.sample(100,100)
        img.save(filename="output.png")

输出.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM