Project 27: Starfield Postcard ✨¶

Difficulty: 🟢 Beginner

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

← Back to index

Description¶

Paint a random starfield night sky and stamp a postcard caption onto a PNG.

Libraries & Modules¶

  • PIL.Image, ImageDraw
  • random

Python Concepts You'll Practice¶

Coordinate math, drawing primitives, image compositing

🎛️ Parameter Variations¶

  • STAR_COUNT, canvas size
  • CAPTION — postcard text
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") / "starfield_postcard"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\starfield_postcard

Step 1 — Sprinkle stars¶

In [3]:
from PIL import Image, ImageDraw
import random

# 🎛️ TWEAK THESE
WIDTH, HEIGHT = 900, 560
STAR_COUNT = 220
SEED = 11
CAPTION = "Greetings from CodeItAll"

random.seed(SEED)
img = Image.new("RGB", (WIDTH, HEIGHT), (8, 10, 28))
draw = ImageDraw.Draw(img)
for _ in range(STAR_COUNT):
    x, y = random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1)
    r = random.choice([1, 1, 1, 2, 2, 3])
    glow = random.randint(180, 255)
    draw.ellipse((x - r, y - r, x + r, y + r), fill=(glow, glow, min(255, glow + 20)))
print(f"Sky painted with {STAR_COUNT} stars")
display(img)
Sky painted with 220 stars

No description has been provided for this image

Step 2 — Stamp postcard caption¶

In [4]:
# banner
draw.rectangle((0, HEIGHT - 70, WIDTH, HEIGHT), fill=(250, 250, 250))
draw.text((24, HEIGHT - 48), CAPTION, fill=(20, 20, 20))
out = OUTPUT_DIR / "starfield_postcard.png"
img.save(out)
print(f"Saved postcard → {out}")
display(img)
Saved postcard → outputs\starfield_postcard\starfield_postcard.png
No description has been provided for this image

✅ Project complete¶

Check your output folder: outputs/starfield_postcard/

Next: Project 28: Maze Maker Mini · Index