Turtle Positioning: goto, setpos, setx, sety, setheading
- In this tutorial we will look at the PIL methods that can be used to resize an image.
- We will also check out another method that can be used to get the size (resolution) of an image.
Let’s first open an image so we can resize it.
- We’re going to need Image and ImageFilter modules from PIL library.
from PIL import Image, ImageFilter
setpos (also can be used as .setposition()) can be used to set a position for turtle.
If you combine it with penup() and pendown() methods you can avoid drawing while positioning the turtle.
- Technically all of these methods will do the same thing and move turtle to coordinates (100,100):
setx can be used to change the turtle’s positing along the x-axis (horizontally) while vertical position is untouched.
It can be used as:
turtle.sety(-200) to go to 200 below zero horizontally.
sety can be used to change the turtle’s positing along the y-axis (vertically) while horizontal position is untouched.
It can be used as:
turtle.sety(-200) to go to 200 below zero vertically.
This method can be used to set the heading of turtle. While other methods such as left or right can be used to turn and rotate relatively, setheading offers an absolute way to set heading.
- 0: Turtle will turn East
- 90: Turtle will turn North
- 180: Turtle will turn West
- 270: Turtle will turn South
For instance, you can use
turtle.heading(180) to turn your turtle to the West.
Example 1: Primitive Coat Hanger
We can use our newly acquired turtle positioning knowledge to create a simple coat hanger without the hook. I think it actually exists in hotel wardrobes for practicality.
- We will assign a new color at each step to highlight the usage of each method
- We’ll go to bottom left first using:
- Then we’ll move laterally to the bottom right corner:
- We’ll then go back to origin (0,0) using:
- Finally we’ll draw vertically for the hanging stick part using:
import turtle turtle.color("green") turtle.setpos(-200,-100) turtle.color("blue") turtle.setx(200) turtle.color("orange") turtle.goto(0,0) turtle.color("red") turtle.sety(50) turtle.exitonclick()
Example 2: Left Single Angle Quotation Mark
Let’s draw a left single angle quotation mark only using heading and forward with turtle in Python.
Writing the name of this character is almost harder than drawing it with turtle.
- We’ll first set heading to Northwest:
- Then we’ll move forward:
- We’ll then set heading to Northeast:
- We’ll move forward once again:
import turtle turtle.color("red") turtle.setheading(135) turtle.forward(100) turtle.setheading(45) turtle.forward(100) turtle.exitonclick()
You can hide turtle icon for a better result.
In this Python Tutorial, we’ve seen how to resize an image and different scenarios regarding the new size.
We have also seen hot to get the size of an image and we saw an example regarding how to make use of this information.
You can really see the power of PIL library in image processing as well as basic image editing tasks through these operations.