0

I am working on a program that converts images to .mcfunction files for minecraft bedrock and java edition. In my program, i'm using for loops to plug in for x and y coordinates in image.getpixel. However, this entire night I've been stuck with the error

TypeError: getpixel() takes 2 positional arguments but 3 were given

and am not sure what is causing this. I used to not have this problem but I recently added a function that rotates the image preview and all of the sudden this started happening. Here is my program:

  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()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Cooper
  • 27
  • 1
  • 2
  • 5
  • Lots of code, please trim it down to just the important part – Delrius Euphoria Oct 20 '20 at 04:18
  • Can you post the full error traceback? – B. Bogart Oct 29 '20 at 03:21
  • "However, this entire night I've been stuck with the error" - did you try to look at the error traceback, in order to understand where in the code the problem occurs? Do you see code that tries to call `getpixel`? Do you *understand* the error message? "and am not sure what is causing this." Well, do you know what a positional argument is? – Karl Knechtel Sep 16 '22 at 21:15

1 Answers1

0

Okay, so the error is coming because you had written

imgfcalc.getpixel(i,j)

And according to the error, the method getpixel takes two positional arguments only. But you have given i , j are two positional arguments and one positional argument is imgfcalc(self). Therefore, you have given in total 3 arguments And you need to remove one argument. The function might take those i,j arguments in tuple/list try giving argument as (i,j) instead of i,j

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343