Crop and display an image using tkinter (python)



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding


Release date:2024/9/13         

In Japanese


■Explanation of the Crop function
This article explains how to crop and display an image using tkinter. The function used is crop. Note that a method for simply displaying an image is explained here.

■An example of the crop function
The "crop" part below corresponds to the process of cutting out an image.

import tkinter as tk
from PIL import Image, ImageTk

app = tk.Tk()
img = Image.open('test.jpg')

crop_img = img.crop((100, 100, 300, 300)) # Crop the image to the range 100,100~300,300

tk_img = ImageTk.PhotoImage(crop_img)
img_width, img_height = img.size

canvas = tk.Canvas(app, width=img_width, height=img_height)        # Creating an image display area
canvas.pack()
canvas.create_image(0, 0 , anchor = tk.NW, image=tk_img)        # Image display

app.mainloop()


The results are below, with the original image on the left.










List of related articles



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding