31
Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution Lice See http://software-carpentry.org/license.html for more informati Multimedia Programming

Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Embed Size (px)

Citation preview

Page 1: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Images

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Multimedia Programming

Page 2: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Pictures are much older than text

Just as easy to work with…

…given the right libraries

Explore the Python Imaging Library (PIL)

Other languages have similar tools

Page 3: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Start by loading the image into memory

>>> from PIL import Image

>>> pic = Image.open('ngc1333-noao.jpg')

Page 4: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Examine image properties

>>> pic.format

'JPEG'

>>> pic.size

(640, 480)

640

480

Page 5: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Colors represented by red-green-blue (RGB) triples

Black is (0, 0, 0)

White is (255, 255, 255) or (0xFF, 0xFF, 0xFF)

A color cube

redgreen

blue

black

whitecyan

magenta

yellow

Page 6: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Pixel coordinates are (x, y) tuples

(0, 0) is the upper left corner of the image

– Because that's how old CRT monitors drew things

>>> pic.getpixel((0, 0)) # upper left corner

(17, 12, 18)

Page 7: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find the brightest pixel

Page 8: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find the brightest pixel What does "brightest"

actually mean?

Page 9: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find the brightest pixel

>>> xsize, ysize = pic.size

>>> bx, by, max_val = 0, 0, 0

>>> for x in range(xsize):

... for y in range(ysize):

... r, g, b = pic.getpixel((x, y))

... if r + g + b > max_val:

... bx, by, total = x, y, r + g + b

... print (bx, by), total

...

Page 10: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find the brightest pixel

>>> xsize, ysize = pic.size

>>> bx, by, max_val = 0, 0, 0

>>> for x in range(xsize):

... for y in range(ysize):

... r, g, b = pic.getpixel((x, y))

... if r + g + b > max_val:

... bx, by, total = x, y, r + g + b

... print (bx, by), total

...

(59, 345) 758

Page 11: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

How fast is that?

Page 12: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

How fast is that?

def brightest(picture):

...as above...

return (bx, by), total

Page 13: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

How fast is that?

def brightest(picture):

...as above...

return (bx, by), total

from time import time

def elapsed(func, picture):

start = time()

result = func(picture)

return time() - start, result

Page 14: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

How fast is that?

0.63 seconds

def brightest(picture):

...as above...

return (bx, by), total

from time import time

def elapsed(func, picture):

start = time()

result = func(picture)

return time() - start, result

Page 15: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Ignore coordinates

Page 16: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Ignore coordinates

def faster(picture):

max_val = 0

for (r, g, b) in picture.getdata():

if r + g + b > max_val:

max_val = r + g + b

return max_val

Page 17: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Ignore coordinates

def faster(picture):

max_val = 0

for (r, g, b) in picture.getdata():

if r + g + b > max_val:

max_val = r + g + b

return max_val

Pixels ordered row by row

Page 18: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Ignore coordinates

def faster(picture):

max_val = 0

for (r, g, b) in picture.getdata():

if r + g + b > max_val:

max_val = r + g + b

return max_val

Pixels ordered row by row

0.07 seconds

Page 19: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Ignore coordinates

def faster(picture):

max_val = 0

for (r, g, b) in picture.getdata():

if r + g + b > max_val:

max_val = r + g + b

return max_val

Pixels ordered row by row

Exercise: return (x, y) coordinate of brightest pixel

0.07 seconds

Page 20: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

A useful compromise

Page 21: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

A useful compromise

def inbetween(picture):

xsize, ysize = picture.size

temp = picture.load()

bx, by, max_val = 0, 0, 0

for x in range(xsize):

for y in range(ysize):

r, g, b = temp[x, y]

if r + g + b > max_val:

bx, by, total = x, y, r + g + b

return (bx, by), total

Page 22: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

A useful compromise

def inbetween(picture):

xsize, ysize = picture.size

temp = picture.load()

bx, by, max_val = 0, 0, 0

for x in range(xsize):

for y in range(ysize):

r, g, b = temp[x, y]

if r + g + b > max_val:

bx, by, total = x, y, r + g + b

return (bx, by), total

0.13 seconds

Page 23: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find stars

Page 24: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find stars

Convert to black and white

Page 25: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find stars

Convert to black and white Easier to see

black on white

than vice versa

Page 26: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

Find stars

Convert to black and white

def monochrome(picture, threshold):

black = ( 0, 0, 0)

white = (255, 255, 255)

xsize, ysize = picture.size

temp = picture.load()

for x in range(xsize):

for y in range(ysize):

r, g, b = temp[x, y]

if r + g + b >= threshold: temp[x, y] = black

else: temp[x, y] = white

Page 27: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

if __name__ == '__main__':

pic = Image.open(sys.argv[1])

monochrome(pic, 200 + 200 + 200)

pic.save(sys.argv[2])

Page 28: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

if __name__ == '__main__':

pic = Image.open(sys.argv[1])

monochrome(pic, 200 + 200 + 200)

pic.save(sys.argv[2])Not the same

as (200, 200, 200)

Page 29: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

if __name__ == '__main__':

pic = Image.open(sys.argv[1])

monochrome(pic, 200 + 200 + 200)

pic.save(sys.argv[2])

Page 30: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Multimedia Programming Images

if __name__ == '__main__':

pic = Image.open(sys.argv[1])

monochrome(pic, 200 + 200 + 200)

pic.save(sys.argv[2])

Now we can

start counting

Page 31: Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

November 2010

created by

Greg Wilson

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.