Pillow Common Modules and Methods

Pillow provides rich functionality, including image opening, editing, saving, format conversion, etc.

This article will introduce Pillow's common modules and methods, and provide simple code examples.


Common Modules

Module NameFunction CategoryCore FunctionalityCommon Methods/AttributesTypical Application Scenarios
ImageCore ModuleBasic Image Operationsopen(), save(), resize(), crop(), rotate(), convert()Image loading/saving/resizing/format conversion
ImageDrawDrawing ModuleVector graphics drawingDraw(), line(), rectangle(), text(), ellipse()Add watermarks/draw shapes/annotate images
ImageFontFont SupportText renderingtruetype(), load_default(), getsize()Custom text styles/calculate text area
ImageFilterImage EffectsFilter processingBLUR, SHARPEN, EMBOSS, GaussianBlur()Image blur/sharpening/artistic effects
ImageEnhanceImage OptimizationQuality enhancementBrightness(), Contrast(), Color(), Sharpness()Brightness/contrast/saturation adjustment
ImageOpsAdvanced OperationsAutomation processinggrayscale(), flip(), mirror(), invert()Image inversion/mirror/forced grayscale
ImageChopsChannel OperationsMathematical operationsadd(), subtract(), multiply(), difference()Image compositing/difference detection/mask processing
ImageColorColor SystemColor conversiongetrgb(), getcolor(), colormapHEX/RGB/HSL color conversion
ImageGrabScreen CaptureScreenshot functionalitygrab(), grabclipboard()Screenshot/clipboard image acquisition
ImageStatData AnalysisPixel statisticsStat(), extrema(), mean(), rms()Image color analysis/brightness detection
ImagePalettePaletteIndexed color managementnew(), getcolor(), tobytes()8-bit color image processing/GIF optimization
ImagePathPath ProcessingVector pathsPath(), getbbox(), compact()Complex shape drawing (requires ImageDraw)
ImageSequenceAnimated ImagesFrame processingIterator(), all_frames()GIF animation decomposition/multi-frame TIFF processing
ImageMorphMorphologyImage morphingMorphOp(), apply(), get_on_pixels()Pattern dilation/erosion/structural analysis
ImageShowImage PreviewQuick displayshow(), register_viewer()Quickly view images during debugging
ImageTkGUI IntegrationTkinter supportPhotoImage(), BitmapImage()Display images in Tkinter interface
ImageQtGUI IntegrationQt supportImageQt(), toqimage()PyQt/PySide image display
ExifTagsMetadataEXIF readingTAGS, GPSTAGSRead camera parameters/GPS information
PSDrawPrint OutputPostScriptPSDraw(), begin_document()Generate vector files for printing
TiffTagsMetadataTIFF TagsTIFFTAGSProcess TIFF format metadata

Module Workflow

Advanced processing combinations:

  • Effects creation: ImageFilter + ImageEnhance
  • Image analysis: ImageStat + ImageChops
  • GUI development: ImageTk/ImageQt + ImageDraw

Professional domain support:

  • Scientific images: TIFFTags + ImageSequence
  • Print publishing: PSDraw + ImageColor
  • Computer vision: ImageMorph + ImageOps

Common Methods

PIL.ImageModule Common Methods

Method Description Example
Image.open() Open image file img = Image.open("example.jpg")
Image.save() Save image file img.save("output.png")
Image.show() Display image img.show()
Image.resize() Resize image new_img = img.resize((200, 200))
Image.rotate() Rotate image rotated_img = img.rotate(45)
Image.crop() Crop image cropped_img = img.crop((50, 50, 200, 200))

Example code:

Example

from PIL import Image

# Open image
img = Image.open("example.jpg")

# Resize and save
resized_img = img.resize((300, 300))
resized_img.save("resized_example.jpg")

# Rotate 90 degrees and display
rotated_img = img.rotate(90)
rotated_img.show()

PIL.ImageDrawModule Common Methods

Method Description Example
ImageDraw.Draw() Create drawing object draw = ImageDraw.Draw(img)
draw.line() Draw line draw.line([(0, 0), (100, 100)], fill="red", width=2)
draw.rectangle() Draw rectangle draw.rectangle([(50, 50), (150, 150)], outline="blue", width=3)
draw.text() Draw text draw.text((10, 10), "Hello", fill="black")

Example code:

Example

from PIL import Image, ImageDraw

# Create a blank image
img = Image.new("RGB", (300, 300), "white")
draw = ImageDraw.Draw(img)

# Draw a red line
draw.line([(0, 0), (300, 300)], fill="red", width=2)

# Draw a blue rectangle
draw.rectangle([(50, 50), (250, 250)], outline="blue", width=3)

# Add text
draw.text((100, 100), "Hello Pillow!", fill="black")

# Save image
img.save("draw_example.png")
img.show()

PIL.ImageFilterModule Common Methods

Method Description Example
ImageFilter.BLUR Blur filter img.filter(ImageFilter.BLUR)
ImageFilter.CONTOUR Contour filter img.filter(ImageFilter.CONTOUR)
ImageFilter.SHARPEN Sharpen filter img.filter(ImageFilter.SHARPEN)

Example code:

Example

from PIL import Image, ImageFilter

# Open image
img = Image.open("example.jpg")

# Apply blur filter
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.save("blurred_example.jpg")

# Apply sharpen filter
sharpened_img = img.filter(ImageFilter.SHARPEN)
sharpened_img.show()
Other Extensions