【问题标题】:Python keep generating same numberPython不断生成相同的数字
【发布时间】:2021-12-17 12:11:18
【问题描述】:

我想随机生成 100 张 100x100 像素的图像。

但是当我制作这张图片时,我想改变像素的颜色。

不知何故,图像总是相同的,因为它生成相同的数字。

我应该改变什么?

from PIL import Image, ImageDraw
import random

source_img = Image.open("base.png").convert("RGBA")

draw = ImageDraw.Draw(source_img)

Height = 100
Width = 100

R = 0
G = 0
B = 0

piece = 0
while (piece < 100):
    while (width > 0):
        while (height > -1):
            color = random.randint(1,3)
            if (color == 1):
                R = 255
                G = 0
                B = 0
            elif (color == 2):
                R = 0
                G = 255
                B = 0
            elif (color == 3):
                R = 0
                G = 0
                B = 255
            
            draw.rectangle(((heigth, 0), (heigth, width)), fill=(R, G, B))
            heigth -=1
        width -= 1
        heigth = 100
    piece +=1
    source_img.save("images/" + str(piece) + ".png", "PNG")
    
print("Images is done!")

【问题讨论】:

  • 您可以使用fill = random.choice((255, 0, 0), (0, 255, 0), (0, 0, 255)) 简化您的逻辑。甚至将它们定义为常量:RED = (255, 0, 0) 等,然后是 fill = random.choice((RED, GREEN, BLUE))
  • 仍然创建相同的图像:(
  • 不清楚您的代码应该做什么,以及它与您想要的有何不同。您能否提供示例输入图像、您的预期输出和当前输出?
  • 所以,我想要 100 张完全不同的图像。图像之间的区别在于像素的颜色。但它会不断生成相同顺序的像素并制作 100 张相同的图像。
  • @ddejohn 你能在你的电脑上测试吗?也许更容易看到它在做什么。

标签: python image random python-imaging-library


【解决方案1】:

您的代码中有一些小错误。使用 IDE 和 linter,您可以发现其中的许多问题,这可以解决您的部分问题。错误:

  • height-Height 和 width-Width 大小写不匹配
  • 拼写错误:height-heigth
  • 图像创建后高度和宽度不会重置

我试过这段代码,现在看起来不错:

from PIL import Image, ImageDraw
import random

piece = 0
while (piece < 100):
    with Image.open("base.png").convert("RGBA") as source_img:
        height = 100  # you could read this from the input image base.png
        width = 100  # you could read this too from the input image base.png
        draw = ImageDraw.Draw(source_img)
        while (width > 0):
            while (height > -1):
                color = random.randint(1, 3)
                if (color == 1):
                    R = 255
                    G = 0
                    B = 0
                elif (color == 2):
                    R = 0
                    G = 255
                    B = 0
                elif (color == 3):
                    R = 0
                    G = 0
                    B = 255

                draw.rectangle(((height, 0), (height, width)), fill=(R, G, B))
                height -= 1
            width -= 1
            height = 100
        piece += 1  # this could be in a loop too, e.g. for p in range(100):
        source_img .save("images/" + str(piece) + ".png", "PNG")

print("Images is done!")

在python中,不是定义你迭代的变量,而是最常见的方式

piece = 100
for p in range(piece):
    ...  # deduce or define width here or earlier
    for w in range(width):
        ...  # deduce or define height here or earlier
        for h in range(height):
            ... # do something with p, w and h

这个 PIL 包很不错。稍后会检查。

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2021-12-31
    • 2012-07-02
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多