メモ2

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk

class SkyViewer:
   def __init__(self, root, image_path):
       self.root = root
       self.root.title("Sky Image Viewer")

       # Load image
       self.original_image = Image.open(image_path)
       self.display_image = self.original_image.copy()

       # Variables
       self.zoom_factor = tk.DoubleVar(value=1.0)
       self.angle_of_view = tk.DoubleVar(value=60.0)

       # Canvas
       self.canvas = tk.Canvas(root, bg="black", width=800, height=600)
       self.canvas.pack(expand=tk.YES, fill=tk.BOTH)
       self.update_display()

       # Controls
       ttk.Scale(root, from_=0.1, to=3.0, orient="horizontal", variable=self.zoom_factor, command=self.update_display, length=200, label="Zoom").pack(pady=10)
       ttk.Scale(root, from_=30.0, to=120.0, orient="horizontal", variable=self.angle_of_view, command=self.update_display, length=200, label="Angle of View").pack(pady=10)

       # Bind mouse events
       self.canvas.bind("<B1-Motion>", self.pan_image)

   def update_display(self, event=None):
       zoom = self.zoom_factor.get()
       angle_of_view = self.angle_of_view.get()

       # Resize and crop image based on zoom and angle of view
       width, height = self.original_image.size
       new_width = int(width * zoom)
       new_height = int(height * zoom)
       crop_box = self.calculate_crop_box(new_width, new_height, angle_of_view)

       resized_image = self.original_image.resize((new_width, new_height), Image.ANTIALIAS)
       cropped_image = resized_image.crop(crop_box)

       # Convert to PhotoImage and update canvas
       self.display_image = ImageTk.PhotoImage(cropped_image)
       self.canvas.config(width=new_width, height=new_height)
       self.canvas.create_image(0, 0, anchor=tk.NW, image=self.display_image)

   def calculate_crop_box(self, new_width, new_height, angle_of_view):
       zoom_factor = self.zoom_factor.get()
       center_x = new_width / 2
       center_y = new_height / 2

       horizontal_fov = angle_of_view
       vertical_fov = angle_of_view * (new_height / new_width)

       crop_width = int(self.original_image.width * (horizontal_fov / 360.0))
       crop_height = int(self.original_image.height * (vertical_fov / 180.0))

       crop_box = (
           center_x - crop_width / 2,
           center_y - crop_height / 2,
           center_x + crop_width / 2,
           center_y + crop_height / 2
       )

       return crop_box

   def pan_image(self, event):
       # Pan image based on mouse drag
       dx = event.x - self.start_x
       dy = event.y - self.start_y
       self.canvas.scan_dragto(-dx, -dy, gain=1)
       self.start_x = event.x
       self.start_y = event.y

   def run(self):
       self.root.mainloop()

if __name__ == "__main__":
   root = tk.Tk()
   sky_viewer = SkyViewer(root, "path/to/your/sky_image.jpg")
   sky_viewer.run()

この記事が気に入ったらサポートをしてみませんか?