Project 1: Pixel Palette Pop 🎨¶

Difficulty: 🟢 Beginner

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/pixel_palette_pop/.

← Back to index

Description¶

Generate vibrant gradient artwork and save it as a PNG. You'll learn how images are just grids of colored pixels.

Libraries & Modules¶

  • PIL (Pillow): Image, ImageDraw
  • Built-in: math, Path

Python Concepts You'll Practice¶

Variables, loops, RGB tuples, saving files, functions

🎛️ Parameter Variations¶

  • WIDTH, HEIGHT — try 400×400 vs 1200×800
  • COLOR_A, COLOR_B — swap hex colors (#FF6B6B, #4ECDC4, #FFE66D)
  • STEPS — more steps = smoother gradient (50 vs 200)
  • DIRECTION — "horizontal", "vertical", or "diagonal"
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "pixel_palette_pop"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\pixel_palette_pop

Step 1 — Define your canvas parameters¶

In [3]:
from PIL import Image, ImageDraw

# 🎛️ TWEAK THESE
WIDTH, HEIGHT = 800, 500
COLOR_A = (255, 107, 107)   # coral red (R, G, B)
COLOR_B = (78, 205, 196)    # teal
STEPS = 120
DIRECTION = "horizontal"    # "horizontal" | "vertical" | "diagonal"

def lerp(a, b, t):
    return tuple(int(a[i] + (b[i] - a[i]) * t) for i in range(3))

img = Image.new("RGB", (WIDTH, HEIGHT))
draw = ImageDraw.Draw(img)
print(f"Canvas: {WIDTH}×{HEIGHT}, direction={DIRECTION}")
Canvas: 800×500, direction=horizontal

Step 2 — Paint the gradient pixel by pixel¶

In [4]:
for i in range(STEPS):
    color = lerp(COLOR_A, COLOR_B, i / max(STEPS - 1, 1))
    if DIRECTION == "horizontal":
        x0 = int(i * WIDTH / STEPS)
        x1 = int((i + 1) * WIDTH / STEPS)
        draw.rectangle([x0, 0, x1, HEIGHT], fill=color)
    elif DIRECTION == "vertical":
        y0 = int(i * HEIGHT / STEPS)
        y1 = int((i + 1) * HEIGHT / STEPS)
        draw.rectangle([0, y0, WIDTH, y1], fill=color)
    else:  # diagonal bands
        offset = int(i * (WIDTH + HEIGHT) / STEPS)
        draw.polygon([(0, offset), (offset, 0), (WIDTH, offset - WIDTH), (WIDTH, HEIGHT), (0, HEIGHT)], fill=color)

out = OUTPUT_DIR / "pixel_palette_pop.png"
img.save(out)
print(f"Saved: {out.resolve()}")
display(img)
Saved: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\pixel_palette_pop\pixel_palette_pop.png
No description has been provided for this image

✅ Project complete¶

Check your output folder: outputs/pixel_palette_pop/

Next: Project 2: ASCII Art Machine · Index