Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 706 of 855
Using OpenCV in Python to Cartoonize an Image
Image cartoonization is a popular computer vision technique that transforms regular photos into cartoon-style images. While many professional cartoonizer applications exist, most are paid software. Using OpenCV in Python, we can achieve this effect by combining bilateral filtering for color reduction and edge detection for bold outlines. Algorithm Overview The cartoonization process involves five key steps: Apply bilateral filter to reduce the color palette of the image Convert the original image to grayscale Apply median blur to reduce image noise in the grayscale image ...
Read MoreOpenCV Python Program to blur an image?
OpenCV is one of the best Python packages for image processing. Like signals carry noise attached to them, images too contain different types of noise mainly from the source itself (camera sensor). Python's OpenCV package provides ways for image smoothing, also called blurring. One of the most common techniques is using a Gaussian filter for image blurring, which smooths sharp edges while minimizing excessive blur. Syntax cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) Parameters src − Input image ksize − Gaussian kernel size as (width, height). Both values must be odd and positive sigmaX ...
Read MorePermutation and Combination in Python?
In this section, we are going to learn how to find permutation and combination of a given sequence using Python programming language. Python's itertools module provides built-in functions to generate permutations and combinations efficiently. A permutation is an arrangement of items where order matters, while a combination is a selection where order doesn't matter. Understanding Permutations vs Combinations Before diving into code, let's understand the difference: Permutation: Order matters. For items [A, B], permutations are (A, B) and (B, A) Combination: Order doesn't matter. For items [A, B], there's only one combination: (A, B) ...
Read MorePython program to Rearrange a string so that all same characters become d distance away
Given a string and an integer d, we need to rearrange the string such that all same characters are at least distance d apart from each other. If it's not possible to rearrange the string, we return an empty string. Problem Understanding The key challenge is to place characters with higher frequencies first, ensuring they have enough positions to maintain the required distance d. Example 1 str_input = "tutorialspoint" d = 3 # Result: "tiotiotalnprsu" # Same characters are at least 3 positions apart Example 2 str_input = "aaabc" d = ...
Read MorePython program to Find the first non-repeating character from a stream of characters?
In this article, we will find the first non-repeating character from a stream of characters. A non-repeating character is one that appears exactly once in the string. Let's say the following is our input ? thisisit The following should be our output displaying first non-repeating character ? h Using While Loop We will find the first non-repeating character by comparing each character with others using a while loop. This approach removes all occurrences of each character and checks if only one was removed ? # String my_str = "thisisit" ...
Read MorePython program to Count words in a given string?
Sometimes we need to count the number of words in a given string. Python provides several approaches to accomplish this task, from manual counting using loops to built-in methods like split(). Using For Loop This method counts words by detecting whitespace characters manually − test_string = "Python is a high level language. Python is interpreted language." total = 1 for i in range(len(test_string)): if test_string[i] == ' ' or test_string[i] == '' or test_string[i] == '\t': total = total + 1 print("Total ...
Read MoreIncrement and Decrement Operators in Python?
Python does not have unary increment/decrement operators (++/--) like C or Java. Instead, Python uses compound assignment operators for incrementing and decrementing values. Basic Increment and Decrement Operations To increment a value, use the += operator ? a = 5 a += 1 # Increment by 1 print(a) 6 To decrement a value, use the -= operator ? a = 5 a -= 1 # Decrement by 1 print(a) 4 Why Python Doesn't Have ++ and -- Operators Python follows the ...
Read MorePrint Single and Multiple variable in Python?
To display single and multiple variables in Python, use the print() function. For multiple variables, separate them with commas or use formatting methods. Variables are reserved memory locations to store values. When you create a variable, you reserve space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Print Single Variable in Python To display a single variable, pass it directly to the print() function ? Example # Printing single values print(5) print(10) # Printing variables name = ...
Read MoreSwap two variables in one line in using Python?
Python provides several ways to swap two variables in a single line. The most Pythonic approach uses tuple unpacking, but you can also use arithmetic or bitwise operators ? Using Tuple Unpacking (Recommended) Python's tuple unpacking allows you to swap variables in one elegant line using the comma operator ? a = 5 b = 10 print("Before swapping:") print("a =", a) print("b =", b) # Swap two variables in one line using tuple unpacking a, b = b, a print("After swapping:") print("a =", a) print("b =", b) Before swapping: a ...
Read MoreWhen to use yield instead of return in Python?
In Python, yield and return serve different purposes. The return statement terminates function execution and returns a value, while yield pauses execution and creates a generator that can resume from where it left off. Understanding return Statement The return statement stops function execution immediately and optionally returns a value to the caller ? def get_numbers(): print("Starting function") return [1, 2, 3, 4, 5] print("This line never executes") numbers = get_numbers() print(numbers) Starting function [1, 2, 3, 4, 5] ...
Read More