【Educoder Assignment】※Digital Image——Green Screen Keying
It seems that the order is not right? Should this come before problem solving?
This assignment is much more basic than the previous blog, and most of it is the use of basic functions.
T1 gets image resolution
Nothing to say, just the whole thing s i z e size size即可。
########## Begin #########
from PIL import Image
path = 'src/step1/boy.jpg'
img = Image.open(path)
w, h = img.size
print('宽度:%d\n高度:%d' %(w, h))
########## End ##########
T2 gets a pixel
from PIL import Image
x = int(input()) #像素的横坐标
y = int(input()) #像素的纵坐标
########## Begin ##########
img = Image.open('src/step2/boy.jpg')
print(img.getpixel((x, y)))
########## End ##########
T3 image reverse color
########## Begin ##########
from PIL import Image
path1 = 'src/step3/source/boy.jpg'
path2 = 'src/step3/student/boy2.jpg'
img = Image.open(path1)
w, h = img.size
for i in range(w) :
for j in range(h) :
r, g, b = img.getpixel((i, j))
r = 255 - r; g = 255 - g; b = 255 - b
img.putpixel((i, j), (r, g, b))
img.save(path2)
########## End ##########
T4 modify transparency
′ R G B A ′ 'RGBA' ′RGBA′ is also an image format
########## Begin ##########
from PIL import Image
path1 = 'src/step4/source/boy.jpg'
path2 = 'src/step4/student/boy2.png'
img = Image.open(path1)
img = img.convert('RGBA')
w, h = img.size
for i in range(w) :
for j in range(h) :
r, g, b, a = img.getpixel((i, j))
img.putpixel((i, j), (r, g, b, 100))
img.save(path2)
########## End ##########
T5 cutout
########## Begin ##########
from PIL import Image
boy = Image.open('src/step5/source/boy.jpg')
boy = boy.convert('RGBA')
w, h = boy.size
for x in range(0, w):
for y in range(0, h):
r, g, b, a = boy.getpixel((x, y))
if ((g + 1) / (r + g + b + 3)) > 0.4 and g > 60: #如果是偏绿色
a = 0
boy.putpixel((x, y), (r, g, b, a))
boy.save('src/step5/student/boy2.png')
########## End ##########
T6 add scene
This is mainly p a s t e paste paste function riface m a s k mask maskIt is clear that转载一版BOOKReference.