Created
May 1, 2025 21:28
-
-
Save daltonclaybrook/691c7b0d5137fb753c7f1463516f1a52 to your computer and use it in GitHub Desktop.
Create recursive replicas of an image with ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import os | |
from openai import OpenAI | |
from dotenv import load_dotenv | |
load_dotenv() | |
count = 50 | |
start_duration = 1 | |
mid_duration = 0.2 | |
def main(): | |
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY')) | |
source_path = './images/source.png' | |
prompt = 'Create an exact replica of this image. Don\'t change a thing.' | |
try: | |
for idx in range(count): | |
image_path = source_path if idx == 0 else f'./images/image_{idx}.png' | |
print(f'Creating image {idx + 1} from path: {image_path}') | |
result = client.images.edit( | |
model='gpt-image-1', | |
image=open(image_path, 'rb'), | |
prompt=prompt, | |
quality='medium', | |
size='1024x1024' | |
) | |
image_base64 = result.data[0].b64_json | |
image_bytes = base64.b64decode(image_base64) | |
output_path = os.path.join('images', f'image_{idx + 1}.png') | |
with open(output_path, 'wb') as f: | |
f.write(image_bytes) | |
print('Creating movie file...') | |
with open('images/files.txt', 'w') as f: | |
f.write(f"file 'source.png'\nduration {start_duration}\n") | |
for i in range(1, count): | |
f.write(f"file 'image_{i}.png'\nduration {mid_duration}\n") | |
f.write(f"file 'image_{count}.png'\nduration {start_duration}\n") | |
# Write last file again to ensure final duration is respected | |
f.write(f"file 'image_{count}.png'\n") | |
os.system('ffmpeg -y -f concat -safe 0 -i images/files.txt -vsync vfr -pix_fmt yuv420p images/output.mp4') | |
os.remove('images/files.txt') | |
print('Done!') | |
except Exception as e: | |
print(f'An error occurred: {str(e)}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment