简体   繁体   中英

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

I made a program that renders the Mandelbrot set to an image. I put the draw.point() method in a function, but it doesn't seem to actually draw on the final image, but if I put im.save() in the function it does work. The full code actually uses multiprocessing and renders the image CineBench style, which is why I can't put the im.save() in the function, or pull the draw.point() out of it. Is there some other way I can solve the problem?

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")
    

This is not the entire program, but hopefully enough to make sense

I'm not sure that your sample code is representative, because this version works fine:

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")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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