Skip to content

Instantly share code, notes, and snippets.

@Cewein
Last active October 18, 2022 10:41
Show Gist options
  • Save Cewein/229163d5311e5ccd3f5314c4b4810e40 to your computer and use it in GitHub Desktop.
Save Cewein/229163d5311e5ccd3f5314c4b4810e40 to your computer and use it in GitHub Desktop.
Code for morpho math
import tp_morpho
import glob
import re
from PIL import Image
import numpy as np
import platform
import tempfile
import os
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage import io as skio
import skimage.morphology as morpho
import skimage.feature as skf
from scipy import ndimage as ndi
def subplots(arrayPicture, arrayTitle, bigTitle, width=3, height=2, save=False, countSave=0):
fig, axs = plt.subplots(height, width, figsize=(14, 8))
for i in range(height):
for j in range(width):
axs[i,j].imshow(arrayPicture[height * j + i], cmap='gray')
axs[i,j].set_title(arrayTitle[height * j + i])
for ax in axs.flat:
ax.label_outer()
fig.suptitle(bigTitle, fontsize=20)
if save:
if not os.path.exists("gif/"):
os.makedirs("gif/")
fig.savefig(f"gif/image-{countSave}.png")
plt.close()
else:
plt.show()
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def createGif(name):
# filepaths
fp_in = "gif/image-**.png"
fp_out = f"{name}.gif"
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
imgs = (Image.open(f) for f in natural_sort(glob.glob(fp_in)))
img = next(imgs) # extract first image from iterator
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=200, loop=0)
listStrel = ['square', 'diamond', 'disk', 'line']
def getArrayStrel(size=3):
array = []
for i in range(len(listStrel)):
array += [strel(listStrel[i],size)]
return array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment