import base64 import json import random from PIL import Image, ImageDraw from PIL.PngImagePlugin import PngInfo """ This script will make a new card with a randomized scenario field for the planet, rather than letting the AI invent it all as it goes. It also marks the new PNG with some random colours. You'll need PIL. This script isn't really necessary for the card, just something I was fucking around with a few weeks ago as a proof of concept to see if I could make something better for the chat-specific generation settings in Agnai If it doesn't work, give up immediately """ # Set filenames here input_file = 'Alexandra.card.png' output_file = 'Alexandra.card.modScenario.png' def get_json_from_png(filename): with Image.open(filename) as im: im.load() encoded_character_info = im.info['chara'] human_readable = base64.b64decode(encoded_character_info).decode('utf-8') return json.loads(human_readable) def mark_png_corner_with_triangle(input_filename, output_filename): with Image.open(input_filename) as im: draw = ImageDraw.Draw(im) stripe_colour = (random.randint(120, 255), random.randint(0, 180), random.randint(0, 120)) triangle_colour = (random.randint(0, 180), random.randint(60, 255), random.randint(60, 255)) draw.polygon([(0, 0), (0, 210), (210, 0)], fill=stripe_colour) draw.polygon([(0, 0), (0, 170), (170, 0)], fill=triangle_colour) im.save(output_filename) def write_json_to_png(input_image_filename, character_data, output_filename): # Go from json to encoded string character_data_string = json.dumps(character_data) character_data_bytes = character_data_string.encode('utf-8') character_data_bytes_b64 = base64.b64encode(character_data_bytes) character_data_string_b64 = character_data_bytes_b64.decode('utf-8') metadata = PngInfo() metadata.add_text('chara', character_data_string_b64) # Put metadata and input image into a new file together with Image.open(input_image_filename) as im: im.load() im.save(output_filename, pnginfo=metadata) def create_random_scenario_string(): preamble = 'Alexandra is marooned on a desolate hostile planet after her science exploration ship crashed.' planets = ['The biome is a lush jungle teeming with life and danger.', 'The biome is a desert with scorchingly hot days and freezing nights.', 'The biome is a snowy mountain range with many crevasses.', 'The biome is at the foothills of an active volcano.', 'The biome is a planet ludicrously overgrown with fungi.'] night_skies = ['The night sky here has incredible dancing auroras.', 'The night sky here is an ominous dark red colour.', 'The night sky here has tens of thousands of visible bright stars.', 'The night sky here changes colour dozens of times through the night.', 'The night sky here is perfectly and oppressively pitch black.', 'The night sky here is beautifully lit up by swarms of glowing creatures.'] hooks = ['This planet secretly holds ancient pyramid megastructures used as highly advanced hospitals.', 'This planet secretly has had its core replaced with an enormous malevolent AI supercomputer.', 'This planet secretly has had its core replaced with an enormous benevolent AI supercomputer.', 'This planet is dotted with mysterious cylindrical monoliths of unknown purpose.', 'This planet is secretly an enormous living creature that is in hiding from a threat.', 'This planet seems eerie somehow, haunted with some kind of technological ghosts of the past.'] custom_scenario = ' '.join([preamble, random.choice(planets), random.choice(night_skies), random.choice(hooks)]) return custom_scenario # Read character string and find the Scenario section manually for appending data = get_json_from_png(input_file) # If Scenario found, set the randomised Scenario and save new marked png if data['scenario']: mark_png_corner_with_triangle(input_file, output_file) data['scenario'] = create_random_scenario_string() write_json_to_png(output_file, data, output_file) else: print('Failed to find substring within character for appending.')