#ChatGPT as a general problem-solver

1 messages · Page 1 of 1 (latest)

reef tundra
#

I'm a general hobbyist and nerd and I often need small utilities like batch file converters, other specialty stuff that's task-specific. In this case, I used Chat to write a python script so I didn't have to install any terrible apps off download dot com or whatever. It worked the first time.

reef tundra
#

This script took a couple of tries because Chat kept forgetting to include the IO module, but it resizes an image until it's less than 50k, even works in batches.

#

import io
import os
import tkinter as tk
from tkinter import filedialog
from PIL import Image

def reduce_image_size(file_path, target_size_kb=50):
# Open the image
with Image.open(file_path) as img:
# Convert RGBA to RGB if necessary
if img.mode == 'RGBA':
img = img.convert('RGB')

    # Reduce dimensions until file size is under 50KB
    while True:
        img_buffer = io.BytesIO()
        img.save(img_buffer, format='PNG')
        img_buffer_size = img_buffer.tell() / 1024  # size in KB

        if img_buffer_size <= target_size_kb:
            break

        # Calculate new dimensions (reduce by 10%)
        new_width = int(img.width * 0.9)
        new_height = int(img.height * 0.9)
        img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)

    # Save the final resized image
    resized_file_path = os.path.splitext(file_path)[0] + "_50K.png"
    with open(resized_file_path, 'wb') as f_out:
        f_out.write(img_buffer.getvalue())

    print(f"Resized image saved as: {resized_file_path}")

def resize_images():
# Open file dialog to select images
root = tk.Tk()
root.withdraw() # Hide the main window
file_paths = filedialog.askopenfilenames()
if not file_paths:
return # No files were selected

for file_path in root.tk.splitlist(file_paths):
    try:
        reduce_image_size(file_path)
    except Exception as e:
        print(f"An error occurred with {file_path}: {e}")

Run the function

resize_images()

clear fossil
#

because you have py import tkinter as tk from tkinter import filedialog but i dont think the tkinter module exists under that name because it was imported under the name of tk

#

full code in a codebox ```py
import io
import os
import tkinter as tk
from tkinter import filedialog
from PIL import Image

def reduce_image_size(file_path, target_size_kb=50):
# Open the image
with Image.open(file_path) as img:
# Convert RGBA to RGB if necessary
if img.mode == 'RGBA':
img = img.convert('RGB')

    # Reduce dimensions until file size is under 50KB
    while True:
        img_buffer = io.BytesIO()
        img.save(img_buffer, format='PNG')
        img_buffer_size = img_buffer.tell() / 1024  # size in KB

        if img_buffer_size <= target_size_kb:
            break

        # Calculate new dimensions (reduce by 10%)
        new_width = int(img.width * 0.9)
        new_height = int(img.height * 0.9)
        img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)

    # Save the final resized image
    resized_file_path = os.path.splitext(file_path)[0] + "_50K.png"
    with open(resized_file_path, 'wb') as f_out:
        f_out.write(img_buffer.getvalue())

    print(f"Resized image saved as: {resized_file_path}")

def resize_images():
# Open file dialog to select images
root = tk.Tk()
root.withdraw() # Hide the main window
file_paths = filedialog.askopenfilenames()
if not file_paths:
return # No files were selected

for file_path in root.tk.splitlist(file_paths):
    try:
        reduce_image_size(file_path)
    except Exception as e:
        print(f"An error occurred with {file_path}: {e}")```
reef tundra