VIOLIN / scripts /generate_metadata_Variation_4.py
Perkzi's picture
Switch to Parquet format and ignore raw data folder
5e1ef3a
import csv
import colorsys
import os
def adjust_lightness(hex_val, factor):
"""Adjust the brightness based on a given hex color; the factor can be a positive or negative ratio."""
hex_val = hex_val.lstrip('#')
r, g, b = tuple(int(hex_val[i:i+2], 16) for i in (0, 2, 4))
# Convert to HLS
h, l, s = colorsys.rgb_to_hls(r/255.0, g/255.0, b/255.0)
# Adjust brightness
l = max(0, min(1, l + factor))
r, g, b = colorsys.hls_to_rgb(h, l, s)
return "#{:02X}{:02X}{:02X}".format(int(round(r*255)), int(round(g*255)), int(round(b*255)))
def merge_prompt_with_colors(prompt_file, color_file, output_file, variation=1, color_level=1, range_factor=0.1):
# read prompt
with open(prompt_file, "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
prompts = list(reader)
# read color
with open(color_file, "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
colors = list(reader)
merged_rows = []
id_counter = 1
for prompt in prompts:
prompt_text = prompt["prompt"]
for color in colors:
hex_val = color.get("hex") or color.get("color") or ""
split_val = "test"
merged_rows.append({
"id": id_counter,
"prompt": prompt_text,
"color_target": hex_val, # midpoint of the range
"color_1": adjust_lightness(hex_val, -range_factor), # lower limit
"color_2": adjust_lightness(hex_val, +range_factor), # upper limit
"variation": variation,
"color_level": color_level,
"split": split_val,
"color_format": "Hex code"
})
id_counter += 1
output_dir = os.path.dirname(output_file)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
# write CSV
with open(output_file, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["id","prompt","color_target","color_1","color_2","variation","color_level","split","color_format"])
writer.writeheader()
writer.writerows(merged_rows)
print(f"Generation complete: {output_file}")
merge_prompt_with_colors("raw_config/Variation_4.csv", "raw_config/Color_Level_1.csv", "metadata/Variation_4_Color_1.csv", variation=4, color_level=1)
merge_prompt_with_colors("raw_config/Variation_4.csv", "raw_config/Color_Level_2.csv", "metadata/Variation_4_Color_2.csv", variation=4, color_level=2)
merge_prompt_with_colors("raw_config/Variation_4.csv", "raw_config/Color_Level_3.csv", "metadata/Variation_4_Color_3.csv", variation=4, color_level=3)