예제 #1
0
average = map(int,map(round, cam_im_array.mean(1).mean(0)))
temp = numpy.array([[[0,0,0]]*cam_height]*cam_width,dtype=numpy.uint8)
#temp = numpy.array([[[0,0,0]]*cam_width]*cam_height, dtype=numpy.uint8)                            
whistler.save_im = Image.fromarray(temp)
whistler.im = Image.fromarray(cam_im_array)

whistler.width, whistler.height = cam_height, cam_width#, cam_height

#some defaults
whistler.min_width = cam_width/16
whistler.max_width = cam_width/4
whistler.min_height = cam_height/16
whistler.max_height = cam_height/4
whistler.p_alpha = 0.0125
whistler.remaining = whistler.abs_diff(whistler.save_im, whistler.im)
old_remaining = whistler.remaining

screen = pygame.display.set_mode([cam_width, cam_height])# pygame.FULLSCREEN)
pygame.mouse.set_visible(0)
pygame.display.set_caption("Live Painting")

c = 0
while True :
    for e in pygame.event.get() :
        if e.type == pygame.QUIT\
           or e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
            pygame.camera.quit()
            sys.exit()
    # try to draw something
    while whistler.fail_count < 1 and whistler.remaining <= old_remaining:
예제 #2
0
def draw_and_compare():
    #global save_im, remaining, fail_count, last_saved_count
    global colors
    
    save_im_copy = whistler.save_im.copy()

    #poly_rect_area = get_rand_rect()

    rx1, ry1 = randint(0, whistler.width), randint(0, whistler.height)
    rim = choice(imgs)
    canvasOffset = 0
    rx1 = (rx1 - ((rx1 - canvasOffset) % rim.size[0]));
    ry1 = (ry1 - ((ry1 - canvasOffset) % rim.size[1]));

    offset = choice([1,2,3,4,5,6])
    if offset==1:
        rx1 = rx1 - rim.size[0]/2
        ry1 = ry1 - rim.size[1]/2
    if offset==2:
        rx1 = rx1 - rim.size[0]/2
        ry1 = ry1 + rim.size[1]/2
    if offset==3:
        rx1 = rx1 + rim.size[0]/2
        ry1 = ry1 - rim.size[1]/2
    if offset==4:
        rx1 = rx1 + rim.size[0]/2
        ry1 = ry1 + rim.size[1]/2
    rangle = randint(0,359)
    rim = rim.rotate(rangle, Image.NEAREST, expand=True)
    if choice([1,2]) == 2:
        rim = rim.transpose(Image.FLIP_LEFT_RIGHT)
    if choice([1,2]) == 2:
        rim = rim.transpose(Image.FLIP_TOP_BOTTOM)
    
    rx2 = rx1 + rim.size[0]
    ry2 = ry1 + rim.size[1]

    #save_im_crop = whistler.save_im.crop((rx1, ry1, rx2, ry2))
    #save_im_crop.load()
    #draw_rand_polys(save_im_crop)

    im_crop = whistler.im.crop((rx1, ry1, rx2, ry2))
    save_im_copy_crop = save_im_copy.crop((rx1, ry1, rx2, ry2))    
    im_crop.load(), save_im_copy_crop.load()

    r,g,b = varyb(vary(choice(colors)))
    
    rc, bc, bc, ralpha = rim.split()
    
    blob = ImageOps.colorize(ImageOps.grayscale(rim.convert('RGB')),(r,g,b,0), (255,255,255,0))
    #blob = rim.convert('RGB')
    blob.filter(ImageFilter.BLUR)
    ralpha.filter(ImageFilter.BLUR)
    blob.putalpha(ralpha)
    whistler.save_im.paste(blob,
                 (rx1, ry1), mask=ralpha)
    painted_crop = whistler.save_im.crop((rx1, ry1, rx2, ry2))
    painted_crop.load()

    old_diff = whistler.abs_diff(save_im_copy_crop,im_crop)
    new_diff = whistler.abs_diff(painted_crop,im_crop)
    
    if new_diff < old_diff:
        whistler.remaining -= old_diff-new_diff
        #print ':\timproved\t' + str(old_diff-new_diff)\
        #             + '\tremaining:\t' + str(remaining)
        whistler.last_saved_count = 0
        whistler.fail_count=0
    else:
        whistler.save_im = save_im_copy
        whistler.fail_count +=1