简体   繁体   中英

How to rotate an image on canvas by 90 degree

There is a loaded image on canvas and I have to rotate it by 90 degree.But when I rotate it it is not fitting on canvas and background colour is getting black.I want the image to fit on canvas properly.

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


class SimpleApp(tk.Frame):
    def __init__(self, master, filename, **kwargs):
        self.master = master
        self.filename = filename
        tk.Frame.__init__(self, master)
        self.canvas = tk.Canvas(master, width=1500, height=1200,bg='white')
        self.canvas.grid(row=0,column=0,sticky=(N,W,E,S))
        self.image = Image.open("C:\\Users\\Sripad Jevala\\Desktop\\RESIZE\\Cat1_1.jpg")
        angle = 90
        self.tkimage = ImageTk.PhotoImage(self.image.rotate(angle))
        canvas_obj = self.canvas.create_image(550, 350, image=self.tkimage)

root = tk.Tk()
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
app = SimpleApp(root, "C:\\Users\\Sripad Jevala\\Desktop\\RESIZE\\Cat1_1.jpg")
root.mainloop()

You need to set expand=1 :

self.tkimage = ImageTk.PhotoImage(self.image.rotate(angle, expand=1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM