TypeError: an integer is required (got type str) when using image.resize()
TypeError: an integer is required (got type str) when using image.resize()
我想使用滑块小部件使用浮点值在小范围内缩放图像。我已经创建了滑块和函数来缩放图像,如下所示,但是我收到错误“TypeError:需要一个整数(获取类型 str)”。欢迎任何解决此问题的建议。
def showImage():
img = Image.open('image.png')
img = ImageTk.PhotoImage(img.resize((260, 220), Image.ANTIALIAS))
img_label = Label(frame1, image=img)
img_label.image = img
return img
## function to zoom the image ##
def zoom_img(zoom):
showImage()
newsize = (img.size[0]*zoom,
img.size[1]*zoom)
scaledImg = img.resize(newsize, Image.NEAREST)
newImg = ImageTk.PhotoImage(scaledImg)
img_label.configure(image=newImg)
## scaler widget ##
var = StringVar()
zoom_scale = tk.Scale(root, variable=var, orient='horizontal', from_=220.0, to=440.0, length=100, resolution=20.0, command=zoom_img)
zoom_scale.place(x=1600, y=160)
## Error generated ###
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "c:/Users/DANIEL/Desktop/exp/fcnvmb/butt.py", line 218, in zoom_img
scaledImg = img.resize(newsize, Image.NEAREST)
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 1780, in resize
return self.convert('RGBa').resize(size, resample, box).convert('RGBA')
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 1784, in resize
return self._new(self.im.resize(size, resample, box))
TypeError: an integer is required (got type str)
传递给 tk.Scale
command
回调的值是一个字符串。您需要将其转换为浮点数。
def zoom_img(zoom):
zoom = float(zoom)
...
跟进
好的,这是一个似乎有效的版本。我假设您希望图像 window 保持恒定大小,这意味着您还需要平移。我已经填写了一些你遗漏的细节,但是一旦你替换你的图像文件名,这应该 cut-and-paste 运行。
from tkinter import *
from PIL import Image, ImageTk
def showImage():
img = Image.open('7460.PNG')
img = ImageTk.PhotoImage(img.resize((260, 220), Image.ANTIALIAS))
img_label = Label(root, image=img)
img_label.image = img
img_label.pack()
return img_label
## function to zoom the image ##
def zoom_img(zoom):
global newImg
img = Image.open('7460.PNG')
newsize = (img.size[0]*int(zoom), img.size[1]*int(zoom))
scaledImg = img.resize(newsize, Image.NEAREST)
newImg = ImageTk.PhotoImage(scaledImg)
img_label.configure(image=newImg, width=260,height=220)
root = Tk()
## scaler widget ##
var = StringVar()
zoom_scale = Scale(root, variable=var, orient='horizontal', from_=1, to=20, length=100, resolution=1, command=zoom_img)
img_label = showImage()
zoom_scale.pack()
root.mainloop()
我想使用滑块小部件使用浮点值在小范围内缩放图像。我已经创建了滑块和函数来缩放图像,如下所示,但是我收到错误“TypeError:需要一个整数(获取类型 str)”。欢迎任何解决此问题的建议。
def showImage():
img = Image.open('image.png')
img = ImageTk.PhotoImage(img.resize((260, 220), Image.ANTIALIAS))
img_label = Label(frame1, image=img)
img_label.image = img
return img
## function to zoom the image ##
def zoom_img(zoom):
showImage()
newsize = (img.size[0]*zoom,
img.size[1]*zoom)
scaledImg = img.resize(newsize, Image.NEAREST)
newImg = ImageTk.PhotoImage(scaledImg)
img_label.configure(image=newImg)
## scaler widget ##
var = StringVar()
zoom_scale = tk.Scale(root, variable=var, orient='horizontal', from_=220.0, to=440.0, length=100, resolution=20.0, command=zoom_img)
zoom_scale.place(x=1600, y=160)
## Error generated ###
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "c:/Users/DANIEL/Desktop/exp/fcnvmb/butt.py", line 218, in zoom_img
scaledImg = img.resize(newsize, Image.NEAREST)
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 1780, in resize
return self.convert('RGBa').resize(size, resample, box).convert('RGBA')
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 1784, in resize
return self._new(self.im.resize(size, resample, box))
TypeError: an integer is required (got type str)
传递给 tk.Scale
command
回调的值是一个字符串。您需要将其转换为浮点数。
def zoom_img(zoom):
zoom = float(zoom)
...
跟进
好的,这是一个似乎有效的版本。我假设您希望图像 window 保持恒定大小,这意味着您还需要平移。我已经填写了一些你遗漏的细节,但是一旦你替换你的图像文件名,这应该 cut-and-paste 运行。
from tkinter import *
from PIL import Image, ImageTk
def showImage():
img = Image.open('7460.PNG')
img = ImageTk.PhotoImage(img.resize((260, 220), Image.ANTIALIAS))
img_label = Label(root, image=img)
img_label.image = img
img_label.pack()
return img_label
## function to zoom the image ##
def zoom_img(zoom):
global newImg
img = Image.open('7460.PNG')
newsize = (img.size[0]*int(zoom), img.size[1]*int(zoom))
scaledImg = img.resize(newsize, Image.NEAREST)
newImg = ImageTk.PhotoImage(scaledImg)
img_label.configure(image=newImg, width=260,height=220)
root = Tk()
## scaler widget ##
var = StringVar()
zoom_scale = Scale(root, variable=var, orient='horizontal', from_=1, to=20, length=100, resolution=1, command=zoom_img)
img_label = showImage()
zoom_scale.pack()
root.mainloop()