generate sprite from images
This commit is contained in:
36
generator
36
generator
@@ -21,7 +21,7 @@ Options:
|
|||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
from moviepy.editor import VideoFileClip
|
from moviepy.editor import VideoFileClip
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import glob, os, random, sys
|
import glob, os, random, sys, shutil
|
||||||
|
|
||||||
|
|
||||||
TMP_FRAMES_PATH="/tmp/frames/"
|
TMP_FRAMES_PATH="/tmp/frames/"
|
||||||
@@ -33,7 +33,10 @@ def generate_video_thumbnail(args):
|
|||||||
outputPrefix = get_output_prefix()
|
outputPrefix = get_output_prefix()
|
||||||
|
|
||||||
generate_frames(videoFileClip, interval, outputPrefix, size)
|
generate_frames(videoFileClip, interval, outputPrefix, size)
|
||||||
# generate_sprite_from_images()
|
|
||||||
|
columns = int(args['<columns>'])
|
||||||
|
output = args['<output>']
|
||||||
|
generate_sprite_from_images(outputPrefix, columns, size, output)
|
||||||
|
|
||||||
def generate_frames(videoFileClip, interval, outputPrefix, size):
|
def generate_frames(videoFileClip, interval, outputPrefix, size):
|
||||||
print "Extracting", int(videoFileClip.duration / interval), "frames (prefix:" + outputPrefix + ")"
|
print "Extracting", int(videoFileClip.duration / interval), "frames (prefix:" + outputPrefix + ")"
|
||||||
@@ -41,8 +44,6 @@ def generate_frames(videoFileClip, interval, outputPrefix, size):
|
|||||||
extract_frame(videoFileClip, i, outputPrefix, size)
|
extract_frame(videoFileClip, i, outputPrefix, size)
|
||||||
print "Frames extracted."
|
print "Frames extracted."
|
||||||
|
|
||||||
# framesMap = sorted(glob.glob(outputPrefix + "*.png"))
|
|
||||||
# images = [Image.open(filename) for filename in framesMap]
|
|
||||||
|
|
||||||
def extract_frame(videoFileClip, moment, outputPrefix, size):
|
def extract_frame(videoFileClip, moment, outputPrefix, size):
|
||||||
sys.stdout.write(".")
|
sys.stdout.write(".")
|
||||||
@@ -52,11 +53,30 @@ def extract_frame(videoFileClip, moment, outputPrefix, size):
|
|||||||
resize_image(output, size)
|
resize_image(output, size)
|
||||||
|
|
||||||
def resize_image(filename, size):
|
def resize_image(filename, size):
|
||||||
sys.stdout.write("*")
|
|
||||||
sys.stdout.flush()
|
|
||||||
image = Image.open(filename)
|
image = Image.open(filename)
|
||||||
image.thumbnail(size, Image.ANTIALIAS)
|
image.resize(size, Image.ANTIALIAS)
|
||||||
image.save(filename, "PNG")
|
image.save(filename)
|
||||||
|
|
||||||
|
def generate_sprite_from_images(framesPath, columns, size, output):
|
||||||
|
framesMap = sorted(glob.glob(framesPath + "*.png"))
|
||||||
|
images = [Image.open(filename) for filename in framesMap]
|
||||||
|
print "%d frames will be combined." % len(images)
|
||||||
|
masterWidth = size[0] * columns
|
||||||
|
masterHeight = size[1] * len(images) / columns
|
||||||
|
finalImage = Image.new(mode='RGBA', size=(masterWidth, masterHeight), color=(0,0,0,0))
|
||||||
|
merge_frames(images, finalImage, columns, size, output)
|
||||||
|
|
||||||
|
def merge_frames(images, finalImage, columns, size, output):
|
||||||
|
for i, image in enumerate(images):
|
||||||
|
for j in range(0, columns):
|
||||||
|
locationX = size[0] * j
|
||||||
|
locationY = size[1] * i
|
||||||
|
finalImage.paste(image, (locationX, locationY))
|
||||||
|
|
||||||
|
finalImage.save(output, transparency=0)
|
||||||
|
shutil.rmtree(TMP_FRAMES_PATH, ignore_errors=True)
|
||||||
|
print "Saved!"
|
||||||
|
|
||||||
|
|
||||||
def get_output_prefix():
|
def get_output_prefix():
|
||||||
if not os.path.exists(TMP_FRAMES_PATH):
|
if not os.path.exists(TMP_FRAMES_PATH):
|
||||||
|
|||||||
Reference in New Issue
Block a user