Python PIL retains last drawn image to new image and how to stop this

Viewed 61

I am generating images with PIL. I am needing to generate 100 image but the second image has the content of the first image and so on. By image 100 all of the previous data is drawn on the old image. Right now its only generating 5.

My goal is to make a animation out of the random images. That is why the next points are near the old points. I thought it would look cool.

I've added my code and the first and second images to show what is going on

from PIL import Image, ImageDraw
import random


def generate():
imagesizepx = 500
imageColor = (255, 255, 255)
paddingpx = 20

count = 0
points = []
for _ in range(5):

    image = Image.new(
        "RGB",
        size=(imagesizepx, imagesizepx),
        color=imageColor)
    draw = ImageDraw.Draw(image)

    if not points:
        # randomly generate the points that will be connected by the lines
        for _ in range(10):
            random_point = (
                random.randint(paddingpx, imagesizepx - paddingpx),
                random.randint(paddingpx, imagesizepx - paddingpx)
            )
            points.append(random_point)
    else:
        # generate new points near old points
        for i in range(len(points)):
            a, b = points[i]
            random_point = (
                random.randint(a - 10, a + 10),
                random.randint(b - 10, b + 10)
            )
            points.append(random_point)
           

    thickness = 0
    for i, point in enumerate(points):
        p1 = point
        if i == len(points) - 1:
            p2 = points[0]
        else:
            p2 = points[i + 1]

        xy = (p1, p2)
        lineColor = "orange"
        thickness = 2
        draw.line(xy, fill=lineColor, width=thickness)

    # save image
    image.save(str(count) + ".jpeg")
    count += 1

enter image description here

enter image description here

1 Answers

Your problem was that each frame, you appended the new random points to points, which meant that the points from the previous frame where still there. You can solve it by making a temporary list to hold the new random points as you generate them, and then setting the points list to that new list.

Here's the code I got: (I commented out the def generate():, since the code below it was not indented.

from PIL import Image, ImageDraw
import random


#def generate():
imagesizepx = 500
imageColor = (255, 255, 255)
paddingpx = 20

count = 0
points = []
for _ in range(5):

    image = Image.new(
        "RGB",
        size=(imagesizepx, imagesizepx),
        color=imageColor)
    draw = ImageDraw.Draw(image)

    if not points:
        # randomly generate the points that will be connected by the lines
        for _ in range(10):
            random_point = (
                random.randint(paddingpx, imagesizepx - paddingpx),
                random.randint(paddingpx, imagesizepx - paddingpx)
            )
            points.append(random_point)
    else:
        # generate new points near old points
        new_points = [] # Create a new temporary list to hold the new points
        for i in range(len(points)):
            a, b = points[i]
            random_point = (
                random.randint(a - 10, a + 10),
                random.randint(b - 10, b + 10)
            )
            new_points.append(random_point) # Add new points to that list
        points = new_points # Set the original list to the list of new random points

           

    thickness = 0
    for i, point in enumerate(points):
        p1 = point
        if i == len(points) - 1:
            p2 = points[0]
        else:
            p2 = points[i + 1]

        xy = (p1, p2)
        lineColor = "orange"
        thickness = 2
        draw.line(xy, fill=lineColor, width=thickness)

    # save image
    image.save(str(count) + ".jpeg")
    count += 1
Related