In [1]:
import os
import shutil
from PIL import Image
In [2]:
# folder 'images' is created and
# video will be split into frames
# also we will create a folder for the gif output here as well
try:
os.system("mkdir images")
os.system("ffmpeg -i anime.mov -vf fps=4/1 images/out%d.png")
os.system("mkdir gifs")
print('Successful')
except:
print('Error Occured')
In [3]:
# initializing the variables
images = []
page_count = 1
counter = 1
i = 1
# I have still haven't figuired out a proper way to count the total number
# of the generated images, therefore you have to input manually for now
total_number_of_frames = 315
# folder manga_pages will be generated
if not os.path.exists("manga_pages"):
os.system("mkdir manga_pages")
while(i < total_number_of_frames):
# a template will be picked to add the images
template = Image.open("templates/template.png")
try:
im = Image.open("images/out"+ str(i) + ".png") #first image
except FileNotFoundError:
im = Image.open('templates/frame_1.png') #if image doesn't exist, it will have white window
# changing the size to fit the frame of the template
size = 710, 351
im = im.resize(size, resample=0)
template.paste(im,(35,28))
# pasting the image into a frame
try:
im2 = Image.open("images/out"+ str(40 + i) + ".png") #second image
except FileNotFoundError:
im2 = Image.open("templates/frame_2.png") #if image doesn't exist, it will have white window
# changing the size to fit the frame of the template
size2 = 709, 306
im2 = im2.resize(size2, resample=0)
template.paste(im2,(37,399))
# pasting the image into a frame
try:
im3 = Image.open("images/out"+ str(80 + i) + ".png") #second image
except FileNotFoundError:
im3 = Image.open("templates/frame_3.png") #if image doesn't exist, it will have white window
#changing the size to fit the frame of the template
size3 = 710, 245
im3 = im3.resize(size3, resample=0)
template.paste(im3,(35,732))
# pasting the image into a frame
images.append(template)
# a generated template image is added to the list 'images'
# each folder or gif will have 40 frames
if(counter == 40):
# we use ffmpeg to create a video and turn video into a gif
try:
os.system('ffmpeg -framerate 10 -i manga_pages/page%d.png output.mp4')
os.system('ffmpeg -t 15 -i output.mp4 gifs/manga_page'+str(page_count)+'.gif -hide_banner')
# cleaning up, deleting the video, since it is not needed
os.remove('output.mp4')
except:
print('Error while generating the gif')
# we want to change the counter value, so it counts again till 40 frames
# and add 80 to 'i' since we do not want to have repeating images
page_count += 1
counter = 0
i += 80
else:
# images are saved into folder manga_pages
template.save("manga_pages/page" + str(counter) + ".png")
counter += 1
i += 1
In [4]:
# time to clean up, deleting the folders manga_pages and images
shutil.rmtree('manga_pages')
shutil.rmtree('images')
In [5]:
# a quick note on how ffmpeg works:
# if you already have generated file using ffmpeg,
# it will throw an error, if succesfull, it will return 0
# therefore, if you generated the gifs, and try to generated them again,
# it will return an Error, make sure to either move the files or delete them.