Python PIL 将最后绘制的图像保留为新图像以及如何停止此操作
Python PIL retains last drawn image to new image and how to stop this
我正在使用 PIL 生成图像。我需要生成 100 张图像,但第二张图像具有第一张图像的内容,依此类推。通过图像 100,所有以前的数据都绘制在旧图像上。现在它只产生 5.
我的目标是用随机图像制作动画。这就是为什么下一个点靠近旧点的原因。我觉得它看起来很酷。
我已经添加了我的代码以及第一张和第二张图片以显示正在发生的事情
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
你的问题是每一帧,你都将新的随机点附加到 points
,这意味着前一帧的点仍然存在。您可以通过创建一个临时列表来保存生成的新随机点,然后将 points
列表设置为该新列表来解决它。
这是我得到的代码:(我注释掉了 def generate():
,因为下面的代码没有缩进。
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
我正在使用 PIL 生成图像。我需要生成 100 张图像,但第二张图像具有第一张图像的内容,依此类推。通过图像 100,所有以前的数据都绘制在旧图像上。现在它只产生 5.
我的目标是用随机图像制作动画。这就是为什么下一个点靠近旧点的原因。我觉得它看起来很酷。
我已经添加了我的代码以及第一张和第二张图片以显示正在发生的事情
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
你的问题是每一帧,你都将新的随机点附加到 points
,这意味着前一帧的点仍然存在。您可以通过创建一个临时列表来保存生成的新随机点,然后将 points
列表设置为该新列表来解决它。
这是我得到的代码:(我注释掉了 def generate():
,因为下面的代码没有缩进。
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