I wrote a little code to try to create an image with only one black dot per Y-value, but it creates more then one per Y-value. The image ->
我写了一些代码来尝试创建一个每个Y值只有一个黑点的图像,但每个Y值创建的图像多一个。图像 - >
the image is small, but you can see that there can be more than one black dot per Y-value. The actual code:
图像很小,但您可以看到每个Y值可以有多个黑点。实际代码:
from PIL import Image
from random import *
from math import *
white = (255,255,255)
black = (0,0,0)
width = 50
height = 10
num = 0
def line(num):
mylist = []
a = Image.new("RGB", (width, height))
for row in xrange(width):
dot = False
for col in xrange(height):
rand = random()
b = float(col)/(width-1)
if b > rand and not dot:
mylist.append(black)
dot = True
else:
mylist.append(white)
a.putdata(mylist)
a.save("boop"+str(num)+".png")
line(num)
Normally when it appends a black dot, dot
becomes true, so there can't be another black dot before the next row of pixels. Why isn't this working?
通常当它附加黑点时,点变为真,因此在下一行像素之前不能有另一个黑点。为什么这不起作用?
EDIT: exemple of desired output ->
编辑:所需输出的例子 - >
2 个解决方案
#1
2
Your row is set for range width and your column is height, change the xrange(width)
with xrange(height)
and vice versa then the image should be output as desired.
您的行设置为范围宽度,列是高度,使用xrange(高度)更改xrange(宽度),反之亦然,然后应根据需要输出图像。
As it stands, it is checking for 10 columns for a dot as opposed to the 50 it should be looking for.
就目前而言,它正在检查一个点的10列而不是它应该寻找的50列。
EDIT 3
import numpy as np
#other declarations as above#
def line(num):
mylist = np.zeros((width, height))
for row in mylist:
row[randint(0,9)] = 1
npArr = mylist.T
a = Image.new("RGB", (width, height))
mylist = []
for idx, val in np.ndenumerate(npArr):
if val == 1:
mylist.append(black)
else:
mylist.append(white)
a.putdata(mylist)
a.save("boop"+str(num)+".png")
If you can install numpy, you can create an array, fill it row-wise since you can append a column with a random 1 (dot) in it and then rotate the array to the landscape orientation.
如果你可以安装numpy,你可以创建一个数组,按行填充它,因为你可以在其中追加一个随机1(点)的列,然后将数组旋转到横向。
#2
0
so I found what the problem was, the putdata(list) function write from left to right then from top to bottom on the image but my function was trying to append from top to bottom then left to right so it wasnt creating the right output. to fix that, I fill the list with white color (width*height times) then I change the change the color in the list with [col * width + row] formula. the code:
所以我发现问题是什么,putdata(list)函数从左到右然后从上到下写在图像上,但是我的函数试图从上到下然后从左到右追加,所以它没有创建正确的输出。为了解决这个问题,我用白色填充列表(宽度*高度倍数),然后用[col * width + row]公式更改列表中颜色的更改。代码:
width = 50
height = 10
num = 0
mylist = []
def line(num):
a = Image.new("RGB", (width, height))
for pixel in xrange(width*height):
mylist.append(white)
for row in xrange(width):
dot = False
for col in xrange(height):
rand1 = random()
rand2 = random()
b = float(col)/(height-1)
if rand1 < b < rand2 and not dot:
mylist[col*width+row] = black
dot = True
if col == height-1 and not dot:
rand3 = randint(0, height-1)
mylist[rand3*width+row] = black
a.putdata(mylist)
a.save("boop"+str(num)+".png")
line(num)
输出 - >
#1
2
Your row is set for range width and your column is height, change the xrange(width)
with xrange(height)
and vice versa then the image should be output as desired.
您的行设置为范围宽度,列是高度,使用xrange(高度)更改xrange(宽度),反之亦然,然后应根据需要输出图像。
As it stands, it is checking for 10 columns for a dot as opposed to the 50 it should be looking for.
就目前而言,它正在检查一个点的10列而不是它应该寻找的50列。
EDIT 3
import numpy as np
#other declarations as above#
def line(num):
mylist = np.zeros((width, height))
for row in mylist:
row[randint(0,9)] = 1
npArr = mylist.T
a = Image.new("RGB", (width, height))
mylist = []
for idx, val in np.ndenumerate(npArr):
if val == 1:
mylist.append(black)
else:
mylist.append(white)
a.putdata(mylist)
a.save("boop"+str(num)+".png")
If you can install numpy, you can create an array, fill it row-wise since you can append a column with a random 1 (dot) in it and then rotate the array to the landscape orientation.
如果你可以安装numpy,你可以创建一个数组,按行填充它,因为你可以在其中追加一个随机1(点)的列,然后将数组旋转到横向。
#2
0
so I found what the problem was, the putdata(list) function write from left to right then from top to bottom on the image but my function was trying to append from top to bottom then left to right so it wasnt creating the right output. to fix that, I fill the list with white color (width*height times) then I change the change the color in the list with [col * width + row] formula. the code:
所以我发现问题是什么,putdata(list)函数从左到右然后从上到下写在图像上,但是我的函数试图从上到下然后从左到右追加,所以它没有创建正确的输出。为了解决这个问题,我用白色填充列表(宽度*高度倍数),然后用[col * width + row]公式更改列表中颜色的更改。代码:
width = 50
height = 10
num = 0
mylist = []
def line(num):
a = Image.new("RGB", (width, height))
for pixel in xrange(width*height):
mylist.append(white)
for row in xrange(width):
dot = False
for col in xrange(height):
rand1 = random()
rand2 = random()
b = float(col)/(height-1)
if rand1 < b < rand2 and not dot:
mylist[col*width+row] = black
dot = True
if col == height-1 and not dot:
rand3 = randint(0, height-1)
mylist[rand3*width+row] = black
a.putdata(mylist)
a.save("boop"+str(num)+".png")
line(num)
输出 - >