If you have ever needed to rotate an image you know it can be annoying to get it just right. In this article we’ll be looking at how you can use Python to rotate images both randomly and by an exact measurement.
Getting the Necessary Libraries
To create this program you will need to import two libraries, random and pillow. The random library comes pre packaged with Python and so you can simply import it with no extra work needed.
The pillow library however does not come packaged with Python and needs to be installed. This can easily be done with pip, check out the pillow website for additional details.
from PIL import Image import random
User Input and Variable Definitions
Once you’ve imported the necessary libraries the first thing you should do is get the name of the image the user wants rotated and open it. To open the image use the “Image.open()” function from the pillow library and insert the user input as an argument.
imgname = input("Enter the filename of the image you wish to rotate: ") # Gets name of image img = Image.open(imgname) # Opens up selected Image
After you’ve done this you should ask the user whether or not they want their image to be rotated randomly. Once you’ve gotten the input use a while statement that will make sure the user enters in something valid and keep them in the loop until they enter a correct input
Random = input("Do you want to randomly rotate your image? (Y/N)") while Random.upper() != "Y" and Random.upper() != "N": print("Invalid Input Please Try Again") Random = input("Do you want to randomly rotate your image? (Y/N)")
Now you can rotate the user’s image in the requested manner.
Rotating the Image Randomly
If they chose to rotate the image randomly first ask them how many times they want the image to be rotated. Be sure to use the “int()” function to convert the input from a string to an integer otherwise the program will crash when you try to use this variable later on.
if Random.upper() == "Y": amount = int(input("How many times would you like to randomly rotate the image?: "))
Next create a variable called “count” and set it equal to 0 and create a while statement that will run for as long as “count“ is less than“amount”.
count = 0 while amount > count:
Inside the while loop use “random.randint” to generate a random number between 1 and 360, this will be how many degrees the image is rotated by.
rotate = random.randint(0, 360) # Generates a random degree from 0 to 360
Next use the “.rotate()” function to turn the user’s image by the amount generated earlier. This takes two arguments, the number of degrees to rotate the image and whether or not to expand the image as it rotates. Use the “rotate” variable from earlier for the first argument, and set the second argument to “expand=True”. If you don’t set expand to true the edges of the image will get cut off every time it’s rotated.
output = img.rotate(rotate, expand=True) # Rotates image to the selected amount
Lastly use the “.save” function to save the newly rotated image to the current directory. Be sure to add the current count onto the end of the image otherwise every time the program goes through the loop it will just overwrite the image and you’ll end up with only one instead of how many you requested.
output.save("output%s.jpg" % count) # Saves Every Rotation count += 1
Rotating the Image by a Set Number of Degrees
If the user selected “N” earlier prompt them for the amount of degrees they want to rotate their image by. Then simply use this input as the first argument in the “.rotate” function and then save the image using the “.save” function.
if Random.upper() == "N": rotate = int(input("How many degrees do you want to rotate your image by?: ")) output = img.rotate(rotate, expand=True) # Rotates image by set number of degrees output.save("output.jpg") # Saves the rotation
Congratulations you’ve now successfully completed the image rotator!
If you want to expand upon the program try adding in an additional feature which will rotate the image a full 360 degrees one degree at a time, saving a picture for every rotation. You could also put in additional options which will flip the image upside down, reflect it, and give the user the option to choose between clockwise and counterclockwise.
Download the above source code here.
If you have any questions or comments email them to me at email@example.com