def main(): analysis = Analysis(TUNE_RESULTS_FOLDER) print("Best hyperparameter {}".format( analysis.get_best_config(metric="mean_reward", mode="max"))) best_model_path = analysis.get_best_logdir(metric="mean_reward", mode="max") print( "Best model found in {}, start rendering .gif".format(best_model_path)) best_model = SomeModelToTrain({ 'learning_rate': 0.1, 'batch_size': 1, 'target_update': 1 }) checkpoint_path = f'{best_model_path}/checkpoint_{MAX_TRAINING_ITERATION}' best_model.load(checkpoint_path + '/' + MODEL_FILENAME) # we got this part from https://stable-baselines.readthedocs.io/en/master/guide/examples.html and modified it env = gym.make('LunarLander-v2') images = [] state = env.reset() for j in range(210): action = best_model.agent.act(state) img = env.render(mode='rgb_array') images.append(img) state, reward, done, _ = env.step(action) if done: break env.close() imageio.mimsave( 'best_model.gif', [np.array(img) for i, img in enumerate(images) if i % 2 == 0], fps=29) optimize('best_model.gif')
async def glitchtgi(client, message): pablo = await edit_or_reply(message, "`Processing...`") if not message.reply_to_message: await pablo.edit("Please Reply To Image For Glitching") return photolove = await convert_to_image(message, client) await pablo.edit("`Gli, Glitchiiingggg.....`") pathsn = f"Glitched.gif" glitch_imgs = glitcher.glitch_image(photolove, 2, gif=True, color_offset=True) glitch_imgs[0].save( pathsn, format="GIF", append_images=glitch_imgs[1:], save_all=True, duration=DURATION, loop=LOOP, ) await pablo.edit("`Optimizing Now!`") optimize(pathsn) await pablo.edit("`Starting Upload!`") if message.reply_to_message: await client.send_animation( message.chat.id, pathsn, reply_to_message_id=message.reply_to_message.message_id, ) else: await client.send_animation(message.chat.id, pathsn) if os.path.exists(pathsn): os.remove(pathsn) await pablo.delete()
def optimize_size(file): print("Optimizing size...") before_size = getsize(file) optimize(file) after_size = getsize(file) print("Original Size: {}. Optimized to {}.".format(before_size, after_size))
def gifware(file: Path, data: dict, template: dict, save: str = 'test.gif') -> None: '''Modify the contents of a GIF to regional languages. Input: file: GIF input file data: tag => language mapping template: Instructions on where to position the text save: Path to save the modified GIF ''' gif = io.get_reader(file) frames = [] for (i, frame) in enumerate(gif): frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB) if str(i) in template: frame = draw_caption(frame, data, captions=template[str(i)], lang=template['meta']['lang']) frames.append(frame) io.mimwrite(save, frames, duration=eval(template['meta']['duration'])) optimize(str(save))
def package_2_gif(filenames, target_file, fps=12): images = [] for filename in filenames: images.append(imageio.imread(filename)) imageio.mimsave(target_file, images, fps=fps) optimize(target_file, options=["--lossy"], colors=64) optimize_gif(target_file)
def _make_meta_gifs(): output_dir = "../output/gifs" if os.path.isdir(output_dir): shutil.rmtree(output_dir) os.makedirs(output_dir) n_versions = _count_versions() for version in range(n_versions): directory = f"../output/simulations/v{version}/plots" images = [] for day in range(N_DAYS + 1): filename = os.path.join(directory, f'iteration0_day{day}.png') # Crop image, original is 1100 x 800 left = 130 right = 1000 top = 90 bottom = 720 im = Image.open(filename) im_cropped = im.crop((left, top, right, bottom)) images.append(im_cropped) outfile = os.path.join(output_dir, f"v{version}.gif") imageio.mimsave(outfile, images) # Optimize to reduce storage pygifsicle.optimize(outfile)
def on_any_event(self, event): print(event.event_type, event.src_path) if (event.event_type in EVENTS): folder_or_file = event.src_path poutput = Path(BASE_PATH).joinpath(OUTPUT_FOLDER_NAME) pfolder_or_file = Path(folder_or_file) if pfolder_or_file.is_file(): self.wait_for_file(pfolder_or_file) if self.is_video(str(pfolder_or_file)): if self.get_file_size( str(pfolder_or_file)) <= self.mb_to_bytes( VIDEO_SIZE_LIMIT_MB): file_name = pfolder_or_file.stem #get file name of file tht triggered the event output_path_full = poutput.joinpath( file_name + '.gif') # create full path for ouput output_path_full = str(output_path_full) clip = (mpy.VideoFileClip(folder_or_file)).resize( width=1280) clip.write_gif(output_path_full) clip.close() optimize(output_path_full) else: print( f"File size greater than {VIDEO_SIZE_LIMIT_MB} mb") # print("") else: print("Not a video")
def anime(self, target, fps): # Initialize the environment obs, ended = self.envInterface.reset(target), False path = pathlib.Path.joinpath(self.out_path, str(hash(self.agent.agent_name)) + ".gif").as_posix() i = 1 while pathlib.Path(path).exists(): path = pathlib.Path.joinpath( self.out_path, str(hash(self.agent.agent_name + str(i))) + ".gif").as_posix() i += 1 writer = imageio.get_writer(path, mode='I', fps=fps) print("output path: " + path) # Add the white canvas img = self.envInterface.render() writer.append_data(img) # Play until the sequence has ended while not ended: action = self.agent.decision(obs) obs, score, ended, info = self.envInterface.step(action) img = self.envInterface.render() writer.append_data(img) optimize(path)
def generateGif(gif_name, frames, duration=0.35): print(len(frames), "images were written in this GIF.") imgs = [] for image_name in frames: imgs.append(imageio.imread(image_name)) imageio.mimsave(gif_name, imgs, 'GIF', duration=duration) optimize(gif_name)
def save(self, path, optimize: bool = True, duration: float = None, fps: int = 25) -> None: """ :param duration: s duration between frames """ if len(self.pierogis) > 1: ims = [np.asarray(pierogi.image) for pierogi in self.pierogis] if duration is not None: imageio.mimwrite(path, ims=ims, duration=duration, fps=fps) else: imageio.mimwrite(path, ims=ims, fps=fps) if optimize and os.path.splitext(path)[1] == ".gif": try: import pygifsicle pygifsicle.optimize(path) except FileNotFoundError as err: print(err) elif len(self.pierogis) == 1: self.pierogis[0].save(path) else: raise Exception("Dish has no pierogis")
def visualize(env, ac, cr=None, gif=False): done = False obs = env.reset() imgs, visited_pos, visited_vel = [], [], [], acts, means, stds, vals = [], [], [], [] if gif: img = env.render('rgb_array') while not done: if gif: imgs.append(img) visited_pos.append(obs[0]) visited_vel.append(obs[1]) act, _ = ac.get_action(obs) if cr: val = cr.get_action(obs, critic=True) vals.append(val.squeeze().detach().item()) acts.append(act[0]) means.append(ac.action_mean.detach().item()) stds.append(ac.action_std.detach().item()) obs, rew, done, _ = env.step(act) if gif: img = env.render('rgb_array') if gif: imageio.mimsave( '/tmp/current_gif.gif', [np.array(img) for i, img in enumerate(imgs) if i % 2 == 0], fps=29) optimize('/tmp/current_gif.gif') return visited_pos, visited_vel, acts, means, stds, vals
def run_episodes_and_create_video(policy, eval_tf_env, eval_py_env, path): num_episodes = 1 frames = [] fps = 16 for episode in range(num_episodes): time_step = eval_tf_env.reset() frames.append(eval_py_env.render()) step = 1 while not time_step.is_last() and step < 1000: action_step = policy.action(time_step) time_step = eval_tf_env.step(action_step.action) frames.append(eval_py_env.render()) print(f"\rperforming {step} step of Ep.{episode}", end="") step += 1 # 몇 초 간 유예합니다 for _ in range(fps * 3): frames.append(frames[-1]) with open(path, "wb") as gif_file: imageio.mimsave(gif_file, frames, format="gif", fps=fps) optimize(path) with open(path, "rb") as gif_file: IPython.display.display(embed_gif(gif_file.read()))
def compile_animation(self, iteration, delete_files=False, start_iteration=0, duration=0.1): filenames = [ self.get_tmp_animation_filename(i) for i in range(start_iteration, iteration) ] images = [] for filename in filenames: try: image = imageio.imread(filename) except FileNotFoundError: # not every iteration has a plot continue images.append(image) if filename == filenames[-1]: for i in range(10): images.append(imageio.imread(filename)) if delete_files: os.remove(filename) # Save the gif in a new animations sub-folder os.makedirs(self.animation_save_dir, exist_ok=True) animation_filename = ( self.animation_save_dir + self.get_animation_filename() ) imageio.mimsave(animation_filename, images, duration=duration) optimize(animation_filename) # compress gif file size
def pngs_to_gif(): """ Creates gif for the von karman vortex street given the saved pngs """ # Create the frames frames = [] imgs = glob.glob( r"./figures/von_karman_vortex_shedding/all_png_parallel/*.png") regex = re.compile(r'\d+') numbers = [int(x) for img in imgs for x in regex.findall(img)] img_dict = {img: number for img, number in zip(imgs, numbers)} ordered_img_dict = collections.OrderedDict( sorted(img_dict.items(), key=lambda item: item[1])) for img, _ in ordered_img_dict.items(): new_frame = Image.open(img) frames.append(new_frame) # Save into a GIF file that loops forever frames[0].save('./figures/von_karman_vortex_shedding/png_to_gif.gif', format='GIF', append_images=frames[1:], save_all=True, duration=0.5, loop=0) optimize(r'./figures/von_karman_vortex_shedding/png_to_gif.gif')
async def glitch(event): if event.fwd_from: return sed = await event.get_reply_message() okbruh = await event.edit("`Gli, Glitchiiingggg.....`") photolove = await convert_to_image(event, friday) pathsn = f"./starkgangz/@fridayot.gif" glitch_imgs = glitcher.glitch_image(photolove, 2, gif=True, color_offset=True) glitch_imgs[0].save( pathsn, format="GIF", append_images=glitch_imgs[1:], save_all=True, duration=DURATION, loop=LOOP, ) c_time = time.time() optimize(pathsn) stark_m = await uf( file_name="*****@*****.**", client=borg, file=open(pathsn, 'rb'), progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress( d, t, event, c_time, "Uploading..", pathsn ) ), ) await borg.send_file(event.chat_id, stark_m, caption="Powered By @FridayOT") await okbruh.delete() for starky in (pathsn, photolove): if starky and os.path.exists(starky): os.remove(starky)
def test_optimize_pathlib(): here = Path(__file__).parent.resolve() source = here / "big.gif" destination = here / "small.gif" optimize(source, destination) optimize(destination) os.remove(destination)
def grid_gif(self, gif_fn, img_tnsr, idx, reverse = True, up_fctr = 3, duration = 0.1, nrow = 4): transform = torchvision.transforms.Compose([torchvision.transforms.ToPILImage(mode=None), torchvision.transforms.Resize(img_tnsr[0].size(-1) * up_fctr, 3), torchvision.transforms.ToTensor()]) img_tnsr = img_tnsr.to(self.device) if reverse: path_parm = np.r_[self.__transition, self.__transition[::-1]] else: path_parm = self.__transition with imageio.get_writer(gif_fn, mode='I', duration=duration) as writer: for i in range(path_parm.size): path_enc = torch.empty(len(idx), self.__encoder.num_dim, dtype = torch.float32, device = self.device) for j in trange(len(idx), miniters = 1, leave = False, ncols = 100, desc = "{:3d}/{:3d}".format(i + 1, path_parm.size), position=0): vtx = img_tnsr[idx[j]] vtx_enc = self.__encoder(vtx) path_enc[j] = vtx_enc[0] * path_parm[i] + (1 - path_parm[i]) * vtx_enc[1] path_dc = self.__decoder(path_enc) dc_up = torch.empty(path_dc.size(0), path_dc.size(1), path_dc.size(2) * up_fctr, path_dc.size(3) * up_fctr, dtype = torch.float, device = self.device) for k in range(path_dc.size(0)): dc_up[k] = transform(path_dc[k]) img_grid = torchvision.utils.make_grid(dc_up.detach(), padding = 8, nrow = 4)#.permute(1, 2, 0) writer.append_data(np.array(torchvision.transforms.ToPILImage(mode=None)(img_grid))) optimize(gif_fn)
def _optimize_gif(self, gif_name): # pip install pygifsicle from pygifsicle import optimize if self.verbose: print("Optimize gif") optimize(gif_name, colors=100)
def game_2d(N, C, r=0.3, game_length=30, distance: str = 'L2', kernel_size: int = 3, create_gif=False, device="cpu"): """ Start an iterative moving process. Args: N: Size of game board C: Number of different populations game_length: Number of iterations r: Tolerance value. If less than r% of neighbours are of the same class, cell whants to move. distance: One of {`L2`, `L1`}. kernel_size: Size of neighbourhood to look at. Returns: move_hist: Number of cells that moved at each iteration """ assert r <= 1, "Wrong r value! 0 <= r <= 1" game_map = init_map_2d(N, C).to(device) name = f'schelling2d_size-{N}_C-{C}_dist-{distance}_ks-{kernel_size}_neigh-{int(r*8)}' fname = 'imgs/schelling2d_tmp.png' move_hist = [] images = [] for i in tqdm(range(game_length), desc=f'Number of neighbours={int(r*8)}', leave=False, disable=not create_gif): # Return torch.tensor game_map, moved = game_step_2d(game_map, r, distance='L2', kernel_size=3) if create_gif: game_map_2d = compress_2d(game_map) plot_2d(game_map_2d, C=C, r=r, i=i, save_image=True, name=fname, figsize=(14, 11)) images.append(imageio.imread(fname)) # print(images[-1].shape) os.remove(fname) move_hist.append(moved) if create_gif: fname = f'imgs/{name}.gif' imageio.mimsave(fname, images, fps=10) pygifsicle.optimize(fname) return move_hist
def mapas_serie_letalidad(serie_vulnerabilidad, gif=True, lapso_dias=1): ''' Recibe el DataFrame con la vulnerabilidad calculada para un rango de fechas y genera los mapas de cada fecha. ''' dir_salida = 'resultados/mapas/letalidad' if not os.path.exists(dir_salida): os.mkdir(dir_salida) serie_vulnerabilidad = serie_vulnerabilidad[[ 'dia_ajuste', 'geometry', 'tasa_covid_letal', 'CLAVE_MUNICIPIO_RES' ]].copy() serie_vulnerabilidad = serie_vulnerabilidad.drop_duplicates() inicio = serie_vulnerabilidad.dia_ajuste.min() fin = serie_vulnerabilidad.dia_ajuste.max() imagenes_mapas = [] lista_mapas = [] for fecha in pd.date_range(inicio, fin, freq=f'{lapso_dias}D'): datos_mapa = serie_vulnerabilidad[( serie_vulnerabilidad.dia_ajuste == fecha)] datos_mapa = datos_mapa[datos_mapa.tasa_covid_letal.notna()] fig = datos_mapa.plot(column='tasa_covid_letal', cmap='YlOrRd', scheme="equalinterval", figsize=(10, 10), legend=True, k=10, edgecolor="face") # remove axis of chart fig.axis('off') # add a title fig.set_title('Letalidad COVID-19 Municipios de México', \ fontdict={'fontsize': '25', 'fontweight' : '3'}) nombre_mapa = fecha.strftime("%y%m%d") + '_letalidad.jpg' ruta_archivo = os.path.join(dir_salida, nombre_mapa) lista_mapas.append(ruta_archivo) # position the annotation to the bottom left fig.annotate(fecha.strftime("%d/%m"), xy=(0.1, .225), xycoords='figure fraction', horizontalalignment='left', verticalalignment='top', fontsize=35) chart = fig.get_figure() chart.savefig(ruta_archivo, dpi=300) plt.close(chart) imagenes_mapas.append(imageio.imread(ruta_archivo)) inicio_str = inicio.strftime('%m%d') fin_str = fin.strftime('%m%d') imageio.mimsave( f'letalidad_covid_{inicio_str}_{fin_str}_{f"{lapso_dias}D"}.gif', imagenes_mapas, duration=1) optimize(f'letalidad_covid_{inicio_str}_{fin_str}_{f"{lapso_dias}D"}.gif') return lista_mapas
def main(): with imageio.get_writer(sys.argv[1], mode='I') as writer: for filename in sys.argv[2:]: image = imageio.imread(filename) writer.append_data(image) optimize(sys.argv[1])
def save_images(self, save_uri): self.image_append() imageio.mimsave(save_uri, self.images, 'GIF', duration=self.DUR) print("GIF export complete!") optimize(save_uri) print("GIF optimize complete!") gifsicle(sources=[save_uri], optimize=False, options=['-O3']) print("GIF deep optimize complete!")
def pl_timelapse(): image_list = [] for file_name in images: stats = os.stat(file_name) if stats.st_size > 100000 and str(file_name).__contains__('PL'): image_list.append(imageio.imread(file_name)) imageio.mimwrite(POINT_DIR + '/pl_timelapse.gif', image_list, fps=4) optimize(POINT_DIR + '/pl_timelapse.gif')
def output_gif(name, data, durations, _): # another still last frame to help twitter's broken gif playback data.append(data[-1]) durations.append(3) print('saving...') gif_path = output_path / (name + '.gif') imageio.mimsave(gif_path, data, duration=durations) print('shrinking...') optimize(str(gif_path))
def make_gif_from_string(move_string): filenames = make_images_from_string(move_string) with imageio.get_writer(f'output/{move_string}.gif', mode='I', fps=3) as writer: for filename in filenames: image = imageio.imread(filename) writer.append_data(image, ) optimize(f'output/{move_string}.gif') for filename in filenames: remove(filename) return f'output/{move_string}.gif'
def make_gif(gifname, fnames, fps, do_delete=False): import imageio with imageio.get_writer(gifname, mode='I', fps=fps) as writer: for fname in fnames: writer.append_data(imageio.imread(fname)) from pygifsicle import optimize optimize(str(gifname)) if do_delete: for fname in fnames: os.remove(fname) return gifname
def gif(self): """ 合成gif """ materials = self.materials() log.info('合成GIF') self.img_background.save(self.gif_path, save_all=True, loop=True, append_images=materials, duration=250) log.info('合成完成') log.info('压缩GIF') optimize(self.gif_path) log.info('压缩完成')
def render(self): if self.noise_dim == 3: self.images = self._make_3d_gif() else: self.images = self._make_4d_gif() if not self.pipeline.is_empty(): print("Running pipeline") self.images = self.pipeline.run(self.images) self._to_gif() if self.compress: optimize(self.output_file)
def generate_gif(self, gif): # Local vars gif = gif.value frames = [] custom_image = self._set_image(gif) # Get the frames for the gif and sort from 1 to n frames_path = f"{self._working_directory}/Frames/{gif['frames_path']}" filenames = [ os.path.join(frames_path, f) for f in os.listdir(frames_path) if isfile(join(frames_path, f)) ] filenames.sort(key=natural_sort) # Verify that the number of points match the number of frames if len(filenames) != len(gif['points']): raise ValueError( f"The number of frames and files do not match! Files: {len(filenames)}, points: " f"{len(gif['points'])}") # Create each frame overlay_points = gif['points'] for n in range(len(filenames)): # Check if None if overlay_points[n]: background = Image.open(filenames[n]).convert('RGBA') background.paste( custom_image, translate_center(overlay_points[n], custom_image), custom_image) frames.append(background) else: frames.append(Image.open(filenames[n]).convert('RGBA')) # Generate the gif file = tempfile.NamedTemporaryFile(delete=False, suffix='.gif') frames[0].save(file.name, format='GIF', append_images=frames[1:], save_all=True, duration=1000 / gif['fps'], loop=0) # Optimize the gif using gifsicle to reduce file size try: optimize(file.name) except FileNotFoundError: warning( 'Unable to optimize the gif. Please make sure gifsicle is installed.\nContinuing without ' 'optimization...\n') return file
def on_created(event): if event.src_path.endswith(".mov"): input_path = event.src_path output_path = os.path.splitext(input_path)[0] + ".gif" print("Converting:\t{}\nTo:\t\t{}\n".format(input_path, output_path)) mov_to_gif(input_path, output_path) print("\n\nOptimizing gif...") optimize(output_path) print("\nDone.") subprocess.call(["open", "-R", output_path])