-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (135 loc) · 4.96 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#CODIO SOLUTION BEGIN
# libraries
import os
import openai
from PIL import Image,ImageOps,ImageFilter
from io import BytesIO
import requests
# Set API key and prompt
openai.api_key = os.getenv('OPENAI_KEY')
def main():
while True:
print("\n1. Generate an image with DALL-E")
print("2. Create variations of an image")
print("3. Resize an image")
print("4. To add a filter to your image")
#print("5. view current directory")
print("0. Exit")
option = input("Choose an option: ")
if option == "1":
prompt = input("Enter the prompt for DALL-E: ")
# Generate the image with DALL-E
generate_image(prompt)
elif option == "2":
image_path = input("Enter the path of the image: ")
# Open the image file
create_variations(image_path)
elif option=="3":
image_path = input("Enter the path of the image you want resized: ")
# run resize function
re_size(image_path)
elif option=="4":
image_path = input("Enter the path of the image you want to add filter to: ")
# run filter function
filters(image_path)
elif option=="5":
# run files list function
fileList()
elif option == "0":
print("Exiting the program...")
break
else:
print("Invalid option. Please enter a valid number.")
def generate_image(prompt1):
# Generate the image with DALL-E
print(f"Generating an image for the prompt: {prompt1}")
# Call DALL-E API here
response = openai.Image.create(
prompt=prompt1,
n=1,
size="256x256")
image_url = response['data'][0]['url']
img_data = requests.get(image_url).content
with open('image_created.png', 'wb') as handler:
handler.write(img_data)
print("Your image was generated under the `image_created.png` file name.")
def create_variations(img):
# Create variations of an image using PIL
print("Creating variations of the image...")
# Call DALL-E API here
response = openai.Image.create_variation(
image=open(img, "rb"),
n=1,
size="256x256")
image_url = response['data'][0]['url']
img_data = requests.get(image_url).content
with open('image_var.png', 'wb') as handler:
handler.write(img_data)
print("Your image was generated under the `image_var.png` file name.")
def re_size(image_path):
## Open an image file
with Image.open(image_path) as img:
# Ask the user to choose a dimension
print("Choose a dimension for resizing the image:")
print("1. 256x256")
print("2. 512x512")
print("3. 1024x1024")
option = input("Enter your option: ")
if option == "1":
target_size = (256, 256)
elif option == "2":
target_size = (512, 512)
elif option == "3":
target_size = (1024, 1024)
else:
print("Invalid option. Using default size 256x256.")
target_size = (256, 256)
# Resize the image
resized_img = img.resize(target_size)
# Save the resized image
resized_img.save('resized_image.png')
print("Your given image was resized and saved under `resized_image.png` ")
def filters(image_path):
# Open an image file
with Image.open(image_path) as img:
# Ask the user to choose a filter
print("Choose a filter to apply on the image:")
print("1. Contour")
print("2. Edge Enhance")
print("3. Find Edges")
print("4. Gaussian Blur")
option = input("Enter your option: ")
if option == "1":
filtered_img = img.filter(ImageFilter.CONTOUR)
elif option == "2":
filtered_img = img.filter(ImageFilter.EDGE_ENHANCE)
elif option == "3":
filtered_img = img.filter(ImageFilter.FIND_EDGES)
elif option == "4":
# Apply Gaussian blur
blur_radius = input("Enter the radius for the Gaussian blur: ")
try:
blur_radius = int(blur_radius)
except ValueError:
print("Invalid radius. Applying Gaussian blur with default radius 5.")
blur_radius = 5
filtered_img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
else:
print("Invalid option. Applying Contour filter by default.")
filtered_img = img.filter(ImageFilter.CONTOUR)
# Save the filtered image
filtered_img.save('filtered_image.png')
print("Filter applied successfully, 'filtered_image.png' created.")
def fileList():
# Get the current directory
current_directory = os.getcwd()
# Get all files in the current directory
files = os.listdir(current_directory)
# Print all files (not directories)
print("The current files in your directory: ")
for file in files:
if os.path.isfile(file):
print(file)
if __name__ == "__main__":
main()
#CODIO SOLUTION END