NumPy

Python Challenge Level 14: Italy – Code

Used a NumPy array. Doesn’t work if the diagonal+1s are white of course.

import Image
from numpy import array, zeros, uint8
 
line = Image.open('wire.png')
# third dimension is for colours
a = zeros((100, 100, 3), dtype=uint8)
 
step = array([0, 1])
position = array([0, 0])
count = 0
 
# continue until when the next spot expected to be unfilled is filled
while not max(a[tuple(position)]):
	# paint colour
	a[tuple(position)] = array(line.getpixel((count, 0)))
 
	# if reached a wall
	if max(position + step) > 99 or min(position + step) < 0 or max(a[tuple(position + step)]):
		# change direction
		step = (abs(step) ^ 1) * (-step[0] if step[0] else step[1])
 
	# advance
	position += step
	count += 1
 
# convert to image
Image.fromarray(a).save('swirl.jpeg')
 Scroll to top