びゅわ
import tkinter as tk
from tkinter import filedialog, Scale
from tkinter import ttk
from PIL import Image, ImageTk, ImageEnhance
import boto3
import numpy as np
from io import BytesIO
class ImageViewer:
def __init__(self, root):
self.root = root
self.root.title("S3 Image Viewer")
self.canvas = tk.Canvas(root, width=800, height=600)
self.canvas.pack()
self.frame = tk.Frame(root)
self.frame.pack(side=tk.BOTTOM)
self.open_button = tk.Button(self.frame, text="Open", command=self.open_image)
self.open_button.pack(side=tk.LEFT)
self.save_button = tk.Button(self.frame, text="Save", command=self.save_image)
self.save_button.pack(side=tk.LEFT)
self.mode_var = tk.StringVar(value="RGB")
self.rgb_radio = tk.Radiobutton(self.frame, text="RGB", variable=self.mode_var, value="RGB", command=self.update_image)
self.rgb_radio.pack(side=tk.LEFT)
self.grayscale_radio = tk.Radiobutton(self.frame, text="Grayscale", variable=self.mode_var, value="Grayscale", command=self.update_image)
self.grayscale_radio.pack(side=tk.LEFT)
self.r_scale = Scale(self.frame, from_=0, to=255, orient=tk.HORIZONTAL, label="R", command=self.update_image)
self.r_scale.pack(side=tk.LEFT)
self.g_scale = Scale(self.frame, from_=0, to=255, orient=tk.HORIZONTAL, label="G", command=self.update_image)
self.g_scale.pack(side=tk.LEFT)
self.b_scale = Scale(self.frame, from_=0, to=255, orient=tk.HORIZONTAL, label="B", command=self.update_image)
self.b_scale.pack(side=tk.LEFT)
self.image = None
self.display_image = None
self.s3 = boto3.client('s3')
self.bucket_name = 'your-bucket-name'
def open_image(self):
filename = filedialog.askopenfilename()
if filename:
with open(filename, 'rb') as f:
self.image = Image.open(f)
self.display_image = self.image
self.show_image()
def save_image(self):
if self.display_image:
output = BytesIO()
self.display_image.save(output, format='JPEG')
output.seek(0)
self.s3.upload_fileobj(output, self.bucket_name, 'saved_image.jpg')
print("Image saved to S3.")
def update_image(self, event=None):
if self.image:
if self.mode_var.get() == "Grayscale":
self.display_image = self.image.convert('L')
else:
r = self.r_scale.get()
g = self.g_scale.get()
b = self.b_scale.get()
rgb_image = np.array(self.image)
rgb_image = rgb_image * [r/255.0, g/255.0, b/255.0]
self.display_image = Image.fromarray(np.uint8(rgb_image))
self.show_image()
def show_image(self):
if self.display_image:
img_width, img_height = self.display_image.size
canvas_width, canvas_height = self.canvas.winfo_width(), self.canvas.winfo_height()
scale = min(canvas_width / img_width, canvas_height / img_height)
img_width, img_height = int(img_width * scale), int(img_height * scale)
self.display_image = self.display_image.resize((img_width, img_height), Image.ANTIALIAS)
self.tk_image = ImageTk.PhotoImage(self.display_image)
self.canvas.create_image(canvas_width//2, canvas_height//2, image=self.tk_image)
if __name__ == "__main__":
root = tk.Tk()
app = ImageViewer(root)
root.mainloop()