Skip to content

Instantly share code, notes, and snippets.

View kerenskybr's full-sized avatar
🤖
___

Roger Monteiro kerenskybr

🤖
___
View GitHub Profile
@kerenskybr
kerenskybr / divide_chubks.py
Created September 22, 2020 01:18
Divide a list in chunks from given size
def divide_chunks(a_list, chunk_size):
"""Divide a list in chunks from given size
"""
for i in range(0, len(a_list), chunk_size):
yield a_list[i:i + chunk_size]
@kerenskybr
kerenskybr / concat_imgs.py
Last active September 19, 2020 13:48
Concat two images towards the height
from PIL import Image
def concat_images(im1, im2):
""" Concat two images towards the height
"""
dst_img = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height))
dst_img.paste(im1, (0, 0))
dst_img.paste(im2, (0, im1.height))
return dst_img
@kerenskybr
kerenskybr / four_lists_to_dict.py
Last active September 18, 2020 12:30
Creates dicts from list of lists
def merge_items(a_list):
"""Merge the 4 lists of items
and combine as values to keys
"""
#Same amount of lists of lists to receive a key
keys = ['key1','key2','key3','key4']
items = [list(i) for i in list(zip(*a_list))]
result = []
for i in items:
@kerenskybr
kerenskybr / gray_to_rgb.py
Created September 16, 2020 01:21
Function to convert the Grayscale data to RGB to match with an architecture
from keras import backend as K
def grayscale_to_rgb(images, channel_axis=-1):
'''Function to convert the Grayscale data
to RGB to match with an architecture
'''
images= K.expand_dims(images, axis=channel_axis)
tiling = [1] * 4 # 4 dimensions: B, H, W, C
tiling[channel_axis] *= 3
images= K.tile(images, tiling)
@kerenskybr
kerenskybr / remove_none.py
Last active May 11, 2023 14:38
Remove null keys and values from a nested structure of dicts, tuple and lists
def remove_none(json_file):
""" Remove null keys and values from
a nested structure of dicts, tuple and lists.
"""
if isinstance(json_file, (list, tuple, set)):
return type(json_file)(remove_none(x) for x in json_file if x is not None)
elif isinstance(json_file, dict):
return type(json_file)((remove_none(k), remove_none(v))
for k, v in json_file.items() if k is not None and v is not None)
else: