- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - PIL Attributes
Python Imaging Library (PIL) is a capable library that gives broad back for opening, controlling, and sparing numerous different picture file formats. PIL offers a wide run of properties that permit engineers to perform different operations on pictures, such as resizing, cropping, rotating, enhancing, and applying channels. PIL offers a broad set of capacities and traits that empower assignments such as picture resizing, cropping, rotating, filtering, improving, and more. In this article, we'll dig into a few of the foremost basic PIL properties alongside their calculations and steps, and compare Python syntax.
Python Imaging Library
Python Imaging Library (PIL) is a prevalent library within the Python ecosystem that gives a wide extend of capabilities for working with pictures. It permits designers to open, control, and spare different picture record groups.
With PIL, designers can load a picture from a record utilizing the Image.open() function. Once the picture is stacked, it can be gotten to and adjusted utilizing different properties and strategies. For illustration, the resize() attribute permits resizing a picture whereas keeping up its angle proportion, making it helpful for creating thumbnails or altering picture measurements for diverse purposes.
PIL too gives the crop() attribute, which permits extricating a particular parcel of a picture based on characterized arranges. This quality is valuable for centering on a particular locale of intrigued inside a picture or expelling undesirable parts.
Picture turn can be accomplished utilizing the rotate() attribute in PIL. It empowers designers to turn a picture by an indicated point, which is important for adjusting picture introduction or accomplishing creative impacts.
In expansion to these essential picture control attributes, PIL offers a wide range of other functionalities. These incorporate applying channels to pictures utilizing the filter() attribute, improving picture quality with the enhance() attribute, including content or other realistic overlays, and indeed performing progressed picture preparing operations such as histogram equalization and color channel control.
PIL underpins a differing run of picture record designs, counting well-known ones such as JPEG, PNG, GIF, BMP, and TIFF. This adaptability permits engineers to work with pictures from different sources and save the prepared pictures in several designs as required.
To use PIL's capabilities, designers must introduce the library utilizing a suitable bundle director, such as pip. Once introduced, the library can be imported into Python scripts utilizing the consequence explanation.
Approach 1: Image Resizing
Resizing a picture could be a common assignment in picture preparation. PIL gives the resize() attribute, which empowers us to resize a picture to a craved width and height while keeping up its perspective proportion. Here's the calculation and step-by-step preparation to resize a picture utilizing PIL −
Algorithm
Step 1 - Stack the picture utilizing PIL's Image.open() function.
Step 2 - Indicate the specified width and tallness for the resized picture.
Step 3 - Conjure the resize() attribute on the picture question, passing the width and tallness as arguments.
Step 3 - Save the resized picture utilizing the save() attribute.
Example
from PIL import Image # Set the location of the image to open a specific image image = Image.open('original_image.jpg') # Specify the desired width and height width = 800 height = 600 # Modify the image resized_image = image.resize((width, height)) # Store the resized image resized_image.save('resized_image.jpg')
Output
Approach 2: Image Cropping
Cropping permits us to extricate a particular parcel of a picture. PIL gives the crop() attribute, which empowers us to edit a picture based on characterized arranges. Let's investigate the calculation and steps for picture editing utilizing PIL −
Algorithm
Step 1 - Load the picture utilizing PIL's Image.open() function.
Step 2 - Indicate the facilitates for the cropping region (cleared out, upper, right, lower).
Step 3 - Conjure the crop() attribute on the picture protest, passing the arranges as contentions.
Step 4 - Save the edited picture utilizing the save() property.
Program
from PIL import Image # Load the image image = Image.open('original_image.jpg') # Specify the cropping coordinates left = 100 upper = 100 right = 400 lower = 400 # Crop the image cropped_image = image.crop((left, upper, right, lower)) # Store the resultant image cropped_image.save('cropped_image.jpg')
Output
Approach 3:
Rotating a picture is another commonly performed operation in picture handling. PIL offers the rotate() attribute, permitting us to turn a picture by an indicated point. Let's look at the calculation and steps for turning a picture utilizing PIL −
Algorithm
Step 1 - Stack the picture utilizing PIL's Image.open() function
Step 2 - Indicate the required rotation point
Step 3 - Conjure the rotate() attribute on the picture object, passing the point as an argument.
Step 4 - Save the turned picture utilizing the save() attribute
Example
from PIL import Image # Set the path of an image that needs to be opened image = Image.open('original_image.jpg') # Step 2: Specify the rotation angle angle = 90 # Step 3: Rotate the image rotated_image = image.rotate(angle) # Step 4: Save the resultant image rotated_image.save('rotated_image.jpg')
Output
Conclusion
In this article, we investigated three fundamental properties given by PIL: image resizing, cropping, and rotation. We secured the calculations, and step-by-step forms, and compared Python syntax for each approach. These traits, in conjunction with numerous others advertised by PIL, engage engineers to perform different image-processing errands productively. By leveraging the capabilities of PIL, you'll improve your Python ventures with effective picture control highlights.