Cartoon an image

 To cartoonize an image using Python, you can use the OpenCV library. Here's an example code that cartoonizes an image:


python
import cv2
import numpy as np

def cartoonize(image):
    # Convert image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply median blur to reduce image noise
    gray = cv2.medianBlur(gray, 5)
    
    # Adjust image contrast and brightness
    edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
    
    # Convert image to BGR color
    color = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
    
    # Apply bilateral filter to create a cartoon effect
    cartoon = cv2.bilateralFilter(color, 9, 300, 300)
    
    return cartoon

# Read input image
image = cv2.imread('input_image.jpg')

# Cartoonize the image
cartoonized_image = cartoonize(image)

# Display the original and cartoonized images
cv2.imshow("Original Image", image)
cv2.imshow("Cartoonized Image", cartoonized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()


Make sure to replace 'input_image.jpg' with the filename of the image you want to cartoonize. This code applies median blur, adaptive threshold, and bilateral filter to create a cartoon-like effect, and then displays the original and cartoonized images for comparison.

Comments

Popular posts from this blog

Ecommerce website

Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

Your task is to find the missing number.