【发布时间】:2019-09-19 21:18:25
【问题描述】:
我正在使用 Python 生成图像,并使用虚线进行点画。划线的周期是恒定的,改变的是划线/空间比。这会产生这样的结果:
但是,在该图像中,虚线具有统一的原点,这会产生难看的垂直排水沟。所以我试图随机化原点以去除排水沟。这种工作,但有一个明显的模式:
想知道这是从哪里来的,我做了一个非常简单的测试用例,上面有堆叠的虚直线:
- 冲刺比:50%
- 短划线周期 20px
- 使用
random.uniform(-10.,+10.)(*) 将原点从-10px 转移到+10px(在初始random.seed()之后
并增加随机性:
所以还是有规律的。我不明白的是,要获得可见的排水沟,您需要有 6 或 7 个连续值落在同一范围内(例如,总范围的一半),这应该是 1/64 的概率,但似乎发生了很多在生成的 200 行中更常见。
我是不是误会了什么?只是我们人类的大脑在看到没有的模式吗?有没有更好的方法来生成更“视觉上随机”的东西(python 2.7,最好不安装任何东西)?
(*) 部分像素在该上下文中有效
附件:我使用的代码(这是一个 Gimp 脚本):
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# Python script for Gimp (requires Gimp 2.10)
# Run on a 400x400 image to see something without having to wait too much
# Menu entry is in "Test" submenu of image menubar
import random,traceback
from gimpfu import *
def constant(minShift,maxShift):
return 0
def triangle(minShift,maxShift):
return random.triangular(minShift,maxShift)
def uniform(minShift,maxShift):
return random.uniform(minShift,maxShift)
def gauss(minShift,maxShift):
return random.gauss((minShift+maxShift)/2,(maxShift-minShift)/2)
variants=[('Constant',constant),('Triangle',triangle),('Uniform',uniform),('Gauss',gauss)]
def generate(image,name,generator):
random.seed()
layer=gimp.Layer(image, name, image.width, image.height, RGB_IMAGE,100, LAYER_MODE_NORMAL)
image.add_layer(layer,0)
layer.fill(FILL_WHITE)
path=pdb.gimp_vectors_new(image,name)
# Generate path, horizontal lines are 2px apart,
# Start on left has a random offset, end is on the right edge right edge
for i in range(1,image.height, 2):
shift=generator(-10.,10.)
points=[shift,i]*3+[image.width,i]*3
pdb.gimp_vectors_stroke_new_from_points(path,0, len(points),points,False)
pdb.gimp_image_add_vectors(image, path, 0)
# Stroke the path
pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 0, 0, 255))
pdb.gimp_context_set_stroke_method(STROKE_LINE)
pdb.gimp_context_set_line_cap_style(0)
pdb.gimp_context_set_line_join_style(0)
pdb.gimp_context_set_line_miter_limit(0.)
pdb.gimp_context_set_line_width(2)
pdb.gimp_context_set_line_dash_pattern(2,[5,5])
pdb.gimp_drawable_edit_stroke_item(layer,path)
def randomTest(image):
image.undo_group_start()
gimp.context_push()
try:
for name,generator in variants:
generate(image,name,generator)
except Exception as e:
print e.args[0]
pdb.gimp_message(e.args[0])
traceback.print_exc()
gimp.context_pop()
image.undo_group_end()
return;
### Registration
desc="Python random test"
register(
"randomize-test",desc,'','','','',desc,"*",
[(PF_IMAGE, "image", "Input image", None),],[],
randomTest,menu="<Image>/Test",
)
main()
【问题讨论】:
-
这并不能完全回答您的问题,但很有趣且相关:stackoverflow.com/questions/7029993/…
-
也许您可以创建一个minimal reproducible example,它允许其他人重现您的简单测试用例。仅根据您的描述,我不确定图像是如何生成的。
-
@JohnColeman 添加了代码。一条线是在 orign 0 处的线的随机移动。我不会将线从一条移动到下一条(但可能我应该这样做)
-
@JohnColeman 破折号从每行的起点开始。上一行没有结转(使用不分割图像宽度的破折号很容易检查)
-
这个话题在这个 Youtube 视频“Randomness is Random - Numberphile”中被谈论。基本上,您希望 0 和 1 的随机序列为 010101 ...,即 (01)+,但事实并非如此。