Fundamentals 7 min read

Adding a Christmas Hat, Snowfall, and Tree Effects to Images with Python OpenCV and Pillow

This tutorial demonstrates how to use Python's OpenCV and Pillow libraries to detect faces and overlay a Christmas hat on images, create dynamic snowfall effects, and generate a stylized Christmas tree composition, providing complete code snippets and installation instructions.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Adding a Christmas Hat, Snowfall, and Tree Effects to Images with Python OpenCV and Pillow

This guide shows how to add a Christmas hat to a portrait image using Python's OpenCV library for face detection and Pillow for image manipulation, followed by examples for generating snowfall animations and a Christmas‑tree graphic.

import cv2
from PIL import Image

# Load images
input_image_path = "input.jpg"  # replace with your image
hat_image_path = "hat.png"      # replace with your hat image
original_image = cv2.imread(input_image_path)
hat_image = cv2.imread(hat_image_path)

# Face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

for (x, y, w, h) in faces:
    hat_height, hat_width, _ = hat_image.shape
    resized_hat = cv2.resize(hat_image, (w, int(hat_height * 0.7)), interpolation=cv2.INTER_AREA)
    roi_color = original_image[y:y + int(hat_height * 0.7), x:x + w]
    alpha_s = resized_hat[:, :, 3] / 255.0
    alpha_l = 1.0 - alpha_s
    for c in range(0, 3):
        roi_color[:, :, c] = (alpha_s * resized_hat[:, :, c] + alpha_l * roi_color[:, :, c])
    original_image[y:y + int(hat_height * 0.7), x:x + w] = roi_color

cv2.imshow("Result", original_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
result_image = Image.fromarray(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
result_image.save("output.jpg")

Install the required libraries with pip install opencv-python Pillow and replace input.jpg and hat.png with your own file paths before running the script.

For a snowfall effect, the following Pillow‑based code creates an image with randomly placed white snowflakes:

from PIL import Image, ImageDraw
import random

def draw_snowflake(draw, size, angle):
    x1, y1 = 0, 0
    x2, y2 = size[0], size[1]
    x3, y3 = (size[0] // 2), (size[1] * 2)
    draw.polygon([(x1, y1), (x2, y2), (x3, y3)], fill="white")
    draw.rotate(angle)
    draw.line((x1, y1, x2, y2), fill="white", width=2)
    draw.line((x1, y1, x3, y3), fill="white", width=2)
    draw.line((x2, y2, x3, y3), fill="white", width=2)

def create_snowfall_image(input_image_path, output_image_path, snowflakes_count):
    image = Image.open(input_image_path)
    width, height = image.size
    new_image = Image.new("RGB", (width, height), "black")
    draw = ImageDraw.Draw(new_image)
    new_image.paste(image, (0, 0))
    for i in range(snowflakes_count):
        size = (random.randint(8, 16), random.randint(8, 16))
        angle = random.randint(-45, 45)
        x = random.randint(0, width - size[0])
        y = random.randint(0, height - size[1])
        draw_snowflake(draw, size, angle)
        draw.translate(x, y)
    new_image.save(output_image_path)

if __name__ == "__main__":
    create_snowfall_image("input.jpg", "output.jpg", 50)

Install Pillow with pip install Pillow before executing the snowfall script.

To generate a stylized Christmas‑tree image, use the following Pillow code that draws multiple colored triangles and applies a blur filter:

from PIL import Image, ImageDraw, ImageFilter
import random

def draw_triangle(draw, size, color):
    x1, y1 = (size[0] // 2) - (size[1] // 2), 0
    x2, y2 = (size[0] // 2) + (size[1] // 2), 0
    x3, y3 = (size[0] // 2), size[1]
    draw.polygon([(x1, y1), (x2, y2), (x3, y3)], fill=color)

def create_christmas_tree_image(input_image_path, output_image_path, triangle_size):
    image = Image.open(input_image_path)
    width, height = image.size
    new_image = Image.new("RGB", (width * 2, height), "white")
    draw = ImageDraw.Draw(new_image)
    new_image.paste(image, (width // 2, 0))
    for i in range(triangle_size):
        size = (i * 4 + 8, i * 2 + 4)
        color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        draw_triangle(draw, size, color)
    new_image = new_image.filter(ImageFilter.BLUR)
    new_image.save(output_image_path)

if __name__ == "__main__":
    create_christmas_tree_image("input.jpg", "output.jpg", 8)

Again, ensure Pillow is installed and replace the placeholder file names with your actual image paths before running.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TutorialOpenCVpillowimage-processingchristmas
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.