Skip to content

Instantly share code, notes, and snippets.

@pratu16x7
Last active May 8, 2023 19:07
Show Gist options
  • Save pratu16x7/2eeb12eb977230ffc43c8a2ef717df5b to your computer and use it in GitHub Desktop.
Save pratu16x7/2eeb12eb977230ffc43c8a2ef717df5b to your computer and use it in GitHub Desktop.
90% of this code is not by me
# 90% of this code is not be me
import csv
def read_csv(filename):
"""Reads a CSV file and returns a list of lists."""
table = []
with open(filename, 'r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
table.append(row)
return table
def read_csv_as_list_of_dicts(filename):
"""Reads a CSV file and returns a list of dictionaries."""
table = []
with open(filename, 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
table.append(row)
return table
def get_str_upto_char(s, c):
"""Returns the substring of s up to the first occurrence of c."""
return s[:s.find(c)]
def read_csv_as_feeels_data(filename):
"""Reads a CSV file and returns a list of dictionaries."""
table = {}
with open(filename, 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
actual_id = get_str_upto_char(row['id'], ';')
if actual_id not in table:
table[actual_id] = {}
table[actual_id][row['layer_name'][1:]] = row['figma_text']
return table
def turn_list_of_dicts_to_csv(list_of_dicts, filename):
"""Turns a list of dictionaries into a CSV file."""
with open(filename, 'w') as file:
fieldnames = list_of_dicts[0].keys()
csv_writer = csv.DictWriter(file, fieldnames=fieldnames)
csv_writer.writeheader()
for row in list_of_dicts:
csv_writer.writerow(row)
input = 'raw_figma_data.csv'
output = 'FEEELS.csv'
table = list(read_csv_as_feeels_data(input).values())
turn_list_of_dicts_to_csv(table, output)
# ===
import os
from PIL import Image
from argparse import ArgumentParser
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg"])
def load_img(filepath):
img = Image.open(filepath).convert('RGB')
return img
def resize_img(img, size):
return img.resize(size, Image.BILINEAR)
def resize_img_to_height(img, height):
w, h = img.size
ratio = height / h
return img.resize((int(w * ratio), height), Image.BILINEAR)
def save_as_png(img, filepath):
img.save(filepath, "PNG")
def process_img(filepath, size):
img = load_img(filepath)
img = resize_img(img, size)
save_as_png(img, filepath)
def iterate_over_paths_in_current_dir():
for filename in os.listdir(os.getcwd()):
if is_image_file(filename):
process_img(filename, (256, 256))
if __name__ == "__main__":
args = ArgumentParser()
args.add_argument("--height", type=int, default=200)
args = args.parse_args()
iterate_over_paths_in_current_dir()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment