I'm using Python to generate images using dashed lines for stippling. The period of the dashing is constant, what changes is dash/space ratio. This produces something like this:
However in that image the dashing has a uniform origin and this creates unsightly vertical gutters. So I tried to randomize the origin to remove the gutters. This sort of works but there is an obvious pattern:
Wondering where this comes from I made a very simple test case with stacked dashed straight lines:
dash ratio: 50%
dash period 20px
origin shift from -10px to +10px using random.uniform(-10.,+10.)(*) (after an initial random.seed()
And with added randomness:
So there is still pattern. What I don't understand is that to get a visible gutter you need to have 6 or 7 consecutive values falling in the same range (says, half the total range), which should be a 1/64 probability but seems to happen a lot more often in the 200 lines generated.
Am I misunderstanding something? Is it just our human brain which is seeing patterns where there is none? Could there be a better way to generate something more "visually random" (python 2.7, and preferably without installing anything)?
(*) partial pixels are valid in that context
Annex: the code I use (this is a Gimp script):
#!/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()
Think of it like this: a gutter is perceptible until it is obstructed (or almost so). This only happens when two successive lines are almost completely out of phase (with the black segments in the first line lying nearly above the white segments in the next). Such extreme situations only happens about one out of every 10 rows, hence the visible gutters which seem to extend around 10 rows before being obstructed.
Looked at another way -- if you print out the image, there really are longish white channels through which you can easily draw a line with a pen. Why should your mind not perceive them?
To get better visual randomness, find a way to make successive lines dependent rather than independent in such a way that the almost-out-of-phase behavior appears more often.
There's at least one obvious reason why we see a pattern in the "random" picture : the 400x400 pixels are just the same 20x400 pixels repeated 20 times.
So every apparent movement is repeated 20 times in parallel, which really helps the brain analyzing the picture.
Actually, the same 10px wide pattern is repeated 40 times, alternating between black and white:
You could randomize the dash period separately for each line (e.g. between 12 and 28):
Here's the corresponding code :
import numpy as np
import random
from matplotlib import pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = [13, 13]
N = 400
def random_pixels(width, height):
return np.random.rand(height, width) < 0.5
def display(table):
plt.imshow(table, cmap='Greys', interpolation='none')
plt.show()
display(random_pixels(N, N))
def stripes(width, height, stripe_width):
table = np.zeros((height, width))
cycles = width // (stripe_width * 2) + 1
pattern = np.concatenate([np.zeros(stripe_width), np.ones(stripe_width)])
for i in range(height):
table[i] = np.tile(pattern, cycles)[:width]
return table
display(stripes(N, N, 10))
def shifted_stripes(width, height, stripe_width):
table = np.zeros((height, width))
period = stripe_width * 2
cycles = width // period + 1
pattern = np.concatenate([np.zeros(stripe_width), np.ones(stripe_width)])
for i in range(height):
table[i] = np.roll(np.tile(pattern, cycles), random.randrange(0, period))[:width]
return table
display(shifted_stripes(N, N, 10))
def flexible_stripes(width, height, average_width, delta):
table = np.zeros((height, width))
for i in range(height):
stripe_width = random.randint(average_width - delta, average_width + delta)
period = stripe_width * 2
cycles = width // period + 1
pattern = np.concatenate([np.zeros(stripe_width), np.ones(stripe_width)])
table[i] = np.roll(np.tile(pattern, cycles), random.randrange(0, period))[:width]
return table
display(flexible_stripes(N, N, 10, 4))
Posting my final solution as an answer, but please upvote others.
John Coleman has a point when he says:
To get better visual randomness, find a way to make successive lines dependent rather than independent in such a way that the almost-out-of-phase behavior appears more often.
So, finally, the best way to avoid gutters is to forego randomness and have a very fixed scheme of shifts, and one that works well is a 4-phase 0,25%,75%,50% cycle:
OK, there is still slight diamond pattern, but it is much less visible than the patterns introduced by the random schemes I tried.
This is slightly counter-intuitive, but as you add random elements together the randomness gets less. If I follow correctly the range of each element is 10px - 30px. So the total size of 10 elements is 100px to 300px, but the distribution is not even across that range. The extremes are very unlikely and on average it will be pretty close to 200px, so that fundamental 20px pattern will emerge. Your random distribution needs to avoid this.
EDIT: I see I slightly misunderstood, and all dashes are are 20px with a random offset. So, I think looking at any 1 vertical set of dashes would appear random, but that same random set is repeated across the page, giving the pattern.
I'm developing a metacognition experiment in PsychoPy (v. 1.90.1) and I need a visual analogue scale to measure confidence. However, I can't find a way to remove the numeric values (0 and 1) from the extremities of the Psychopy VAS.
Is there any way to hide them?
I need the word labels ("Not at all confident", "Extremely confident") but I would also like to have the answers recorded on a 0-100 scale (or an equivalent 0-1) as the analogue scale does (so switching to categorical wouldn't do).
Any suggestion?
Thanks in advance.
Sonia
You may also be interested in the new Slider, which is included in the current PsychoPy beta versions and will be part of the next release. Here is a Python 3 code example how to use it:
from psychopy.visual.window import Window
from psychopy.visual.slider import Slider
win = Window()
vas = Slider(win,
ticks=(1, 100),
labels=('Not at all confident', 'Extremely confident'),
granularity=1,
color='white')
while not vas.rating:
vas.draw()
win.flip()
print(f'Rating: {vas.rating}, RT: {vas.rt}')
Before re-use, you will have to call vas.reset().
Take a look at the documentation, particularly labels and scale. This is one solution:
# Set up window and scale
from psychopy import visual
win = visual.Window()
scale = visual.RatingScale(win,
labels=['Not at all confident', 'Extremely confident'], # End points
scale=None, # Suppress default
low=1, high=100, tickHeight=0)
# Show scale
while scale.noResponse:
scale.draw()
win.flip()
# Show response
print scale.getRating(), scale.getRT()
Might I extend #hoechenberger's answer to add support for
the randomised start on each trial (set markerPos to a random val in units of the tick marks)
keypress support (just use keys to adjust markerPos and when you're done assign that to rating)
custom step sizes (when you've understood (2) then this is probably obvious)
Python2.7 (no need to force Py3.6 here :wink: )
Here's the code:
from psychopy import visual
from psychopy import event
from numpy.random import random
stepSize = 2
win = visual.Window()
vas = visual.Slider(win,
ticks=(0, 1),
labels=('Not at all confident', 'Extremely confident'),
granularity=1,
color='white')
for thisTrialN in range(5):
vas.reset()
vas.markerPos = random() # randomise start
while not vas.rating:
# check keys
keys = event.getKeys()
if 'right' in keys:
vas.markerPos += stepSize
if 'left' in keys:
vas.markerPos -= stepSize
if 'return' in keys:
# confirm as a rating
vas.rating = vas.markerPos
# update the scale on screen
vas.draw()
win.flip()
print('Rating: {}, RT: {}'.format(vas.rating, vas.rt))
Hopefully I'll be able to explain this well. I'm currently using helper functions to draw a six-pointed star in the turtle graphics window of python. First, we had to create a function to draw a triangle. Here is my code:
import turtle
wn = turtle.Screen()
tess = turtle.Turtle()
tess.speed(30)
def triangle(sz):
for i in range(3):
tess.fd(sz)
tess.lt(120)
Then, we had to use the triangle function to draw a six-pointed star. Here is my code:
def sixPtdStar(sz):
triangle(sz)
tess.lt(90)
tess.pu()
tess.fd(80)
tess.rt(90)
tess.fd(120)
tess.pd()
tess.rt(180)
triangle(sz)
Now, for me, this all runs smoothly. But the parameters for our test run of those two functions was that sz = 120 (so in the shell we'd type sixPtdStar(120) and it would run. But then we had to draw a row of stars with a new function, and then a BOX outline by those rows of stars, in another function. Here is my code:
def rowOfStars(numInRow,sz):
for i in range(numInRow):
sixPtdStar(sz)
tess.pu()
tess.lt(90)
tess.fd(80)
tess.lt(90)
def sqrOfRows(numInRow, sz):
for i in range(4):
rowOfStars(numInRow, sz)
tess.rt(90)
While this accomplishes the task, it only does so if the sz = 120. And for our test run on the rowOfStars function, the parameters are supposed to be (6, 72) and for the test run on the sqrOfRows function, our parameters are supposed to be (6, 36).
So my issue is this. How can I make this work no matter what sz equals? When I run it as is (with (6, 72) for rowOfStars or (6, 36) for sqrOfRows), the pen moves too far because the triangles aren't as big anymore.
Please let me know if more info is needed! Thanks! (I'm using Python 3.5.2)
Anywhere you use a unit that has a dimension:
tess.fd(80)
tess.fd(120) # probably should be tess.fd(sz)
tess.fd(80)
you need to scale it by what ever logic you used to get from 120 (sz) to 80. However, as #wptreanor mentioned, that logic is slightly flawed as the points on your star are uneven:
Also, your rowOfStars() routine doesn't really draw a row of stars (math is off and the pen is in the wrong state at times.) Simply fixing the scaling won't fix this. Finally, your sqrOfRows() routine won't work until rowOfStars() is fixed, and to make it useful, you need to adjust the starting position on the screen to make room for the drawing.
Below is my rework of your code to address some of these issues. It uses a slightly different calculation of how to position from finishing the lower to starting the upper triangle so the numbers are slightly different:
from turtle import Turtle, Screen
WIDTH_RATIO = 2 * 3**0.5 / 3 # ratio of widest point in star to edge of triangle
def triangle(size):
for i in range(3):
tess.fd(size)
tess.lt(120)
def sixPtdStar(size):
triangle(size)
tess.lt(30)
tess.pu()
tess.fd(size * WIDTH_RATIO)
tess.lt(150)
tess.pd()
triangle(size)
def rowOfStars(numInRow, size):
for i in range(numInRow):
sixPtdStar(size)
tess.pu()
tess.lt(90)
tess.fd(size * WIDTH_RATIO / 2)
tess.lt(90)
tess.pd()
def sqrOfRows(numInRow, size):
tess.pu()
halfSize = numInRow * size / 2
tess.goto(-halfSize, halfSize) # center on screen
tess.pd()
for i in range(4):
rowOfStars(numInRow, size)
tess.rt(90)
screen = Screen()
tess = Turtle()
tess.speed("fastest") # numbers > 10 are all equivalent, safer to use symbols
sqrOfRows(6, 36)
screen.exitonclick()
The problem is in your sixPtdStar() function.
def sixPtdStar(sz):
triangle(sz)
tess.lt(90)
tess.pu()
tess.fd(80) # here
tess.rt(90)
tess.fd(120) # and here
tess.pd()
tess.rt(180)
triangle(sz)
If your function takes a size as a parameter, all functions involving movement (such as forward() or goto()) need to be scaled by the size as well. The following code should work:
def sixPtdStar(sz):
triangle(sz)
tess.lt(90)
tess.pu()
tess.fd((2.0/3.0)*sz) #formerly 80
tess.rt(90)
tess.fd(sz) #formerly 120
tess.pd()
tess.rt(180)
triangle(sz)
This will ensure that all forward movements are proportional to the size of the object you create. You will need to make similar tweaks to your rowOfStars() function. I've also noticed that your six pointed star isn't fully symmetrical. You could resolve that by replacing tess.fd((2.0/3.0)*sz) with tess.fd((7.0/12.0)*sz).
I have an object that changes its display based on which way it's facing. The object takes a 4x4 grid of frames, and uses each row of 4 frames as an animation for each state.
Currently, I'm loading these into separate sprites using:
def create_animation(image_grid, start_idx, end_idx):
frames = []
for frame in image_grid[start_idx:end_idx]:
frames.append(pyglet.image.AnimationFrame(frame, 0.1))
return pyglet.sprite.Sprite(pyglet.image.Animation(frames))
and then adding the sprite that should be displayed to a batch to be drawn, and removing it when it shouldn't be drawn.
However, reading the documentation, I saw this:
Sprite.batch
The sprite can be migrated from one batch to another, or removed from its batch (for individual drawing). Note that this can be an expensive operation.
Is there a better way to achieve what I'm trying to do without the performance hit of switching the individual sprites in and out of batches?
You can load the image as a TextureGrid:
img = pyglet.resource.image("obj_grid.png")
img_grid = pyglet.image.ImageGrid(
img,
4, # rows, direction
4 # cols, frames of the animation
)
texture_grid = pyglet.image.TextureGrid(img_grid) # this is the one you actually use
Create your (single) sprite:
batch = pyglet.graphics.Batch() # unless you already have a batch
my_object = pyglet.sprite.Sprite(
img=texture_grid[0, 0], # [row, col] for starting grid /frame
batch=batch
)
Determine/change direction ("row", I'm guessing based on input?).
Loop over 0-3 ("col"/frame of the animation ):
pyglet.clock.schedule_interval(change_frame, 0.1, my_object)
def change_frame(dt, my_object): # pyglet always passes 'dt' as argument on scheduled calls
my_object.col += 1
my_object.col = my_object.col & 3 # or my_object.col % 3 if its not a power of 2
And set the frame manually:
current_frame = self.texture_grid[self.row, self.col].get_texture()
my_object._set_texture(current_frame)
No additional calls to draw(), no messing with the batch(). Everything is drawn as usual, but you change the texture it draws as you wish :)
I have a rather simple goal of drawing a few spheres in a 3D space and adjusting their location according to some function. I want to use python with kivy to accomplish this because it make touch screen interfacing super simple, and I found a repository which takes care of most of the heavy lifting with respects to programming.
From this code in the main.py function, I want to draw n spheres, and then update their locations later on (this is done under the draw_elements(self) function, and LOP[] is a list of the class 'points')
def drawPoints():
print self.scene.objects
for i in range(len(self.LOP)):
PushMatrix()
point = self.LOP[i]
point.shape = self.scene.objects['Sphere']
point.color = _set_color(i/10., (i+1)/10., 0., id_color=(int(255/(1+i)), int(255/(1+i)), 255))
point.shape.scale = Scale((i+1)/10.0,(i+1)/10.0,(i+1)/10.0)
self.LOP[i] = point
point.shape.scale.origin = (point.loc[0],point.loc[1],point.loc[2])
_draw_element(point.shape)
PopMatrix()
drawPoints()
When the points are drawn, they are at their stated origin.
Later on the program calls the update_scene function thanks the the clock scheduler.
def update_scene(self, *largs):
def randLoc(point):
newLoc = (0.1*random.random(),0.1*random.random(),0.1*random.random())
oldLoc = point.shape.scale.origin
newLoc = ( newLoc[0]-0.05+oldLoc[0], newLoc[1]-0.05+oldLoc[1], newLoc[2]-0.05+oldLoc[2] )
return newLoc
def updateLocs(self):
for i in range(len(self.LOP)):
point = self.LOP[i]
point.shape.scale.origin = randLoc(point)
if not self.pause:
updateLocs(self)
pass
When this update function is run, only the sphere that was drawn last moves, though it does move correctly.
How can I move the other spheres I drew earlier?
(my source code can be found here though it's really just build off of the first repository)