TGViewer
Machine Learning with Python Machine Learning with Python @codeprogrammer · 68.6K subscribers
Post #4376 4.66K
💡 Top 50 Pillow Operations for Image Processing

I. File & Basic Operations

• Open an image file.
from PIL import Image
img = Image.open("image.jpg")

• Save an image.
img.save("new_image.png")

• Display an image (opens in default viewer).
img.show()

• Create a new blank image.
new_img = Image.new("RGB", (200, 100), "blue")

• Get image format (e.g., 'JPEG').
print(img.format)

• Get image dimensions as a (width, height) tuple.
width, height = img.size

• Get pixel format (e.g., 'RGB', 'L' for grayscale).
print(img.mode)

• Convert image mode.
grayscale_img = img.convert("L")

• Get a pixel's color value at (x, y).
r, g, b = img.getpixel((10, 20))

• Set a pixel's color value at (x, y).
img.putpixel((10, 20), (255, 0, 0))


II. Cropping, Resizing & Pasting

• Crop a rectangular region.
box = (100, 100, 400, 400)
cropped_img = img.crop(box)

• Resize an image to an exact size.
resized_img = img.resize((200, 200))

• Create a thumbnail (maintains aspect ratio).
img.thumbnail((128, 128))

• Paste one image onto another.
img.paste(another_img, (50, 50))


III. Rotation & Transformation

• Rotate an image (counter-clockwise).
rotated_img = img.rotate(45, expand=True)

• Flip an image horizontally.
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)

• Flip an image vertically.
flipped_img = img.transpose(Image.FLIP_TOP_BOTTOM)

• Rotate by 90, 180, or 270 degrees.
img_90 = img.transpose(Image.ROTATE_90)

• Apply an affine transformation.
transformed = img.transform(img.size, Image.AFFINE, (1, 0.5, 0, 0, 1, 0))


IV. ImageOps Module Helpers

• Invert image colors.
from PIL import ImageOps
inverted_img = ImageOps.invert(img)

• Flip an image horizontally (mirror).
mirrored_img = ImageOps.mirror(img)

• Flip an image vertically.
flipped_v_img = ImageOps.flip(img)

• Convert to grayscale.
grayscale = ImageOps.grayscale(img)

• Colorize a grayscale image.
colorized = ImageOps.colorize(grayscale, black="blue", white="yellow")

• Reduce the number of bits for each color channel.
posterized = ImageOps.posterize(img, 4)

• Auto-adjust image contrast.
adjusted_img = ImageOps.autocontrast(img)

• Equalize the image histogram.
equalized_img = ImageOps.equalize(img)

• Add a border to an image.
bordered = ImageOps.expand(img, border=10, fill='black')


V. Color & Pixel Operations

• Split image into individual bands (e.g., R, G, B).
r, g, b = img.split()

• Merge bands back into an image.
merged_img = Image.merge("RGB", (r, g, b))

• Apply a function to each pixel.
brighter_img = img.point(lambda i: i * 1.2)

• Get a list of colors used in the image.
colors = img.getcolors(maxcolors=256)

• Blend two images with alpha compositing.
# Both images must be in RGBA mode
blended = Image.alpha_composite(img1_rgba, img2_rgba)


VI. Filters (ImageFilter)
  • ❤ 5
More from @codeprogrammer
  1. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  2. Oct 9, 2026One of the key moments when I truly understood how transformers work: 🧠✨ "Stop thinking o…
  3. Oct 9, 2026🔖 Official PyTorch Tutorials This collection includes: 🫡 PyTorch fundamentals and torch.…
  4. Oct 7, 2026A distinctive bot for subscribing to courses on the most popular educational platforms acr…
  5. Oct 7, 2026Understanding algorithms changes the way you write code. 🧠 This playlist teaches you exac…
  6. Oct 6, 2026🛠 Built an AI app with Cursor, Claude Code or Lovable? Publish it on Zibby (zibby.run), a…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →