After the last write-up you should now recognize with the Pythonista app in addition to the eager community of developers which support it. While we did develop a brand-new script and find out how to import various other scripts into Pythonista, we did not in fact compose any Python code. And while we are not4 be composing any scripts from scratch this time, we’ll be learning the best ways to check out a Python program, as well as to modify particular elements of it to add functionality or make it our own.
A Brief Description of Terminology
Just so are not0 all on the exact same page as we being writing Python code I thought I ‘d go through a few terms that we will be using later on.
Variable: a name assigned to a value. Sounds basic, however exceptionally effective in practice as the value can alter while the name remains the same.
Function: a section of code within a larger program that performs a particular task. Features are vital to keeping code versatile and modular.
Parameter: a special kind of variable made use of when calling a function, offering input values that’ll be utilized later on in the function. Parameters are a huge reason features stay useful and pliable.
Script: term used rather freely, commonly to describe a program consisted of within a file. Since all of the programs composed with Pythonista are included within a single file, they are commonly referred to as scripts.
Editor: the area of the Pythonista app where you edit code.
Console: the section of Pythonista where output from your scripts goes. The console is also typically referred to as the Interpreter, because straight Python code can be entered into the console and run as code. The console is likewise utilized as a location to enter input to be processed by your script.
Module: incorporated into Pythonista, modules are self-contained pieces of Python code offering extra functionality not found in the Python language itself. We will be leveraging several modules in the examples we look at today.
Breaking Down a Python Script
Instead of starting with that very daunting blank page, lets walk with a basic Python script so you can get a feel for the way one is structured prior to composing among your very own.
Import statements are used to trigger Python modules within a script. For consistency’s sake, they are often declared on separate lines, one module per line.
Get the current on iPhone/iPad innovation: Ipads Advisor
While some programs languages need some specific syntax when initializing a variable versus reassigning the value of the variable, Python does not. Instead, merely assign the variable a value and consider it created.
Defining a variable
With Python’s focus on readability, the syntax made use of to specify a feature is clean and clear. The def keyword is put at the start, followed by the name of the function. Any specifications are put inside parentheses, and default values for those criteria may be included also. The function definition is finished with a colon, and the lines comprising the feature are indented.
Defining a function
This one is so simple it barely requires mentioning. Take a look at the bottom of the image below. The really last line of code. That’s the best ways to call a feature in Python, the feature name followed by a pair of parentheses. Any specifications for the function would be passed in by putting them within the parameters.
Calling a function
Modifying a Script: Meme Generator
Ok, lets take what we just learned about how Python scripts are constructed and use it to customize a pre-existing script. In our last lesson, among the scripts highlighted was a Meme Generator that used the Python Image Library (PIL) to add text to the image. are not1 going to look at how changing some variables and a criterion or 2 will let’s modify the resulting image in a significant method. Then we will leverage some of those existing variables and with some simple reasoning improve this script.
Change the Font Color
Open the Meme Generator script in your copy of Pythonista. If you do not have the script, have a look at our last lesson for the directions on how to get it. On line 32 of the Editor we see the following code:
draw. text((10, draw_y), text, font=font, fill='white')
The parameter fill is being readied to ‘white’. Lets change it to ‘red’.
draw. text((10, draw_y), text, font=font, fill='red')
Run the script by pushing the ‘Run’ button and enter in some test text.
The text has actually now changed to red.
Success! As you can see from the image below, our text is now red.
Changing the Font
Now lets alter the font that’s being used here. I’ve actually constantly been partial to Futura, so we will swap that out and see exactly what it looks like. On line 21 in the Editor we see the following:
font = ImageFont. truetype('HelveticaNeue-CondensedBlack', s)
This line sets the font to be utilized as well as the size that text is visiting be. The size is figured out dynamically as the surrounding code block programs. The script utilizes the ImageFont module, which I’ll talk about in higher depth in a future lesson. In the meantime all you actually should understand is that by chaining the first specification in this feature call, we can change the font that’s made use of in our final image. Change the first parameter to be ‘Futura-Medium’.
font = ImageFont. truetype('Futura-Medium', s)
Run the script and enter in some test text.
The font has actually now altered to Futura.
Voila! are not3 effectively altered the font!
Centering the Text
Now that are not2 effectively altered the font and font color, lets get a little more complicated and write some reasoning to center the text. This piece of code will be trickier to compose. It is not really simply changing a few specifications of variables. We should compose some code that determines the final width of the text, subtracts that from the width of the overall image, divides that by 2 and then uses the resulting value as the beginning point for where to place the text. If all that appeared a little frustrating, bear with me. We will take it step-by-step. First we should figure out where in the script to put our brand-new code. are not1 customizing where the text is rendered, so we want to discover exactly what line renders the text, and then begin composing our code above that. The good news is the initial script author did a remarkable task commenting his script, so we know that the code that draws the text starts on line 25:
# Draw the text numerous times in black to get the outline:
The original writer made use of a variable called x to store the value of the ‘x’ collaborates. In line 26 the variable x is set using the xrange function and with a for loop, text gets drawn with the ‘x’ collaborates whose values go from -3 to 4.
for x in xrange(-3, 4):
You might think that implies that the value for the ‘x’ begins at ‘-3 ″ and then continues to “4″. But we should look at the line that actually draws the text, line 29:
draw. text((10 + x, draw_y), text, font=font, fill='black')
Here we see that the ‘x’ work with is really ’10″ plus the value of x. Now that we understand how to set the ‘x’ coordinate, we will put our line that computes the new starting ‘x’ value. Put it above the comment discovered on line 25:
x = (img.size - w)/2
Lets break this line down a little bit. are not1 setting a variable called x. Exactly what are not0 setting it to is the outcome of a basic algebraic picture. We want to take the width of the image and subtract the width of the text from that, then divided the distinction in half. are not1 getting the image width thanks to the ImageDraw module and the img variable that’s passed in as a criterion to the feature are not0 working within.
The specification is readied to the variable of img, and the module provides a size method. Adding the  accesses the width. To learn more on the ImageDraw module and the size approach, you can refer to the inbuilt paperwork. But we will reach that more in the next write-up. To obtain the width of the text are not0 making use of the w variable which is set up on line 22 as a component of the original script. Finally are not0 dividing the entire thing by two.
We cannot forget about the xrange section which develops the black overview around the text. Rather of just ‘-3 ″ and “4″ we will add x:
for x in xrange(x-3, x+4):
We likewise need to eliminate where ’10″ is added to x:
draw. text((x, draw_y), text, font=font, fill='black')
While you could think are not0 done, however we want to focus the primary text too, not simply the outline. So drop down to line 33 and set the ‘x’ collaborate in the draw.text() function:
draw. text((x-2, draw_y), text, font=font, fill='red')
We added the “-2″ to center the primary text within the outline. If you run the script now and add some text, you’ll see it now nicely centered in you image.
Our nicely centered text.
Alright, now that are not5 played around with some Python code and have seen that the entire world does not come crashing down if things are not perfect, don’t hesitate to fiddle with some of the other scripts you discover. In the next post we will dive more into modules and the potential they open for composing scripts of your own.