Create Methods.py
Browse files- Methods.py +66 -0
Methods.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
import uuid
|
7 |
+
|
8 |
+
# function to plot and save an AgentImage
|
9 |
+
def plot_and_save_agent_image(agent_image, save_path=None):
|
10 |
+
# Convert AgentImage to a raw PIL Image
|
11 |
+
pil_image = agent_image.to_raw()
|
12 |
+
|
13 |
+
# Plot the image using PIL's show method
|
14 |
+
pil_image.show()
|
15 |
+
|
16 |
+
# If save_path is provided, save the image
|
17 |
+
if save_path:
|
18 |
+
pil_image.save(save_path)
|
19 |
+
print(f"Image saved to {save_path}")
|
20 |
+
else:
|
21 |
+
print("No save path provided. Image not saved.")
|
22 |
+
|
23 |
+
|
24 |
+
def generate_prompts_for_object(object_name):
|
25 |
+
prompts = {
|
26 |
+
"past": f"Show an old version of a {object_name} from its early days.",
|
27 |
+
"present": f"Show a {object_name} with from present with current features/design/technology.",
|
28 |
+
"future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
|
29 |
+
}
|
30 |
+
return prompts
|
31 |
+
|
32 |
+
|
33 |
+
# Function to generate the car industry history
|
34 |
+
def generate_object_history(object_name):
|
35 |
+
images = []
|
36 |
+
|
37 |
+
# Get prompts for the object
|
38 |
+
prompts = generate_prompts_for_object(object_name)
|
39 |
+
|
40 |
+
# Generate sequential images and display them
|
41 |
+
for time_period, frame in prompts.items():
|
42 |
+
print(f"Generating {time_period} frame: {frame}")
|
43 |
+
result = agent.run(frame) # The tool generates the image
|
44 |
+
|
45 |
+
# Append the image to the list for GIF creation
|
46 |
+
images.append(result.to_raw()) # Ensure we're using raw image for GIF
|
47 |
+
|
48 |
+
# Save each image with the appropriate name (past, present, future)
|
49 |
+
image_filename = f"{object_name}_{time_period}.png"
|
50 |
+
plot_and_save_agent_image(result, save_path=image_filename)
|
51 |
+
|
52 |
+
|
53 |
+
# Create GIF from images
|
54 |
+
gif_path = f"{object_name}_evolution.gif"
|
55 |
+
images[0].save(
|
56 |
+
gif_path,
|
57 |
+
save_all=True,
|
58 |
+
append_images=images[1:],
|
59 |
+
duration=1000, # Duration in milliseconds for each frame
|
60 |
+
loop=0 # Infinite loop
|
61 |
+
)
|
62 |
+
|
63 |
+
# Return images and GIF path
|
64 |
+
return images, gif_path
|
65 |
+
|
66 |
+
|