Interactive program to resize images generated using ChatGPT
Created:
The inspiration for this exercise came from this post where I noted down the need for a “workflow” (I was thinking Alfred workflow) to process my photos for use in this website (see example), but turns out writing out the prompt for ChatGPT resulted in a much simpler solution.
Prompt
write a interactive command line program that asks the user for 1.
input image file name ending in .heic
2. output file name
(assume .png if no extension nothing is provided) 3. output resolution
in the format 800
, where 800 is the width
resize the image file with max width provided in input 3 above, while
storing it in the appropriate file name and extension and store it in
$HOME/files.btbytes.com/pictures/YYYY/MM
where
YYYY
and MM
correspond to today’s date.
ChatGPT output
Certainly. Here’s a Python script to achieve this using the
PIL
(Pillow) library:
import os
from datetime import datetime
from PIL import Image
def get_user_input():
= input("Enter input image filename ending in .heic: ")
input_file = input("Enter output file name (or leave blank for .png): ")
output_file = int(input("Enter output resolution width (e.g., 800): "))
width return input_file, output_file, width
def process_image(input_file, output_file, width):
# Open and resize the image
with Image.open(input_file) as img:
= img.height / img.width
aspect_ratio = int(aspect_ratio * width)
new_height = img.resize((width, new_height))
img
# Determine output path
= datetime.today()
today = today.strftime('%Y')
year = today.strftime('%m')
month = os.path.expanduser(f'$HOME/files.btbytes.com/pictures/{year}/{month}')
output_dir
if not output_file.endswith(('.png', '.jpg', '.jpeg')):
+= '.png'
output_file
= os.path.join(output_dir, output_file)
output_path
# Create directory if doesn't exist
=True)
os.makedirs(output_dir, exist_ok
# Save the image
img.save(output_path)print(f"Image saved to {output_path}")
def main():
= get_user_input()
input_file, output_file, width
process_image(input_file, output_file, width)
if __name__ == '__main__':
main()
Before running the script, ensure you’ve installed the required packages:
pip install Pillow
Run the script, provide the inputs as prompted, and it will save the resized image in the specified directory structure.
Modifications
I saved the modified program as
~/btbytes/bin/proc_image.py
.
import os
from datetime import datetime
from PIL import Image
def get_user_input():
= input("Enter input image filename ending in .heic: ")
input_file = input("Enter output file name (or leave blank for .png): ")
output_file = input("Figure Caption")
caption = int(input("Enter output resolution width (e.g., 800): "))
width return input_file, output_file, caption, width
def process_image(input_file, output_file, caption, width):
# Open and resize the image
with Image.open(input_file) as img:
= img.height / img.width
aspect_ratio = int(aspect_ratio * width)
new_height = img.resize((width, new_height))
img
# Determine output path
= datetime.today()
today = today.strftime("%Y")
year = today.strftime("%m")
month = os.path.expanduser(f"~/files.btbytes.com/pictures/{year}/{month}")
output_dir
if not output_file.endswith((".png", ".jpg", ".jpeg")):
+= ".png"
output_file
= os.path.join(output_dir, output_file)
output_path
# Create directory if doesn't exist
=True)
os.makedirs(output_dir, exist_ok
# Save the image
img.save(output_path)print(f"Image {output_file} saved to {output_path}")
= output_path.replace(os.path.expanduser("~"), "")
uri print(
f"""
<figure>
<img src="https://{uri}" alt="{caption}">
<figcaption>{caption}</figcaption>
</figure>"""
)
def main():
= get_user_input()
input_file, output_file, caption, width
process_image(input_file, output_file, caption, width)
if __name__ == "__main__":
main()
I Copy and paste the figure
code spit out by the above
script into my markdown file.
Summary
It is possible to generate a near complete program to write automation tasks using ChatGPT. The more time you take to write a complete and detailed spec, the more likely you are to get “scripts” to automate a few workflows.
In the above case, it even used a library (Pillow
) that
I know, but not overly familiar.
When I’m using ChatGPT, I (and you) spend more time thinking and writing what the program should do and later on verifying that it indeed fits your earlier specification.