Complete reference for pixel-level image manipulation.
img.getpixel(x, y)Get color of a single pixel.
Parameters:
x (int): X coordinate (0-based)y (int): Y coordinate (0-based)Returns: Tuple[int, int, int, int] - (R, G, B, A)
Example:
# Get pixel at position (100, 50)
r, g, b, a = img.getpixel(100, 50)
print(f"Pixel color: R={r}, G={g}, B={b}, A={a}")
# Check if pixel is red
if r > 200 and g < 50 and b < 50:
print("This pixel is red!")
Notes:
img.putpixel(x, y, color)Set color of a single pixel.
Parameters:
x (int): X coordinatey (int): Y coordinatecolor (Tuple[int, int, int, int]): RGBA colorReturns: Image
Example:
# Set pixel to red
img = img.putpixel(100, 50, (255, 0, 0, 255))
# Draw single pixels to create pattern
for i in range(100):
img = img.putpixel(i, i, (255, 0, 0, 255)) # Diagonal line
Note: For setting many pixels, consider using drawing functions instead.
img.histogram()Get histogram of pixel values.
Parameters: None
Returns: Tuple[List[int], List[int], List[int], List[int]]
Example:
r_hist, g_hist, b_hist, a_hist = img.histogram()
# Find brightest red value
max_red = r_hist.index(max(r_hist))
print(f"Most common red value: {max_red}")
# Check alpha usage
if sum(a_hist[:-1]) == 0:
print("Image has no transparency")
Use cases:
img.dominant_color()Find the most common color in the image.
Parameters: None
Returns: Tuple[int, int, int, int] - (R, G, B, A)
Example:
r, g, b, a = img.dominant_color()
print(f"Dominant color: RGB({r}, {g}, {b})")
# Use for background detection
if r > 240 and g > 240 and b > 240:
print("Image has white background")
img.average_color()Calculate average color across entire image.
Parameters: None
Returns: Tuple[int, int, int, int] - (R, G, B, A)
Example:
r, g, b, a = img.average_color()
print(f"Average color: RGB({r}, {g}, {b})")
# Detect overall image tone
brightness = (r + g + b) / 3
if brightness > 180:
print("Bright image")
elif brightness < 75:
print("Dark image")
img.replace_color(target_color, replacement_color, tolerance)Replace all pixels of one color with another.
Parameters:
target_color (Tuple[int, int, int, int]): Color to replacereplacement_color (Tuple[int, int, int, int]): New colortolerance (int): Color matching tolerance (0-255)
Returns: Image
Example:
# Replace exact white with transparent
img = img.replace_color(
(255, 255, 255, 255), # White
(0, 0, 0, 0), # Transparent
tolerance=10
)
# Replace red with blue (with tolerance)
img = img.replace_color(
(255, 0, 0, 255), # Red
(0, 0, 255, 255), # Blue
tolerance=30
)
# Remove green screen
img = img.replace_color(
(0, 255, 0, 255), # Green
(0, 0, 0, 0), # Transparent
tolerance=50
)
Tolerance explained:
tolerance=0: Only replaces exact matchestolerance=50: Replaces similar colorstolerance=255: Replaces all colorsimg.threshold(threshold_value)Apply threshold to create binary (black/white) image.
Parameters:
threshold_value (int): Threshold (0-255)
Returns: Image
Example:
# Basic threshold
binary = img.threshold(128)
# Dark threshold (more white)
high_thresh = img.threshold(200)
# Light threshold (more black)
low_thresh = img.threshold(50)
Use cases:
img.posterize(levels)Reduce number of color levels (posterization effect).
Parameters:
levels (int): Number of color levels per channel (2-256)
Returns: Image
Example:
# Strong posterization (pop art effect)
poster = img.posterize(4)
# Mild posterization
mild = img.posterize(16)
# Extreme (2 levels = pure black/white per channel)
extreme = img.posterize(2)
# Find all red pixels
red_pixels = []
for y in range(img.height):
for x in range(img.width):
r, g, b, a = img.getpixel(x, y)
if r > 200 and g < 50 and b < 50:
red_pixels.append((x, y))
print(f"Found {len(red_pixels)} red pixels")
# Darken bright pixels
result = img
for y in range(img.height):
for x in range(img.width):
r, g, b, a = img.getpixel(x, y)
brightness = (r + g + b) / 3
if brightness > 200:
# Darken by 50
new_color = (max(0, r-50), max(0, g-50), max(0, b-50), a)
result = result.putpixel(x, y, new_color)
β οΈ Warning: Pixel-by-pixel operations are slow. Use built-in filters when possible!
| Operation | Speed | When to Use |
|---|---|---|
replace_color() |
β‘β‘β‘ Fast | Color replacement |
threshold() |
β‘β‘β‘ Fast | Binary conversion |
posterize() |
β‘β‘β‘ Fast | Reduce colors |
histogram() |
β‘β‘ Medium | Analysis |
dominant_color() |
β‘β‘ Medium | Find main color |
average_color() |
β‘β‘ Medium | Overall tone |
getpixel() single |
β‘β‘β‘ Fast | Single pixel |
getpixel() loop |
β οΈ Slow | Avoid if possible |
putpixel() single |
β‘β‘β‘ Fast | Single pixel |
putpixel() loop |
β οΈ Slow | Use filters instead |
# Use built-in operations
binary = img.threshold(128)
replaced = img.replace_color(old, new, 30)
# Single pixel access is fine
pixel = img.getpixel(100, 100)
# Get statistics
avg = img.average_color()
# Don't loop pixels for effects (use filters instead)
for y in range(img.height):
for x in range(img.width):
img = img.putpixel(x, y, some_color) # SLOW!
# Use this instead:
img = img.replace_color(target, replacement, tolerance)
See Also: