In [126]:
import cv2
import PIL
video = cv2.VideoCapture('source/DSC_0475.mov')
In [128]:
count = 1
success = True
while success:
success, image = video.read()
cv2.imwrite('input/frame%d.png' % count, image)
count += 1
In [77]:
from PIL import Image, ImageDraw
class IImage(object):
def __init__(self, arg):
if type(arg) == type('path'):
self.pil = Image.open(arg, 'r')
elif type(arg) == type(('size', 'tuple')):
self.pil = Image.new('RGBA', arg, 'black')
else:
self.pil = arg
self.width, self.height = self.pil.size; self.frame = 1
def save(self, path, extension='JPEG', quality=80):
self.pil.save(path, extension, quality=quality)
return self
def flipH(self):
data = (self.width, 0, 0, self.height)
self.pil = self.pil.transform((self.width,self.height), Image.EXTENT, data)
return self
def flipV(self):
data = (0, self.height, self.width, 0)
self.pil = self.pil.transform((self.width,self.height), Image.EXTENT, data)
return self
def flipHV(self):
return self.flipH().flipV()
def flipVH(self):
return self.flipV().flipH()
def duplicateMirrorV(self):
canvas = IImage((self.width*2, self.height))
return canvas.paste(self, 0, 0).paste(self.flipV(), self.width, 0)
def rotate(self, angle):
self.pil = self.pil.rotate(angle)
return self
def paste(self, iimg, x, y):
self.pil.paste(iimg.pil, (x,y))
return self
def thumbnail(self, width, height, mode):
self.pil.thumbnail((width,height), mode)
self.width, self.height = self.pil.size
return self
def compositeWith(self, iimg, mask):
self.pil = Image.composite(self.pil, iimg.pil, mask.pil.convert('L'))
return self
def fillPolygon(self, points, color):
maskdraw = ImageDraw.Draw(self.pil)
maskdraw.polygon(points, color)
return self
def copy(self):
return IImage(self.pil.copy())
def makeSquare(self):
side = min(self.width, self.height)
box = (
(self.width - side) // 2,
(self.height - side) // 2,
(self.width + side) // 2,
(self.height + side) // 2
)
self.pil = self.pil.crop(box)
self.width, self.height = self.pil.size
return self
def mirrorTopLeft(self):
points = [
(0, 0),
(0, self.height),
(self.width, 0)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(-90).flipV()
self.compositeWith(rotated, mask)
return self
def mirrorTopRight(self):
points = [
(0, 0),
(self.width, 0),
(self.width, self.height)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(90).flipV()
self.compositeWith(rotated, mask)
return self
def mirrorBottomLeft(self):
points = [
(0, 0),
(0, self.height),
(self.width, self.height)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(90).flipV()
self.compositeWith(rotated, mask)
return self
def mirrorBottomRight(self):
points = [
(0, self.height),
(self.width, self.height),
(self.width, 0)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(-90).flipV()
self.compositeWith(rotated, mask)
return self
def blendWith(self, iimg, alpha=0.5):
self.pil = Image.blend(self.pil, iimg.pil, alpha)
return self
def kaleidoscope(self, keepOriginalSize=True):
img0 = self.copy().flipH()
img1 = self.copy().flipV()
img2 = self.copy().flipHV()
return IImage.combine(self, img0, img1, img2, keepOriginalSize)
@classmethod
def combine(cls, iimg0, iimg1, iimg2, iimg3, keepOriginalSize=True):
width, height = iimg0.width, iimg0.height
newSize = (width*2, height*2)
canvas = IImage(newSize)
canvas.paste(iimg0, 0, 0)
canvas.paste(iimg1, width, 0)
canvas.paste(iimg2, 0, height)
canvas.paste(iimg3, width, height)
if keepOriginalSize:
return canvas.thumbnail(width, height, Image.NEAREST)
return canvas
In [125]:
import subprocess
import os, sys
from IPython.display import Image as IPmg
class IImage2(IImage):
def makeTriangle(self):
im = self.pil
#first mask
mask = Image.new('L', im.size, color=255)
draw = ImageDraw.Draw(mask)
triangles = [(self.width/2, 0), (0, self.height), (0,0),
(self.width, 0), (self.width, self.height), (self.width/2, 0)]
draw.polygon(triangles, fill=0)
im.putalpha(mask)
im.save('output/frame{}.png'.format(self.frame))
im = Image.open('output/frame{}.png'.format(self.frame))
self.pil = im
self.frame +=1
#display(IPmg(filename='temp.png', height=300, width=300))
return self
class Pattern:
def __init__(self, paths):
self.folder = os.path.split(paths)[0]
self.filenames = os.path.split(paths)[1]
self.imgPaths = paths
if not os.path.exists(self.folder):
raise Exception('Directory does not exist')
self.imgNumber = len(os.listdir(self.folder))
if self.imgNumber == 0:
raise Exception('Empty folder')
if '.' in self.filenames:
self.extension = self.filenames[self.filenames.rindex('.'):]
def toVideo(self, outputPath='output.mpg', frameRate="24"):
sys.stdout.write("Writing video...")
sys.stdout.flush()
subprocess.call([
"ffmpeg",
"-r", frameRate, #frame rate
"-i", self.imgPaths, #input path
"-qscale", '1', #same quality as source
"-y", #overwrite output
outputPath #output path
], stderr=subprocess.PIPE)
sys.stdout.write("done\n")
return self
def apply(self, methodList):
for method in methodList:
for n in range(1, self.imgNumber):
displayProgress('Applying '+method, n, self.imgNumber-1)
imgPath = self.imgPaths.format(n)
iimg = IImage2(imgPath)
print iimg.pil.filename
iimg = getattr(iimg, method)()
#convert to png
pngpath = imgPath[:imgPath.find('.')+1] + 'png'
iimg.save(pngpath)
return self
def displayProgress(text, n, total):
sys.stdout.write("\r" + text + " %.2f%%" % (n*100/float(total)))
sys.stdout.flush()
if n==total:
sys.stdout.write("\n")
In [122]:
pattern = Pattern('output/frame{}.png')
pattern.apply(['makeTriangle'])
Out[122]:
In [ ]: