fast-voice-full / utils.py
broadfield's picture
Update utils.py
db646eb verified
import os
import io
import PIL
import matplotlib
from randimage import get_random_image, show_array
from randimage import GaussianBlobMask, NormalMask, SaltPepperMask, EPWTPath, ProbabilisticPath, ColoredPath, show_array
import random
class make_im():
def __init__(self):
self.root=os.getcwd()
self.c_maps=['Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Grays', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_grey', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gist_yerg', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'grey', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r']
self.img_size = (256,256)
def run_all(self):
#mask = SaltPepperMask(img_size).get_mask()
#mask = NormalMask(img_size).get_mask()
self.mask = GaussianBlobMask(self.img_size).get_mask(3)
# show_array(mask, cmap='gray')
# mask = GaussianBlobMask().get_mask()
self.epwtpath = EPWTPath(self.mask).get_path()
self.ppath = ProbabilisticPath(self.mask).get_path()
#cmap = 'Spectral'
#cmap = 'terrain'
for ea in self.c_maps:
cmap = ea
self.epwtimg = ColoredPath(self.epwtpath, self.mask.shape).get_colored_path(cmap)
self.pimg = ColoredPath(self.ppath, self.mask.shape).get_colored_path(cmap)
if not os.path.isdir(f"{self.root}/image_alls/{ea}"):
os.mkdir(f'{self.root}/images_all/{ea}')
else: pass
buffer = io.BytesIO()
buffer.write(self.pimg)
buffer.seek(0)
#matplotlib.image.imsave(f"{self.root}/images_all/{ea}/{ea}_mask.png", self.mask, cmap='gray')
#matplotlib.image.imsave(f"{self.root}/images_all/{ea}/{ea}_epwt.png", self.epwtimg)
#matplotlib.image.imsave(f"{self.root}/images_all/{ea}/{ea}_prob.png", self.pimg)
return buffer
def run_1(self,name='temp',img_size=[128,128],cmap='flag_r',key=0):
randvar = random.randint(2,10)
self.mask = GaussianBlobMask(img_size).get_mask(randvar)
self.epwtpath = EPWTPath(self.mask).get_path()
self.ppath = ProbabilisticPath(self.mask).get_path()
if not os.path.isdir(f"{self.root}/images"):
os.mkdir(f'{self.root}/images')
self.epwtimg = ColoredPath(self.epwtpath, self.mask.shape).get_colored_path(cmap)
self.pimg = ColoredPath(self.ppath, self.mask.shape).get_colored_path(cmap)
#img=PIL.Image.frombytes(self.pimg)
#matplotlib.image.imsave(f"{self.root}/images/{name}_mask.png", self.mask, cmap='gray')
#matplotlib.image.imsave(f"{self.root}/images/{name}_epwt.png", self.epwtimg)
matplotlib.image.imsave(f"{self.root}/images/{name}_prob.png", self.pimg)
buffer = io.BytesIO()
#img.save(buffer, format='PNG')
buffer.write(self.pimg)
#buffer.seek(0)
return buffer
#img = get_random_image(img_size) #returns numpy array
#print(img)
#print(dir(img))
#img_out = Image.frombytes(img.tobytes())
#show_array(img) #shows the image
#img_out.save("rand_im.png")
##matplotlib.image.imsave("randimage.png", img)
#for ea in range(10):
# run_1(img_size=[256,256],key=ea)