Download presentation
Presentation is loading. Please wait.
Published byBertram Porter Modified over 9 years ago
1
Image Operations 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
2
Image Operations Most operations built into the library
3
Multimedia ProgrammingImage Operations Most operations built into the library import sys from PIL import Image, ImageFilter filename = sys.argv[1] p = Image.open(filename) p.filter(ImageFilter.BLUR).save('blur-' + filename) p.filter(ImageFilter.CONTOUR).save('contour-' + filename) p.filter(ImageFilter.EMBOSS).save('emboss-' + filename) p.transpose(Image.FLIP_LEFT_RIGHT).save('flip-' + filename)
4
Multimedia ProgrammingImage Operations blur emboss contour flip
5
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates
6
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
7
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
8
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
9
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
10
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
11
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
12
Multimedia ProgrammingImage Operations Basic image editing is mostly about coordinates original = Image.open(sys.argv[1]) x_size, y_size = original.size x_half, y_half = x_size / 2, y_size / 2 half = original.resize((x_half, y_half)) x_double, y_double = x_size * 2, y_size * 2 double = Image.new('RGB', (x_double, y_double)) for x in range(4): for y in range(4): box = (x * x_half, y * y_half, (x+1) * x_half, (y+1) * y_half) double.paste(half, box) double.save(sys.argv[2])
13
Multimedia ProgrammingImage Operations
14
Multimedia ProgrammingImage Operations Draw on images import sys from PIL import Image, ImageDraw BORDER = 10 GRAY = (128, 128, 128) pic = Image.open(sys.argv[1]) xsize, ysize = pic.size draw = ImageDraw.Draw(pic) draw.rectangle((0, 0, xsize, BORDER), fill=GRAY) draw.rectangle((0, 0, BORDER, ysize), fill=GRAY) draw.rectangle((0, ysize-BORDER, xsize, ysize), fill=GRAY) draw.rectangle((xsize-BORDER, 0, xsize, ysize), fill=GRAY) pic.save('border-' + sys.argv[1])
15
Multimedia ProgrammingImage Operations Draw on images import sys from PIL import Image, ImageDraw BORDER = 10 GRAY = (128, 128, 128) pic = Image.open(sys.argv[1]) xsize, ysize = pic.size draw = ImageDraw.Draw(pic) draw.rectangle((0, 0, xsize, BORDER), fill=GRAY) draw.rectangle((0, 0, BORDER, ysize), fill=GRAY) draw.rectangle((0, ysize-BORDER, xsize, ysize), fill=GRAY) draw.rectangle((xsize-BORDER, 0, xsize, ysize), fill=GRAY) pic.save('border-' + sys.argv[1])
16
Multimedia ProgrammingImage Operations Draw on images import sys from PIL import Image, ImageDraw BORDER = 10 GRAY = (128, 128, 128) pic = Image.open(sys.argv[1]) xsize, ysize = pic.size draw = ImageDraw.Draw(pic) draw.rectangle((0, 0, xsize, BORDER), fill=GRAY) draw.rectangle((0, 0, BORDER, ysize), fill=GRAY) draw.rectangle((0, ysize-BORDER, xsize, ysize), fill=GRAY) draw.rectangle((xsize-BORDER, 0, xsize, ysize), fill=GRAY) pic.save('border-' + sys.argv[1])
17
Multimedia ProgrammingImage Operations Draw on images Exercise: put frame around entire image
18
Multimedia ProgrammingImage Operations Work with color bands import sys from PIL import Image filename = sys.argv[1] pic = Image.open(filename) pic.load() bands = pic.split() for (i, name) in enumerate('rgb'): bands[i].save(filename.replace('.', '-%s.' % name))
19
Multimedia ProgrammingImage Operations Work with color bands import sys from PIL import Image filename = sys.argv[1] pic = Image.open(filename) pic.load() bands = pic.split() for (i, name) in enumerate('rgb'): bands[i].save(filename.replace('.', '-%s.' % name)) (red[], green[], blue[])
20
Multimedia ProgrammingImage Operations Work with color bands import sys from PIL import Image filename = sys.argv[1] pic = Image.open(filename) pic.load() bands = pic.split() for (i, name) in enumerate('rgb'): bands[i].save(filename.replace('.', '-%s.' % name)) workaround
21
Multimedia ProgrammingImage Operations Work with color bands redgreenblue
22
Multimedia ProgrammingImage Operations Use point functions to manipulate pixel values R, G, B = 0, 1, 2 SCALE = 0.5 def decrease(x): return x * SCALE pic = Image.open(sys.argv[1]) pic.load() bands = pic.split() bands = (bands[R].point(decrease), bands[G], bands[B]) more_red = Image.merge('RGB', bands) more_red.save('bluegreen-' + sys.argv[1])
23
Multimedia ProgrammingImage Operations Work with color bands and point functions R, G, B = 0, 1, 2 SCALE = 0.5 def decrease(x): return x * SCALE pic = Image.open(sys.argv[1]) pic.load() bands = pic.split() bands = (bands[R].point(decrease), bands[G], bands[B]) more_red = Image.merge('RGB', bands) more_red.save('bluegreen-' + sys.argv[1])
24
Multimedia ProgrammingImage Operations Work with color bands and point functions R, G, B = 0, 1, 2 SCALE = 0.5 def decrease(x): return x * SCALE pic = Image.open(sys.argv[1]) pic.load() bands = pic.split() bands = (bands[R].point(decrease), bands[G], bands[B]) more_red = Image.merge('RGB', bands) more_red.save('bluegreen-' + sys.argv[1])
25
Multimedia ProgrammingImage Operations Work with color bands and point functions R, G, B = 0, 1, 2 SCALE = 0.5 def decrease(x): return x * SCALE pic = Image.open(sys.argv[1]) pic.load() bands = pic.split() bands = (bands[R].point(decrease), bands[G], bands[B]) less_red = Image.merge('RGB', bands) less_red.save('bluegreen-' + sys.argv[1])
26
Multimedia ProgrammingImage Operations Less red makes the image look more blue/green
27
Multimedia ProgrammingImage Operations Less red makes the image look more blue/green What happens if you increase blue and green instead?
28
Multimedia ProgrammingImage Operations Highlight a region in an image
29
Multimedia ProgrammingImage Operations Highlight a region in an image Option 1: recolor the pixels
30
Multimedia ProgrammingImage Operations Highlight a region in an image Option 1: recolor the pixels Option 2: blend with a square of desired size –New pixel = (left pixel + right pixel) / 2
31
Multimedia ProgrammingImage Operations Figure out the coordinates major_x major_y highlight_x highlight_y Low x=(major_x / 2) – (highlight_x / 2) =(major_x – highlight_x) / 2 High x=(major_x / 2) + (highlight_x / 2) =(major_x + highlight_x) / 2
32
Multimedia ProgrammingImage Operations BLEND = 0.5 major_name, highlight_name = sys.argv[1:3] major, major_x, major_y = get(major_name) highlight, highlight_x, highlight_y = get(highlight_name) box = ((major_x - hl_x) / 2, (major_y - hl_y) / 2, (major_x + hl_x) / 2, (major_y + hl_y) / 2) middle = major.crop(box) middle = Image.blend(middle, highlight, BLEND) major.paste(middle, box) major.save('higlight-' + major_name)
33
Multimedia ProgrammingImage Operations BLEND = 0.5 major_name, highlight_name = sys.argv[1:3] major, major_x, major_y = get(major_name) highlight, highlight_x, highlight_y = get(highlight_name) box = ((major_x - hl_x) / 2, (major_y - hl_y) / 2, (major_x + hl_x) / 2, (major_y + hl_y) / 2) middle = major.crop(box) middle = Image.blend(middle, highlight, BLEND) major.paste(middle, box) major.save('higlight-' + major_name)
34
Multimedia ProgrammingImage Operations BLEND = 0.5 major_name, highlight_name = sys.argv[1:3] major, major_x, major_y = get(major_name) highlight, highlight_x, highlight_y = get(highlight_name) box = ((major_x - hl_x) / 2, (major_y - hl_y) / 2, (major_x + hl_x) / 2, (major_y + hl_y) / 2) middle = major.crop(box) middle = Image.blend(middle, highlight, BLEND) major.paste(middle, box) major.save('higlight-' + major_name)
35
Multimedia ProgrammingImage Operations BLEND = 0.5 major_name, highlight_name = sys.argv[1:3] major, major_x, major_y = get(major_name) highlight, highlight_x, highlight_y = get(highlight_name) box = ((major_x - hl_x) / 2, (major_y - hl_y) / 2, (major_x + hl_x) / 2, (major_y + hl_y) / 2) middle = major.crop(box) middle = Image.blend(middle, highlight, BLEND) major.paste(middle, box) major.save('higlight-' + major_name)
36
Multimedia ProgrammingImage Operations BLEND = 0.5 major_name, highlight_name = sys.argv[1:3] major, major_x, major_y = get(major_name) highlight, highlight_x, highlight_y = get(highlight_name) box = ((major_x - hl_x) / 2, (major_y - hl_y) / 2, (major_x + hl_x) / 2, (major_y + hl_y) / 2) middle = major.crop(box) middle = Image.blend(middle, highlight, BLEND) major.paste(middle, box) major.save('higlight-' + major_name)
37
Multimedia ProgrammingImage Operations BLEND = 0.5 major_name, highlight_name = sys.argv[1:3] major, major_x, major_y = get(major_name) highlight, highlight_x, highlight_y = get(highlight_name) box = ((major_x - hl_x) / 2, (major_y - hl_y) / 2, (major_x + hl_x) / 2, (major_y + hl_y) / 2) middle = major.crop(box) middle = Image.blend(middle, highlight, BLEND) major.paste(middle, box) major.save('higlight-' + major_name)
38
Multimedia ProgrammingImage Operations PIL provides basic image processing
39
Multimedia ProgrammingImage Operations PIL provides basic image processing OpenCV (http://opencv.willowgarage.com) is a complete image processing library
40
Multimedia ProgrammingImage Operations PIL provides basic image processing OpenCV (http://opencv.willowgarage.com) is a complete image processing library Have to convert images…
41
Multimedia ProgrammingImage Operations PIL provides basic image processing OpenCV (http://opencv.willowgarage.com) is a complete image processing library Have to convert images… …but it's worth it
42
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.