How to get access to gpt-4-vision-preview?

For fixing the forum post, ask an AI “format this messed up code”.

Or I ask an AI to keep your image encode function under four tiles, reducing 1133 to 793 prompt tokens.

!pip install pillow

from io import BytesIO
from PIL import Image

def encode_image(image_path):
    with Image.open(image_path) as img:
        width, height = img.size
        max_dim = max(width, height)
        if max_dim > 1024:
            scale_factor = 1024 / max_dim
            new_width = int(width * scale_factor)
            new_height = int(height * scale_factor)
            img = img.resize((new_width, new_height))
        
        buffered = BytesIO()
        img.save(buffered, format="PNG")
        img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
        return img_str
1 Like