Pillow Module Method
Pillow provides rich features, 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 Name | Function Category | Core Functions | Common Methods/Attributes | Typical Application Scenarios |
| --- | --- | --- | --- | --- |
| **Image** | Core Module | Basic Image Operations | `open()`, `save()`, `resize()`, `crop()`, `rotate()`, `convert()` | Image Loading/Saving/Resizing/Format Conversion |
| **ImageDraw** | Drawing Module | Vector Graphics Drawing | `Draw()`, `line()`, `rectangle()`, `text()`, `ellipse()` | Adding Watermarks/Drawing Graphics/Image Annotation |
| **ImageFont** | Font Support | Text Rendering | `truetype()`, `load_default()`, `getsize()` | Custom Text Styles/Text Area Calculation |
| **ImageFilter** | Image Effects | Filter Processing | `BLUR`, `SHARPEN`, `EMBOSS`, `GaussianBlur()` | Image Blur/Sharpening/Artistic Effects |
| **ImageEnhance** | Image Enhancement | Quality Enhancement | `Brightness()`, `Contrast()`, `Color()`, `Sharpness()` | Brightness/Contrast/Saturation Adjustment |
| **ImageOps** | Advanced Operations | Automated Processing | `grayscale()`, `flip()`, `mirror()`, `invert()` | Image Inversion/Mirror/Force Grayscale |
| **ImageChops** | Channel Operations | Mathematical Operations | `add()`, `subtract()`, `multiply()`, `difference()` | Image Compositing/Difference Detection/Mask Processing |
| **ImageColor** | Color System | Color Conversion | `getrgb()`, `getcolor()`, `colormap` | HEX/RGB/HSL Color Conversion |
| **ImageGrab** | Screen Capture | Screenshot Function | `grab()`, `grabclipboard()` | Screen Capture/Clipboard Image Acquisition |
| **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 Processing | Vector Path | `Path()`, `getbbox()`, `compact()` | Complex Shape Drawing (needs to be used with ImageDraw) |
| **ImageSequence** | Dynamic Image | Frame Processing | `Iterator()`, `all_frames()` | GIF Animation Decomposition/Multi-frame TIFF Processing |
| **ImageMorph** | Morphology | Image Deformation | `MorphOp()`, `apply()`, `get_on_pixels()` | Pattern Dilation/Erosion/Structure Analysis |
| **ImageShow** | Image Preview | Quick Display | `show()`, `register_viewer()` | Quick Image Viewing During Debugging |
| **ImageTk** | GUI Integration | Tkinter Support | `PhotoImage()`, `BitmapImage()` | Displaying Images in 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()` | Generating Print-ready Vector Files |
| **TiffTags** | Metadata | TIFF Tags | `TIFFTAGS` | Processing TIFF Format Metadata |
### Module Working Process
!(#)
**Advanced Processing Combinations:**
* Special Effects: ImageFilter + ImageEnhance
* Image Analysis: ImageStat + ImageChops
* GUI Development: ImageTk/ImageQt + ImageDraw
**Professional Domain Support:**
* Scientific Imaging: TIFFTags + ImageSequence
* Print Publishing: PSDraw + ImageColor
* Computer Vision: ImageMorph + ImageOps
* * *
## Common Methods
### `PIL.Image` Module 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:**
## Instance
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` Module 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:**
## Instance
from PIL import Image, ImageDraw
# Create a Blank Image
img = Image.new("RGB",(300,300),"white")
draw = ImageDraw
YouTip