-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreen_screen.py
58 lines (43 loc) · 1.54 KB
/
green_screen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# @author: Xavier Collantes
from PIL import Image
from PIL import ImageColor
import os, arrow
def greenScreen(bg, gs):
# This operation takes a while, let's keep track with a timer
# Use Tableau to find green threshold value to be used below
p = 'green_values.csv'
file = open(p, 'w+')
file.write("Red, Green, Blue\n")
# Establish counter for green screen image
WIDTH = gs.size[0] - 1
HEIGHT = gs.size[1] - 1
pos = (0, 0)
try:
# Stop when smallest dimension is reached on green screen image
while pos[0] <= WIDTH:
while pos[1] <= HEIGHT:
print("processing pixel: ", pos)
if gs.getpixel(pos)[1] > 150: # If pixel on green screen image is somewhat green
print("change pixel: ", pos)
frontColor = bg.getpixel(pos) # Get corresponding pixel color of the same coordinates
gs.putpixel(pos, frontColor)
else:
pass
try:
o = str(gs.getpixel(pos)[0]) + ", " + str(gs.getpixel(pos)[1]) + ", " + str(gs.getpixel(pos)[2]) + "\n"
file.write(o) # Output RGB values for analysis
except IOError as ioe:
print("Error with file output %s" %ioe)
pos = (pos[0], pos[1] + 1) # Increment height by 1
pos = (pos[0], 0)
pos = (pos[0] + 1, pos[1]) # Increment width by 1
except Exception as e:
print("Iteration issue %s" %e)
pass
file.close()
gs.save(arrow.now('US/Pacific').format('DD-MMM-YY_HH-mm-SS') + '.png')
if __name__=='__main__':
os.chdir('lib/')
back = Image.open('transpose_horizontal.png')
shia = Image.open('shia.jpg')
greenScreen(back, shia)