Created
December 14, 2020 16:48
-
-
Save Cewein/e44f758dc33004dd9e469bcd41f9f6fc to your computer and use it in GitHub Desktop.
A small list of function for making gif
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Usefull libraries | |
from PIL import Image | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import glob | |
from PIL import Image | |
import re | |
import os | |
def atof(text): | |
try: | |
retval = float(text) | |
except ValueError: | |
retval = text | |
return retval | |
def natural_keys(text): | |
''' | |
alist.sort(key=natural_keys) sorts in human order | |
http://nedbatchelder.com/blog/200712/human_sorting.html | |
(See Toothy's implementation in the comments) | |
float regex comes from https://stackoverflow.com/a/12643073/190597 | |
''' | |
return [ atof(c) for c in re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text) ] | |
#custiom image displaying function that allow saving and also indexing | |
def imshow(I,title=None,size=500,save=False,index=-1): | |
""" display an image with a specific size """ | |
plt.figure(figsize=(size//80,size//80)) | |
plt.gray() | |
plt.imshow(I) | |
if title: | |
if index != -1: | |
plt.title(title+"-"+str(index)) | |
else: | |
plt.title(title) | |
if(save): | |
plt.savefig(f'{title}-{index}.png') | |
plt.close() | |
else: | |
plt.show() | |
pass | |
#create a gif from list of data: Ilist and a list of name : IName | |
def createGif(Ilist,IName): | |
#pass trough all image in Ilist | |
for x in range(len(Ilist)): | |
#save image for the gif | |
for i in range(0,50): | |
#change what you want to do here, this is just the computation the saving append after | |
qt = split(Ilist[x],(0,0,int(Ilist[x].shape[0]),int(Ilist[x].shape[1])),[],i) | |
l2 = listRegions(qt) | |
#save the image on you computer | |
imshow(drawRegions(l2,Ilist[x]),IName[x],save=True,index=i) | |
#get all the file generated | |
fp_in = IName[x]+"-*.png" | |
fp_out = IName[x]+".gif" | |
#process them into a gif | |
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in),key=natural_keys)] | |
img.save(fp=fp_out, format='GIF', append_images=imgs, | |
save_all=True, duration=100, loop=0) | |
#perform image cleanup | |
for f in sorted(glob.glob(fp_in),key=natural_keys): | |
os.remove(f) | |
#createGif([cam,angio,muscle,prisme,seiche],["camera","angio","muscle","primse","seiche"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment