Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Common Pillow Modules and Methods

Common Pillow Modules and Methods

Pillow provides a rich set of features, including image opening, editing, saving, format conversion, and more.

This article introduces Pillow's common modules and methods and provides simple code examples.


Common modules

Module name Function category Core Features Common methods/attributes Typical use cases
Image Core modules Basic Image Operations open(), save(), resize(), crop(), rotate(), convert() Image loading/saving/resizing/format conversion
ImageDraw Drawing modules Vector graphics drawing Draw(), line(), rectangle(), text(), ellipse() Adding watermarks/drawing shapes/annotating images
ImageFont Font support Text rendering truetype(), load_default(), getsize() Custom text styles/calculating text areas
ImageFilter Image effects Filter processing BLUR, SHARPEN, EMBOSS, GaussianBlur() Image blurring/sharpening/artistic effects
ImageEnhance Image optimization Quality enhancement Brightness(), Contrast(), Color(), Sharpness() Brightness/contrast/saturation adjustment
ImageOps Advanced operations Automate the process grayscale(), flip(), mirror(), invert() Image inversion/mirroring/forced grayscale
ImageChops Channel operations Math operations add(), subtract(), multiply(), difference() Image composition/difference detection/mask processing
ImageColor Color systems Color conversion getrgb(), getcolor(), colormap HEX/RGB/HSL color conversion
ImageGrab Screen capture Screenshot functionality grab(), grabclipboard() Screen capture/clipboard image retrieval
ImageStat Data analysis Pixel statistics Stat(), extrema(), mean(), rms() Image color analysis/brightness detection
ImagePalette Palette Indexed color management new(), getcolor(), tobytes() 8-bit color image processing/GIF optimization
ImagePath Path handling Vector paths Path(), getbbox(), compact() Complex shape drawing (requires ImageDraw)
ImageSequence Dynamic images Frame processing Iterator(), all_frames() GIF animation decomposition/multi-frame TIFF processing
ImageMorph Morphology Image deformation MorphOp(), apply(), get_on_pixels() Pattern dilation/erosion/structural analysis
ImageShow Image preview Quick display show(), register_viewer() Quickly view images during debugging
ImageTk GUI integration Tkinter support PhotoImage(), BitmapImage() Display images in the Tkinter interface
ImageQt GUI integration Qt support ImageQt(), toqimage() PyQt/PySide image display
ExifTags Metadata EXIF reading TAGS, GPSTAGS Reading camera parameters/GPS information
PSDraw Print output PostScript PSDraw(), begin_document() Generate vector files for printing
TiffTags Metadata TIFF tags TIFFTAGS Handling TIFF format metadata

Module Workflow

Advanced processing combinations:

** Professional Domain Support:**


// 2022-01-01 00:00:00 GMT

PIL.Image Common Module Methods

Method Description Example
Image.open() Open the image file img = Image.open("example.jpg")
Image.save() Save an image file img.save("output.png")
Image.show() Displays an image img.show()
Image.resize() Resize an image new_img = img.resize((200, 200))
Image.rotate() Rotate an image rotated_img = img.rotate(45)
Image.crop() Crop the 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.ImageDraw Common Module Methods

Method Description Example
ImageDraw.Draw() Create a drawing object draw = ImageDraw.Draw(img)
draw.line() Draw a line draw.line([(0, 0), (100, 100)], fill="red", width=2)
draw.rectangle() Draw a rectangle draw.rectangle([(50, 50), (150, 150)], outline="blue", width=3)
draw.text() Draw the 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 the image

img.

save

(

"draw_example.png"

)

img.

show

(

)


PIL.ImageFilter Common Module 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 a sharpening filter

sharpened_img

=

img.

filter

(

ImageFilter.

SHARPEN

)

sharpened_img.

show

(

)