Skip to content

Commit 7a64446

Browse files
committed
Removed local image save; more efficient
Removed the local image save and instead took the bytes directly from the Pillow image into the OpenAI request.
1 parent 32c8c59 commit 7a64446

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

canvas_image.png

-20 KB
Loading

main.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from io import BytesIO
12
import tkinter as tk
2-
from PIL import Image, ImageDraw, ImageGrab, ImageFont, ImageTk
3+
from PIL import Image, ImageDraw, ImageFont
34
from openai import OpenAI
45
from tkinter import font as tkFont
5-
import pytesseract
66
import base64
77

88
class DrawingApp:
@@ -85,13 +85,12 @@ def redraw_all(self):
8585
self.canvas.create_line(coords, fill='white', width=5)
8686

8787
def calculate(self):
88-
self.image.save("canvas_image.png")
89-
90-
def encode_image(image_path):
91-
with open(image_path, "rb") as image_file:
92-
return base64.b64encode(image_file.read()).decode('utf-8')
88+
def encode_image_to_base64(image):
89+
buffered = BytesIO()
90+
image.save(buffered, format="PNG")
91+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
9392

94-
base64_image = encode_image("canvas_image.png")
93+
base64_image = encode_image_to_base64(self.image)
9594

9695
response = self.client.chat.completions.create(
9796
model="gpt-4o",
@@ -141,4 +140,4 @@ def draw_answer(self, answer):
141140
if __name__ == "__main__":
142141
root = tk.Tk()
143142
app = DrawingApp(root)
144-
root.mainloop()
143+
root.mainloop()

0 commit comments

Comments
 (0)