GIMP Chat
http://gimpchat.com/

Python-Fu (TypeError: wrong parameter type)
http://gimpchat.com/viewtopic.php?f=9&t=15460
Page 1 of 1

Author:  TRBear [ Mon Apr 10, 2017 8:15 pm ]
Post subject:  Python-Fu (TypeError: wrong parameter type)

Hi there. I have been having some problems with my Python-Fu script.

Before I show my script, I should clarify what I want my script to do.

1. Find an .xcf file that is located in a specific file directory
2. Set text onto a given text layer
3. Save the .xcf as a .png

Here are the script templates I used:

image = pdb.gimp_xcf_load(dummy_param, filename, raw_filename) /
pdb.gimp_text_layer_set_text(layer, text) /
pdb.file_png_save(image, drawable, filename, raw_filename, interlace, compression, bkgd, gama, offs, phys, time) /


And here is what I wrote in it:

image = pdb.gimp_xcf_load(0, "C:\E\D\C\B\A.xcf", "C:\E\D\C\B\A.xcf")
pdb.gimp_text_layer_set_text("TextLayer", "Hello_World")
pdb.file_png_save(1Picture.png, 1Picture.png, HelloWorld, HelloWorld, No, 9, No, No, No, No, Yes)


This is the error I get...

image = pdb.gimp_xcf_load(0, "C:\E\D\C\B\A.xcf", "C:\E\D\C\B\A.xcf")
>>> pdb.gimp_text_layer_set_text("TextLayer", "Hello_World")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong parameter type
>>> pdb.file_png_save(1Picture.png, 1Picture.png, HelloWorld, HelloWorld, No, 9, No, No, No, No, Yes)


Any help would be appreciated. Thanks.

Author:  ofnuts [ Tue Apr 11, 2017 2:12 am ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

Plenty of problems....
  • (Minor...) The 2nd parm of gimp_xcf_load is the "base name" of the file ("A.xcf")
  • In string literals, the backslash ("\") has a special meaning, for instance "\t" is a tab. To enter a backslash you should double it: "C:\\E\\D\\C\\B\\A.xcf". This only apply to string literals in your source code, if the string is read from elsewhere, the backslash is a backslash.
  • pdb.gimp_text_layer_set_text("TextLayer", "Hello_World") changes the text on an existing layer (the first parameter is a text layer object). To create a text layer, use pdb.gimp_text_layer_new(...). Note that this doesn't add the layer to the image, you have to add it explicitly (pdb.gimp_image_add_layer(image,layer,position)
  • file_png_save() only saves a single layer, so you have to merge your layers before saving the resulting later (try layer_new_from_visible())
  • the second parameter of "file_png_save" is a layer object (obtained above)
  • put quotes around file names "1Picture.png"
  • Unless you defined them as variables, "Yes", and "No" aren't possible values. Try "True" and "False" (without quotes), or 1 and 0.
  • But you can save yourself so grief and use file_png_save_defaults(image, layer, filename, raw_filename)

Author:  TRBear [ Tue Apr 11, 2017 12:47 pm ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

Thank you very much for the response. With the information you mentioned, I imagine that these are scripts that are relevant for my desired script (in this order):

>>> image = pdb.gimp_xcf_load(dummy_param, filename, raw_filename)
>>> layer = pdb.gimp_text_layer_new(image, text, fontname, size, unit)
>>> pdb.gimp_image_insert_layer(image, layer, parent, position)
>>> pdb.gimp_text_layer_set_text(layer, text)
>>> layer = pdb.gimp_layer_new_from_visible(image, dest_image, name)
>>> pdb.file_png_save_defaults(image, drawable, filename, raw_filename)


Inputting my own information, I get...

image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
layer = pdb.gimp_text_layer_new(“A”, “TextLayer”, “Arial”, 64, 1)
pdb.gimp_image_insert_layer(“A”, “TextLayer”, “Background”, (640, 640))
pdb.gimp_text_layer_set_text("TextLayer", "Hello_World")
layer = pdb.gimp_layer_new_from_visible("A", "A", "B")
pdb.file_png_save_defaults("A", "B", "C:\\J\\K\\F\\O”, “O”)


and when inputted into the console this appears...

>>> image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: Could not open 'C:\\E\\D\\C\\B\\A.xcf' for reading: No such file or directory
>>> layer = pdb.gimp_text_layer_new(“A”, “TextLayer”, “Arial”, 64, 1)
  File "<input>", line 1
    layer = pdb.gimp_text_layer_new(“A”, “TextLayer”, “Arial”, 64, 1)
                                    ^
SyntaxError: invalid syntax
>>> pdb.gimp_image_insert_layer(“A”, “TextLayer”, “Background”, (640, 640))
  File "<input>", line 1
    pdb.gimp_image_insert_layer(“A”, “TextLayer”, “Background”, (640, 640))
                                ^
SyntaxError: invalid syntax
>>> pdb.gimp_text_layer_set_text("TextLayer", "Hello_World")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong parameter type
>>> layer = pdb.gimp_layer_new_from_visible("A", "A", "B")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong parameter type
>>> pdb.file_png_save_defaults("A", "B", "C:\\J\\K\\F\\O”, “O”)


/

I cannot see any clear mistakes within my script. I do think that there may be an error in the way that I inputted the "image" criteria; please let me know the proper way to do it if it is wrong. Regardless of all this, thanks again for your detailed response, I really do appreciate it.

Author:  ofnuts [ Tue Apr 11, 2017 5:15 pm ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

>>> image = pdb.gimp_xcf_load(dummy_param, filename, raw_filename)
>>> layer = pdb.gimp_text_layer_new(image, text, fontname, size, unit)
>>> pdb.gimp_image_insert_layer(image, layer, parent, position)

>>> pdb.gimp_text_layer_set_text(layer, text)

Not really useful since you can set the text in gimp_text_layer_new

>>> layer = pdb.gimp_layer_new_from_visible(image, dest_image, name)
>>> pdb.file_png_save_defaults(image, drawable, filename, raw_filename)


Inputting my own information, I get...

image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
layer = pdb.gimp_text_layer_new(“A”, “TextLayer”, “Arial”, 64, 1)


Actually: layer = pdb.gimp_text_layer_new(image, “TextLayer”, “Arial”, 64, UNIT_PIXEL)
You have to understand what a variable is. There is a fairly good explanation on Wikipedia. The image parameter is the image variable you created when you did image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf"). The last parameter is a number, but using one of the "constant variables" that are initialized with numeric values is much more readable and avoids errors. For the complete list:
>>> [u for u in dir() if "UNIT_" in u]
['UNIT_INCH', 'UNIT_MM', 'UNIT_PICA', 'UNIT_PIXEL', 'UNIT_POINT']

pdb.gimp_image_insert_layer(“A”, “TextLayer”, “Background”, (640, 640))

pdb.gimp_image_insert_layer(image, layer, None,0), because image and layer are the two variables you set above. None tells that there is no parent (there is a parent if you insert your layer in a layer group). The position is not a position in (X,Y), but the position in the layer stack. 0 is the top position.

pdb.gimp_text_layer_set_text("TextLayer", "Hello_World")

As said above, not necessary since you have already set the text when you created the layer. But if you really want this, the first parameter is the variable that contains the current text layer:
pdb.gimp_text_layer_set_text(layer, "Hello_World")

layer = pdb.gimp_layer_new_from_visible("A", "A", "B")

merged_layer = pdb.gimp_layer_new_from_visible(image,image, "New from visible")

image is used twice, once to designate the image from which the new layer is constructed, and once to designate
the target image. There are cases where you would use this to create the visible layer in another image and you would use two different image variables. Use a new variable merged_layer to hold the new layer.

pdb.file_png_save_defaults("A", "B", "C:\\J\\K\\F\\O”, “O”)

pdb.file_png_save_defaults(image,merged_layer, "C:\\J\\K\\F\\O”, “O”)

Note that you don't add the layer to the image (pdb.gimp_image_insert_layer(...)) but AFAIK you don't need this to save the image.

and when inputted into the console this appears...

>>> image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: Could not open 'C:\\E\\D\\C\\B\\A.xcf' for reading: No such file or directory
>>> layer = pdb.gimp_text_layer_new(“A”, “TextLayer”, “Arial”, 64, 1)
File "<input>", line 1
layer = pdb.gimp_text_layer_new(“A”, “TextLayer”, “Arial”, 64, 1)
^
SyntaxError: invalid syntax
>>> pdb.gimp_image_insert_layer(“A”, “TextLayer”, “Background”, (640, 640))
File "<input>", line 1
pdb.gimp_image_insert_layer(“A”, “TextLayer”, “Background”, (640, 640))
^
SyntaxError: invalid syntax
>>> pdb.gimp_text_layer_set_text("TextLayer", "Hello_World")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: wrong parameter type
>>> layer = pdb.gimp_layer_new_from_visible("A", "A", "B")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: wrong parameter type
>>> pdb.file_png_save_defaults("A", "B", "C:\\J\\K\\F\\O”, “O”)

I cannot see any clear mistakes within my script. I do think that there may be an error in the way that I inputted the "image" criteria; please let me know the proper way to do it if it is wrong. Regardless of all this, thanks again for your detailed response, I really do appreciate it.


Mostly you are confusing the "names" of the images and layers (as displayed in the UI) with the names of variables that hold these "object" in the program.

Author:  TRBear [ Mon Apr 17, 2017 1:00 am ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

Hello again. Happy Easter if you celebrate that. Using the reccomendations you listed above, I managed to stitch together this script:

image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
layer = pdb.gimp_text_layer_new(image, “TextLayer”, “Arial”, 64, UNIT_PIXEL)
pdb.gimp_image_insert_layer(image, layer, None,0)
pdb.gimp_text_layer_set_text(layer, "Hello_World")
merged_layer = pdb.gimp_layer_new_from_visible(image,image, "New from visible")
pdb.file_png_save_defaults(image,merged_layer, "C:\\J\\K\\F\\O”, “O”)

Inputting this script onto the Python console gives me this result:

>>> image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
>>> layer = pdb.gimp_text_layer_new(image, “TextLayer”, “Arial”, 64, UNIT_PIXEL)
File "<input>", line 1
layer = pdb.gimp_text_layer_new(image, “TextLayer”, “Arial”, 64, UNIT_PIXEL)
^
SyntaxError: invalid syntax
>>> pdb.gimp_image_insert_layer(image, layer, None,0)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'layer' is not defined
>>> pdb.gimp_text_layer_set_text(layer, "Hello_World")
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'layer' is not defined
>>> merged_layer = pdb.gimp_layer_new_from_visible(image,image, "New from visible")
>>> pdb.file_png_save_defaults(image,merged_layer, "C:\\J\\K\\F\\O”, “O”)

There are some issues with the defining of the layer. This seems to stem from the root cause of the Syntax Error for line 2, but I cannot see what is actually wrong with that line. Other than that, everything else seems to have run without any problems. If you could take a look and see what you think is the issue, it would be greatly appreciated.

Author:  paynekj [ Mon Apr 17, 2017 2:14 am ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

on the line layer = pdb.gimp_text_layer_new(image, “TextLayer”, “Arial”, 64, UNIT_PIXEL) you've used "smart quotes" instead of proper double-quotes

Author:  ofnuts [ Mon Apr 17, 2017 2:29 am ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

As Kevin says :) A frequent problem when one uses a non-programmer editor to write code(*). If you haven't got it already, find the "notepad++" editor, it's free and will do syntax highlighting (so it shows you such problems without even needing to run the code).

Also, use 'code' tags to bracket your code in your posts (the '#' icon in the post editor) it makes it much more readable.

In other words, this:

Image

produces this:

image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
layer = pdb.gimp_text_layer_new(image, “TextLayer”, “Arial”, 64, UNIT_PIXEL)
pdb.gimp_image_insert_layer(image, layer, None,0)
pdb.gimp_text_layer_set_text(layer, "Hello_World")
merged_layer = pdb.gimp_layer_new_from_visible(image,image, "New from visible")
pdb.file_png_save_defaults(image,merged_layer, "C:\\J\\K\\F\\O”, “O”)


(*) At work a server was broken for several days because someone copied a configuration value directly from an MS-Office document. On startup the server was looking for a file with a non-breaking space in the file name. Very hard to find:)

Author:  TRBear [ Mon Apr 17, 2017 3:26 pm ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

It seems that the type of quotations was a problem and now the script can run sucessfully. Here is the final script:

image = pdb.gimp_xcf_load(0, "C:\\E\\D\\C\\B\\A.xcf", "A.xcf")
layer = pdb.gimp_text_layer_new(image, "TextLayer", "Arial", 64, UNIT_PIXEL)
pdb.gimp_image_insert_layer(image, layer, None,0)
pdb.gimp_text_layer_set_text(layer, "Hello_World")
merged_layer = pdb.gimp_layer_new_from_visible(image,image, "New from visible")
pdb.file_png_save_defaults(image,merged_layer, "C:\\J\\K\\F\\O.png", "O.png")


Note: You need to specify a file extension for the final line of "pdb.file_png_save_defaults". In my case, I used .png

While the script can run successfully, it doesn't change the original .xcf picture at all. I'll try my best to explain what exactly I am trying to do with the use of pictures.

I want the "123" in Example1.png to become "Five" as seen in Example2.png

I had thought that I needed to use the "pdb.gimp_text_layer_set_text" line, but it seems that it needs to be defined using the "layer = pdb.gimp_text_layer_new" line. If there is any way to do what I am trying to do, please let me know.

Also, thanks for the reminder about the
formatting. I edited my previous posts to make it much easier to read!

Attachments:
Example2.png
Example2.png [ 126.04 KiB | Viewed 3547 times ]
Example1.png
Example1.png [ 117.91 KiB | Viewed 3547 times ]

Author:  ofnuts [ Mon Apr 17, 2017 6:43 pm ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

TRBear wrote:
While the script can run successfully, it doesn't change the original .xcf picture at all.

If you want th XCF file to change you also have to save the image.


TRBear wrote:
I'll try my best to explain what exactly I am trying to do with the use of pictures.

I want the "123" in Example1.png to become "Five" as seen in Example2.png

I had thought that I needed to use the "pdb.gimp_text_layer_set_text" line, but it seems that it needs to be defined using the "layer = pdb.gimp_text_layer_new" line. If there is any way to do what I am trying to do, please let me know.

Still not clear... You create the layer with some text, and then before using the layer, you change the text. Why not create the layer with the final text directly? When you change the text of the layer in the script, the layer is not resized, so at best the text won't be centered in the layer, and in many cases it will be cut off. So even if the layer existed before, it is often a better idea to delete it and recreate a new one.

Author:  TRBear [ Sun Apr 30, 2017 12:10 am ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

Hi there. I just got back to this right now. I have no problems with doing it the way above as long as it does what I am trying to do. Also, could you tell me what you mean by saving the image? I could not find any script on the Python Procedure Browser that would do that, so if you could provide me with what you have in mind, that would be appreciated.

Author:  ofnuts [ Sun Apr 30, 2017 4:40 am ]
Post subject:  Re: Python-Fu (TypeError: wrong parameter type)

TRBear wrote:
Also, could you tell me what you mean by saving the image? I could not find any script on the Python Procedure Browser that would do that, so if you could provide me with what you have in mind, that would be appreciated.


pdb.gimp_file_save() or even better pdb.gimp_xcf_save().

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/