Python 如何更新使用image.open()打开的图像
我只是好奇,如果不用再次调用缩放功能,我将如何更新使用缩放功能打开的图像。另外,如果你知道任何更简单的方法来做我正在做的事情,这只是简单地试图使一个图像“缩放”与鼠标滚轮,那么我的耳朵是开放的lol。任何对代码的其他评论是感激的,谢谢 具体来说,这一部分就在这里:Python 如何更新使用image.open()打开的图像,python,python-3.x,tkinter,python-imaging-library,Python,Python 3.x,Tkinter,Python Imaging Library,我只是好奇,如果不用再次调用缩放功能,我将如何更新使用缩放功能打开的图像。另外,如果你知道任何更简单的方法来做我正在做的事情,这只是简单地试图使一个图像“缩放”与鼠标滚轮,那么我的耳朵是开放的lol。任何对代码的其他评论是感激的,谢谢 具体来说,这一部分就在这里: pictureVar = StringVar() pictureVar.set("a.png") Zoom(root, path=pictureVar.get()) pictureVar.set("b.png") 然后在此处使用
pictureVar = StringVar()
pictureVar.set("a.png")
Zoom(root, path=pictureVar.get())
pictureVar.set("b.png")
然后在此处使用Image.open()打开图片:
类缩放(帧):
“使用鼠标滚轮进行简单缩放”
定义初始化(自身、大型机、路径):
''初始化主框架''
帧。初始化(自,主=大型机)
#画布的垂直和水平滚动条
vbar=自动滚动条(self.master,orient='vertical')
hbar=AutoScrollbar(self.master,orient='horizontal')
vbar.grid(行=0,列=1,sticky='ns')
grid(行=1,列=0,sticky='we')
#开放图像
self.image=image.open(path)#请。你甚至没有提到你的代码转储的哪一部分是相关的…@Julien我很抱歉。。。我是新来的,我不常来这里,在编码方面也不是很有经验。如果有帮助,我会更新它。那么是什么阻止你创建一个update\u image(self,path)
方法来实现这一点呢?Zoom
不是一个函数,它是一个类。调用类时,它会创建一个实例,并通过调用类“\uuuu init\uuuu()
方法对其进行初始化。实例值将成为第一个参数,通常称为self
,它将自动传递给定义的类的所有其他方法。如果将图像
附加到画布
,则您可以在以后根据需要使用图像ID
更新画布
。
class Zoom(Frame):
''' Simple zoom with mouse wheel '''
def __init__(self, mainframe, path):
''' Initialize the main Frame '''
Frame.__init__(self, master=mainframe)
# Vertical and horizontal scrollbars for canvas
vbar = AutoScrollbar(self.master, orient='vertical')
hbar = AutoScrollbar(self.master, orient='horizontal')
vbar.grid(row=0, column=1, sticky='ns')
hbar.grid(row=1, column=0, sticky='we')
# Open image
self.image = Image.open(path) #<------------------------!!!!!!!!!!!
import csv
import os
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
class AutoScrollbar(Scrollbar):
''' A scrollbar that hides itself if it's not needed.
Works only if you use the grid geometry manager '''
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
self.grid_remove()
else:
self.grid()
Scrollbar.set(self, lo, hi)
class Zoom(Frame):
''' Simple zoom with mouse wheel '''
def __init__(self, mainframe, path):
''' Initialize the main Frame '''
Frame.__init__(self, master=mainframe)
# Vertical and horizontal scrollbars for canvas
vbar = AutoScrollbar(self.master, orient='vertical')
hbar = AutoScrollbar(self.master, orient='horizontal')
vbar.grid(row=0, column=1, sticky='ns')
hbar.grid(row=1, column=0, sticky='we')
# Open image
self.image = Image.open(path)
# Create canvas and put image on it
self.canvas = Canvas(self.master, highlightthickness=0,
xscrollcommand=hbar.set, yscrollcommand=vbar.set)
self.canvas.grid(row=0, column=0, sticky='nswe')
vbar.configure(command=self.canvas.yview) # bind scrollbars to the canvas
hbar.configure(command=self.canvas.xview)
# Make the canvas expandable
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
# Bind events to the Canvas
self.canvas.bind('<ButtonPress-1>', self.move_from)
self.canvas.bind('<B1-Motion>', self.move_to)
self.canvas.bind('<MouseWheel>', self.wheel)
self.imscale = 0.25
self.imageid = None
self.delta = 0.75
# Text is used to set proper coordinates to the image. You can make it invisible.
self.text = self.canvas.create_text(0, 0, anchor='nw', text='')
self.show_image()
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
def move_from(self, event):
''' Remember previous coordinates for scrolling with the mouse '''
self.canvas.scan_mark(event.x, event.y)
def move_to(self, event):
''' Drag (move) canvas to the new position '''
self.canvas.scan_dragto(event.x, event.y, gain=1)
def wheel(self, event):
''' Zoom with mouse wheel '''
scale = 1.0
# Respond to Linux (event.num) or Windows (event.delta) wheel event
if event.num == 5 or event.delta == -120:
scale *= self.delta
self.imscale *= self.delta
if event.num == 5 or event.delta == 120:
scale /= self.delta
self.imscale /= self.delta
# Rescale all canvas objects
x = self.canvas.canvasx(event.x)
y = self.canvas.canvasy(event.y)
self.canvas.scale('all', x, y, scale, scale)
self.show_image()
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
def show_image(self):
''' Show image on the Canvas '''
if self.imageid:
self.canvas.delete(self.imageid)
self.imageid = None
self.canvas.imagetk = None # delete previous image from the canvas
width, height = self.image.size
new_size = int(self.imscale * width), int(self.imscale * height)
imagetk = ImageTk.PhotoImage(self.image.resize(new_size))
# Use self.text object to set proper coordinates
self.imageid = self.canvas.create_image(self.canvas.coords(self.text),
anchor='nw', image=imagetk)
self.canvas.lower(self.imageid) # set it into background
self.canvas.imagetk = imagetk # keep an extra reference to prevent garbage-collection
pictureVar = StringVar()
pictureVar.set("a.png")
Zoom(root, path=pictureVar.get())
pictureVar.set("b.png")
mainloop()