Below is a brief summary of what is happening here. The first two lines initialize the script and give you access to some useful libraries. The following code section def first_plugin contains instructions that you provide to GIMP. Information followed from the register is everything that GIMP needs to know about the plugin.
This is the information you need to provide to GIMP to register your script:
Finally, you need to call the main () command .
Save your script and select All Files from the Save as type menu. Be sure to include the .py extension in your file name.
Next, place this file in the GIMP plug-in directory, which can be found in Windows at Program Files> GIMP 2> lib> gimp> 2.0 (or ~ LibraryApplication SupportGIMP2.8scripts on a Mac). You may need admin rights to do this.
Create GIMP and open the Image menu . You will see Hello, World! right at the bottom.
Now is the time to make your script a bit more useful.
Now, you will have to rewrite your script so that it can actually do something practical. Open the text file again, then copy and paste the following code:
#! / usr / bin / env python
from gimpfu import *
def test_script (customtext, font, size):
img = gimp.Image (1, 1, RGB)
layer = pdb.gimp_text_fontname (img, None, 0, 0, customtext, 10, True, size, PIXELS, font)
img.resize (layer.width, layer.height, 0, 0)
gimp.Display (img)
gimp.displays_flush ()
register (
"python_test",
"KIỂM TRA",
"KIỂM TRA",
"Brad Jones",
"Brad Jones",
"2017",
"KIỂM TRA",
"",
[
(PF_STRING, "customtext", "Text string", 'Scripting is handy!'),
(PF_FONT, "font", "Font", "Sans"),
(PF_SPINNER, "size", "Font size", 100, (1, 3000, 1)),
],
[],
test_script, menu = "/ File / Create")
main ()
This is a bit more complicated than the Hello, World! Scripts, but they are very similar in structure. First, create an image.
img = gimp.Image (1, 1, RGB)
Then you add text based on user-provided parameters.
layer = pdb.gimp_text_fontname (img, None, 0, 0, customtext, 10, True, size, PIXELS, font)
Next, you resize the image to the size of the text.
img.resize (layer.width, layer.height, 0, 0)
Finally, you ask GIMP to display the image on the screen.
gimp.Display (img)
gimp.displays_flush ()
All that's left is to add the registration information that GIMP needs, with the addition of some parameter settings that you haven't done before.
[
(PF_STRING, "customtext", "Text string", 'Scripting is handy!'),
(PF_FONT, "font", "Font", "Sans"),
(PF_SPINNER, "size", "Font size", 100, (1, 3000, 1)),
],
Save this file as you did with the Hello, World! Script, move it to the plug-ins directory and restart GIMP. Go to File> Create> TEST to try your plugin.
You will see a window where you can set different parameters.
Click OK and you will create an image that looks like this.
This illustrates how you can use GIMP scripts to automate a process, including several different actions. Now, write a script to make changes to an image that you have opened.
Once you are comfortable with Python programming in GIMP, you can automate all types of editing for your images. However, we will start as simple as possible, by deploying a script to reverse the color of the current layer.
To get started, reopen the text editor, then copy and paste the following script:
#! / usr / bin / env python
from gimpfu import *
def invert_current_layer (img, layer):
pdb.gimp_invert (layer)
register (
"python_fu_invert_current_layer",
"Invert layer",
"Invert colors in the current layer",
"Brad Jones",
"Brad Jones",
"2017",
"/ Filters / Custom / Invert current layer",
"*",
[],
[],
invert_current_layer)
main ()
This is the result of the script you created earlier. The first two lines of code specify a number of platforms, and some of the final lines take care of the registration. This is an important part:
def invert_current_layer (img, layer):
pdb.gimp_invert (layer)
You are defining your process, letting GIMP know which components you will refer to, then using pdb.gimp_invert to guide the color adjustment program. Save this file in .py format, add it to the plug-ins folder , then open GIMP to check if it works.
Navigate to Filters> Custom> Invert current layer .
You will get a similar result as above. Of course, performing a reverse operation in GIMP is relatively easy, but this is only the starting point. The great thing about writing your own script is that you can create something that suits you perfectly.
Once you understand the basic concepts of programming in GIMP, it's time to start testing. Think about the type of process you usually do a lot of times and it will be useful if automated. Then you will come to the most difficult part: Find out how to use code to realize those ideas.
Fortunately, GIMP gives you some support. Navigate to Help> Procedure Browser and you will be able to access a list of all the processes you can use.
Procedure Browser not only lists processes but also provides you with information about the parameters you need to provide in your code.
You can scroll through the entire list of processes or use the search bar to narrow down the field. After that, just insert the process name and parameters into your script.
This information will be invaluable when you work on your scripts. Start with some simple tools, before you perform some really useful automated processes!
Do you need help with programming with GIMP? Or do you have a trick you want to share with other users? Either way, why not leave your comments in the comment section below?
See more: