繁体   English   中英

PIL ImageDraw.Draw() 在 function 中使用时不起作用

[英]PIL ImageDraw.Draw() doesn't work when used in a function

我制作了一个将 Mandelbrot 集渲染为图像的程序。 我将 draw.point() 方法放在 function 中,但它似乎并没有实际绘制最终图像,但如果我将 im.save() 放在 function 中,它确实有效。 完整代码实际上使用多处理并渲染图像 CineBench 风格,这就是为什么我不能将 im.save() 放在 function 中,或从中拉出 draw.point()。 还有其他方法可以解决问题吗?

from PIL import Image, ImageDraw


im = Image.new("RGB", (hor_res, vert_res), (0, 0, 0))
draw = ImageDraw.Draw(im)


def mandelbrot():
    # mandelbrot code


def box_renderer(x_start: int, x_end: int, y_start: int, y_end: int):
    for y in range(y_start, y_end):
        for x in range(x_start, x_end):
             colour = 255 - int(255*mandelbrot(x, y)/iterations)
             draw.point([x, y], (0, 0, colour))


if __name__ == "__main__":
    box_renderer(args)
    im.save("mandelbrot.png", "PNG")
    

这不是整个程序,但希望足以理解

我不确定您的示例代码是否具有代表性,因为这个版本工作正常:

from PIL import Image, ImageDraw

hor_res, vert_res = 200, 200
im = Image.new("RGB", (hor_res, vert_res), (255, 0, 0))
draw = ImageDraw.Draw(im)

def box_renderer(x_start: int, x_end: int, y_start: int, y_end: int):
    for y in range(y_start, y_end):
        for x in range(x_start, x_end):
             colour = 128
             draw.point([x, y], (0, 0, colour))


if __name__ == "__main__":
    box_renderer(x_start=50,x_end=100,y_start=20,y_end=180)
    im.save("mandelbrot.png", "PNG")

在此处输入图像描述

暂无
暂无

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

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