Source Information¶
Created by:
Updated by: October 01, 2024 by Gloria Seo
Resources: https://matplotlib.org/
Goal¶
This notebook introduces the Pillow library, covering how to load, manipulate, and process images, while exploring its extensive file format support and powerful image processing features.
Image Processing¶
The Python Imaging Library (PILLOW) adds image processing capabilities to your Python interpreter.
This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.
The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.
This chapter will give you an introduction to PILLOW packages. Enjoy!
Required Modules for the Jupyter Notebook¶
Before running the notebook, we need to load the following modules.
Module: PIL , Image, ImageFilter, ImageEhance, Sys
from PIL import Image
import sys
from PIL import ImageFilter
from PIL import ImageEnhance
Importing an image¶
Load an image using the Image.open command and setting it to a variable.
im = Image.open("image.jpg")
Now you can examine the image specs using the print function.
print(im.format, im.size, im.mode)
JPEG (317, 301) RGB
The format shows the file type or source of the image. The size prints out the image size in pixels. The mode shows the coloring mode which in this case should be RGB.
Now lets print out the image using the show command.
im.show()
This should open up a new tab that has the image in it. Show however is very buggy at the moment. So we will only use it a few times. You can try to open up the image in draft mode which is an opened but not loaded image which may be useful for some situations.
with Image.open("image.jpg") as im:
print("original =", im.mode, im.size)
im.draft("L", (100, 100))
print("draft =", im.mode, im.size)
original = RGB (317, 301) draft = L (159, 151)
Manipulating Images¶
We will now get to manipulating image types. First lets look at identifying your image type.
for infile in sys.argv[1:]:
try:
with Image.open("image.jpg") as im:
print(infile, im.format, "%dx%d" % im.size, im.mode)
except OSError:
pass
-f JPEG 317x301 RGB /home/amehrotra1/.jupyter/runtime/kernel-8b48e399-1bb3-4281-a72c-88a6e6f0d9d1.json JPEG 317x301 RGB
Cropping images is with the crop command. Define the pixel size and it will crop.
im = Image.open("Blank_world_map-6.png")
box = (10, 10, 40, 40)
region = im.crop(box)
region.show()
The Filter method allows you to put filters over you images. So you can automate image filtering if you iterate through a folder.
im = Image.open("image.jpg")
out = im.filter(ImageFilter.BLUR)
out.show()
Image enhancing can be done with the ImageEnhance function.
im = Image.open("face.jpg")
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")
Col = ImageEnhance.Color(im)
Con = ImageEnhance.Contrast(im)
Bright = ImageEnhance.Brightness(im)
Sharp = ImageEnhance.Sharpness(im)
Those are all of the functions you can edit for the image using PIL.
You can scale images using point transformations.
out = im.point(lambda i: i * 1.2)
im.show()
out.show()
When you compare both images. You can see one image is clearly large than the other.
Submit Ticket¶
If you find anything that needs to be changed, edited, or if you would like to provide feedback or contribute to the notebook, please submit a ticket by contacting us at:
Email: consult@sdsc.edu
We appreciate your input and will review your suggestions promptly!