Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_4x4_combos.py
- from Tkinter import *
- import itertools
- import os
- from PIL import ImageGrab
- from PIL import ImageDraw
- from PIL import Image
- # Number of squares in x and y dimensions.
- n = 4
- # Total dimensions of display.
- t = 64
- ww = t
- hh = t
- '''
- directory = tkFileDialog.askdirectory()
- print (directory)
- '''
- directory = "C:/py/artio"+"/"
- root = Tk()
- root.geometry("%sx%s+0+0" % (ww, hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- img_pil = Image.new("RGB", (ww, hh))
- cv_pil = ImageDraw.Draw(img_pil)
- def pil_to_jpg(filename, img):
- img.save(filename)
- def combine(zeros, ones):
- for indices in itertools.combinations(range(zeros+ones), ones):
- item = ['0'] * (zeros+ones)
- for index in indices:
- item[index] = '1'
- yield ''.join(item)
- def make_square(x, y, color):
- return canvas.create_rectangle(x * ss, y * ss, (x+1) * ss, (y+1) * ss,
- fill=color, outline=color)
- def make_squares():
- squares = []
- for x in range(n):
- squares.append([])
- for y in range(n):
- squares[x].append(make_square(x, y, 'white'))
- return squares
- done = False
- def quit_handler(event):
- global done
- done = True
- # Left mouse button click on canvas terminates the program.
- root.bind('<Button-1>', quit_handler)
- ss = ww/n
- squares = make_squares()
- black = n*n/2
- ppp = []
- while black < n*n+1:
- white = n*n-black
- combos = combine(white, black)
- for combo in combos:
- seq = combo
- combo = list(seq)
- for x in range(len(squares)):
- for y in range(len(squares[x])):
- sq = squares[x][y]
- color = 'black' if int(combo.pop()) else 'white'
- canvas.itemconfig(sq, fill=color, outline=color)
- cv_pil.rectangle((x*ss,y*ss,x*ss+ss,y*ss+ss), fill=color, outline=color)
- root.update()
- filename = directory+seq+".jpg"
- pil_to_jpg(filename, img_pil)
- black += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement