Created
April 6, 2013 20:46
-
-
Save ernado/5327567 to your computer and use it in GitHub Desktop.
draw.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ImageDraw | |
import Image | |
def draw_circle(draw, x, y, r, color=(0, 0, 0), bg=(255, 255, 255), d=2): | |
draw.ellipse((x - r, y - r, x + r, y + r), fill=color) | |
r -= d | |
draw.ellipse((x - r, y - r, x + r, y + r), fill=bg) | |
def draw_net(filename, size=35, circle_k=0.8, students=24, lessons=6, border=2, supersampling_k=2): | |
""" | |
Generates the net table blank by given data | |
:param filename: path to output image file | |
:param size: size of the table cell | |
:param circle_k: circle diameter / cell size | |
:param students: number of students | |
:param lessons: number of lessons | |
:param border: border in pixels | |
:param supersampling_k: upscale number | |
""" | |
size *= supersampling_k # upscaling the cell size | |
# COLOR CONSTANTS | |
WHITE = (255, 255, 255) | |
BLACK = (0, 0, 0) | |
# Calculating the width and height | |
width = int((2 + students) * size) | |
height = int((2 + lessons) * size) | |
image_size = (width, height) # packing to the tuple | |
# Creating new image and initializing draw object | |
im = Image.new('RGB', image_size) | |
draw = ImageDraw.Draw(im) | |
# filling with white color | |
draw.rectangle((0, 0, width, height), WHITE) # fill | |
# drawing markets | |
draw.rectangle((0, 0, size, size), BLACK) # TL | |
draw.rectangle((width-size, 0, width, size), BLACK) # TR | |
draw.rectangle((width-size, height - size, width, height), BLACK) # BR | |
draw.rectangle((0, height - size, size, height), BLACK) # BL | |
# drawing circles | |
circle_r = (size/2) * circle_k | |
for student in range(0, students): | |
for lesson in range(0, lessons): | |
x = size * (1.5 + student) # size + 0.5*size + size*student | |
y = size * (1.5 + lesson) # size + 0.5*size + size*lesson | |
draw_circle(draw, x, y, circle_r, BLACK, WHITE, border) | |
# text_pos = (10, 10) | |
# draw.text(text_pos, 'Test', (255, 0, 0)) | |
# downscaling | |
im.thumbnail((int(width/supersampling_k), int(height/supersampling_k)), Image.ANTIALIAS) | |
im.save(filename) | |
# print devices | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description='CyWarden blank generator') | |
parser.add_argument('output', help='Output image file') | |
parser.add_argument('-students', default=24, help='Number of students (default=24)', type=int) | |
parser.add_argument('-lessons', default=6, help='Number of lessons (default=6)', type=int) | |
parser.add_argument('-size', default=30, help='Cell size (default=30)', type=int) | |
parser.add_argument('-border', default=2, help='Border in pixels (default=2)', type=int) | |
parser.add_argument('-sampling', default=2.0, help='Supersampling k (default=2)', type=float) | |
parser.add_argument('-ratio', default=0.8, help='circle_diameter/cell_size (default=0.8)', type=float) | |
parser.add_argument('--o', help='Optimize by optipng', action='store_true', default=False) | |
parser.add_argument('--silent', help='Silent mode', action='store_true', default=False) | |
args = parser.parse_args() | |
verbose = not args.silent | |
# if args.o: | |
# print 'Optimized' | |
if verbose: | |
print 'Generation starded' | |
draw_net(args.output, args.size, args.ratio, args.students, args.lessons, args.border, args.sampling) | |
if verbose: | |
print 'Blank generated' | |
if args.o: | |
import subprocess | |
s = subprocess.Popen(['optipng', args.output], | |
stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0] | |
if verbose: | |
print s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment