Skip to content

Instantly share code, notes, and snippets.

@travis-g
Created February 17, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travis-g/dd2028f18ffb7de76f892f789a6a5eab to your computer and use it in GitHub Desktop.
Save travis-g/dd2028f18ffb7de76f892f789a6a5eab to your computer and use it in GitHub Desktop.
from PIL import Image
import json
import requests
h = 1000
w = 1000
# PIL accesses images in Cartesian co-ordinates, so it is Image[columns, rows]
img = Image.new( 'RGB', (h,w), "black") # create a new black image
pixels = img.load() # create the pixel map
black = 0
white = 0
total = h*w
with requests.Session() as s:
for i in range(img.size[0]): # for every col:
for j in range(img.size[1]): # For every row
response = s.get('http://localhost:6436/roll/d2')
obj = json.loads(response.text)
val = obj['dice']['group'][0]['result']['value']
if val == 1:
black+=1
pixels[i,j] = (0, 0, 0) # set the colour accordingly
elif val == 2:
white+=1
pixels[i,j] = (255,255,255)
else:
print("BAD ERROR")
print("black: {}".format(black/total))
print("white: {}".format(white/total))
# img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment