我正在开发一个程序,将图像转换为适用于Minecraft基岩和Java版本的.mcfunction文件。在我的程序中,我使用for循环插入image.getpixel
中的x和y坐标。但是,整整一晚我都被错误困住了
TypeError:getpixel()带有2个位置参数,但给出了3个
,不确定是什么原因造成的。我以前没有这个问题,但是最近我添加了一个功能,该功能可以旋转图像预览,并且突然之间就开始发生这种情况。这是我的程序:
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
from PIL import ImageTk,Image
import os
from casegen import casegenerator
from tkinter import ttk
from tkinter import messagebox
#creates window
root = Tk()
root.title("Pixelcraft V1.0")
root.iconbitmap("pixlcraftlogo_tKt_icon.ico")
root.geometry("500x350")
print("Sucessfully loaded window!")
#declares a button style
style = Style()
style.configure('W.TButton', font =
('calibri', 10, 'bold', 'underline'),
foreground = 'black')
#declares a canvas style
canvas_width = 390
canvas_height = 210
my_canvas = Canvas(root, width=canvas_width, height=canvas_height, highlightthickness=1,
highlightbackground="gray")
my_canvas.configure(bg='white')
my_canvas.place(x=50, y=70)
startupimg = ImageTk.PhotoImage(Image.open("PixlCraftLogo.png"))
#loads image and puts it on canvas
canvasstartuplogo = my_canvas.create_image(195, 105,image=startupimg)
#widgets and functions:
#makes a prompt to name the function
def filenameinput():
convertbtn.destroy()
ent.place(x=160, y=30)
ent.insert(0, "Enter a filename")
confbutton.place(x=300, y=28)
#color comparer
def warningmaker():
messagebox.showinfo("Warning", "Depending on how large your image is, PixelCraft may become
unresponsive. Please be patient and dont close the tab to prevent file corruption.")
imgtofunc()
def imgtofunc():
confbutton.destroy()
ent.destroy()
fullcommand = str("")
steps = width/100
#scans the pixels in x axis
for i in range(width):
#scans the pixels in y axis
for j in range(height):
#returns rgb values as tuple
mred, mgreen, mblue = imgfcalc.getpixel(i, j)
blockofpix = casegenerator(mred, mgreen, mblue)
singlecommand = "setblock" + " " + "~" + " " + "~" + str(j*-1) + " " + "~" + str(i) + "
" + str(blockofpix) + "\n"
singlecommand = str(singlecommand)
fullcommand = fullcommand + singlecommand
#print(casegenerator(mred, mgreen, mblue))
#print(test1)
print(fullcommand)
#function executes after import button is pressed
def importimages():
#global variables must be defined in order for images to work
global userchosenimg
global userfile
global newtext
global width
global height
global imgfcalc
#file dialog prompt
userfile = filedialog.askopenfilename(title="Select an image", filetypes = (("All Files",
"*.*"), ("jpeg", ".jpeg"), ("jpg", ".jpg"), ("png", ".png")))
#creates a variable of just the file name rather than the whole location
for i in range(len(userfile)):
userfilestrindex = userfile[i*-1]
if userfilestrindex == "/":
charsuntilslash = len(userfile) - i
newtext = userfile[charsuntilslash+1:len(userfile)]
break
#displays name of selected image
userimagefilename = Label(root, text=newtext, font=("Arial", 8))
userimagefilename.place(x=40, y=30)
#removes the import button
importimages1.place_forget()
#removes the default logo
my_canvas.delete(canvasstartuplogo)
#removes the name of the default image
defaultimgfile.destroy()
#copys the image and converts it so that it has a numeric value of rgb
imgfcalc = Image.open(userfile)
width, height = imgfcalc.size
image_rgb = imgfcalc.convert("RGB")
loadontocanvas(0)
#puts image on canvas
imgangle1 = 0
def loadontocanvas(rotationalangle):
global resized
global finalpic
global userchosenimg1
global userchosenimg
print("loading...")
try:
my_canvas.delete(userchosenimg1)
except:
print("")
numforrez = (width*175)/height
userchosenimg = Image.open(userfile)
resized = userchosenimg.resize((int(numforrez), 175), Image.ANTIALIAS)
resized = resized.rotate(angle=imgangle1)
finalpic = ImageTk.PhotoImage(resized)
userchosenimg1 = my_canvas.create_image(195, 105,image=finalpic)
print("successfully imported image")
rotateimgbtn.place(x=50, y=300)
convertbtn.place(x=207, y=28)
#end of function
rotq = ""
rotp = ""
def rotatetheimg():
global imgangle1
global rotq
global rotp
imgangle1 += 90
if imgangle1 == 360:
imgangle1 = 0
if imgangle1 == 90:
rotq = "-"
elif imgangle1 == 180:
rotp = "-"
elif imgangle1 == 270:
rotq = "-"
rotp = "-"
elif imgangle1 == 0:
rotq = ""
rotp = ""
loadontocanvas(imgangle1)
#the name of the default image file displayed when the window first starts up
defaultimgfile = Label(root, text='PixelCraftLogo.png')
defaultimgfile.place(x=40, y=30)
startupText = Label(root, text='Select an image file to create a blueprint!', font=("calibri", 12))
startupText.place(x=0, y=0)
imageLabelText = Label(root, text='Image:', font=("Arial", 8, 'bold'))
imageLabelText.place(x=0, y=30)
rotateimgbtn = Button(root, text='⭯', style = 'W.TButton', command=rotatetheimg)
importimages1 = Button(root, text='Browse', style = 'W.TButton', command=importimages)
importimages1.place(x=160, y=30)
convertbtn= Button(root, text='Create my files!', style = 'W.TButton', command=filenameinput)
ent = Entry(root, width=20)
confbutton = Button(root, text='Confirm', style = 'W.TButton', command=warningmaker)
root.mainloop()
答案 0 :(得分:0)
好的,因此错误即将到来,因为您已经写了
imgfcalc.getpixel(i,j)
并且根据错误,方法getpixel仅采用两个位置参数 但是给定i,j是两个位置参数,一个位置参数是imgfcalc(self)。因此,您总共给出了3个参数 并且您需要删除一个参数。 函数可能使用元组/列表中的i,j参数 尝试将参数设为(i,j)而不是i,j
有关tkinter的笔记,您可以访问: https://mycodenotein.netlify.app