Hack on your own with Python

How do I take a photo using my webcam?

Google: python webcam

http://stackoverflow.com/questions/9711946/how-to-programmatically-capture-a-webcam-photo

In [101]:
# take the photo
import pygame.camera
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0])
cam.start()
pg_img = cam.get_image()
import pygame.image
pygame.image.save(pg_img, "photo.bmp")
cam.stop()
pygame.camera.quit()

Where do I edit the photo?

Google: python image editing

http://stackoverflow.com/questions/94875/image-processing-in-python

The best-known library is PIL.

In [102]:
# show the photo
import Image
im = Image.open("photo.bmp")
#im.show() # <- this is what you would normally do
# show it in IPython
def show_here(image):
    imshow(numpy.asarray(image))
    show()
show_here(im)

Do I really have to reread it from a file?

Google: read pygame surface in pil

http://stackoverflow.com/questions/14176689/how-to-convert-a-pygame-surface-to-a-pil-image

In [103]:
str_img = pygame.image.tostring(pg_img, "RGBA",False)
size = (pg_img.get_width(), pg_img.get_height())
pil_img = Image.frombytes("RGBA", size, str_img)
imshow(numpy.asarray(pil_img))
show()

How do I become an artist 1?

Google: pil convert image to black or white

http://stackoverflow.com/questions/18777873/convert-rgb-to-black-or-white

In [104]:
from PIL import ImageStat

def bw(image):
    '''make an image pure black and white (no gray)'''
    gray = image.convert('L')
    stat = ImageStat.Stat(im)
    bw_image = gray.point(lambda x: 0 if x<stat.mean[0] else 255, '1')
    return bw_image
bw_img = bw(pil_img)
show_here(bw_img.convert('RGBA'))

How do I become an artist pt. 2?

Google: python pil andy warhol

http://www.mosesschwartz.com/?tag=python-imaging-library

In [109]:
# colors here are taken directly from Warhol's Che Guevara serigraph
# (in order left to right, top to bottom)
colorset = [ 
    {'bg' : (255,255,0,255), 'fg' : (50,9,125,255), 'skin': (118,192,0,255)},
    {'bg' : (0,122,240,255), 'fg' : (255,0,112,255), 'skin': (255,255,0,255)},
    {'bg' : (50,0,130,255),'fg' : (255,0,0,255),'skin': (243,145,192,255)},
    {'bg' : (255,126,0,255),'fg' : (134,48,149,255),'skin': (111,185,248,255)},
    {'bg' : (255,0,0,255),'fg' : (35,35,35,255),'skin': (255,255,255,255)},
    {'bg' : (122,192,0,255),'fg' : (255,89,0,255),'skin': (250,255,160,255)},
    {'bg' : (0,114,100,255),'fg' : (252,0,116,255),'skin': (250,250,230,255)},
    {'bg' : (250,255,0,255),'fg' : (254,0,0,255),'skin': (139,198,46,255)},
    {'bg' : (253,0,118,255),'fg' : (51,2,126,255),'skin': (255,105,0,255)}
]
def color_bg_fg(image, bg_color, fg_color):
    '''change transparent background to bg_color and change
    everything non-transparent to fg_color'''
    fg_layer = Image.new('RGBA', image.size, fg_color)
    bg_layer = Image.new('RGBA', image.size, bg_color) 
    bw_image = bw(image)
    masked_image = Image.composite(fg_layer, bg_layer, bw_image)
    return masked_image

def warholify(image):
    warhols = []
    for colors in colorset:
        bg = colors['bg']
        fg = colors['fg']
        skin = colors['skin']
        warhols.append(color_bg_fg(im, bg, fg))

    x = im.size[0]
    y = im.size[1]

    blank_image = Image.new("RGB", (x*3, y*3))
    blank_image.paste(warhols[0], (0,0))
    blank_image.paste(warhols[1], (x,0))
    blank_image.paste(warhols[2], (x*2,0))
    warhols[2].save('pop-avatar.png')
    blank_image.paste(warhols[3], (0,y))
    blank_image.paste(warhols[4], (x,y))
    blank_image.paste(warhols[5], (x*2,y))
    blank_image.paste(warhols[6], (0,y*2))
    blank_image.paste(warhols[7], (x,y*2))
    blank_image.paste(warhols[8], (x*2,y*2))
    
    return blank_image

warhol = warholify(pil_img)
show_here(warhol)
warhol.save('photo-warhol.jpg')

Conclusion

  • best way to learn - play!
  • if you're not using IPython, you're doing it wrong ;)
ipython nbconvert dev.warhol.ipynb --to slides --post serve


Thank you!

Dražen Lučanin

@kermit666

Dražen's pop-art