Programming Articles

Page 410 of 2547

Loading and displaying an image using the Pillow library

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 281 Views

In this program, we will read or load an image using the Pillow library. The Pillow library provides the Image.open() method that takes the file path or filename as a string parameter. To display the image, we use the show() function which opens the image in the default image viewer. Installing Pillow First, install the Pillow library if you haven't already ? pip install Pillow Basic Image Loading and Display Here's how to load and display an image using Pillow ? from PIL import Image import io import base64 # ...

Read More

How to overplot a line on a scatter plot in Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 14K+ Views

Overplotting a line on a scatter plot combines scattered data points with a trend line or reference line. This technique is useful for showing relationships, trends, or theoretical models alongside actual data points. Basic Approach Create the scatter plot first using scatter(), then add the line using plot() on the same axes ? import matplotlib.pyplot as plt import numpy as np # Generate sample data x_data = np.linspace(0, 10, 20) y_data = 2 * x_data + 1 + np.random.normal(0, 2, 20) # Linear with noise # Create scatter plot plt.figure(figsize=(8, 6)) plt.scatter(x_data, y_data, ...

Read More

Barchart with vertical labels in Python/Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 10K+ Views

When creating bar charts in Python using Matplotlib, you can rotate axis labels to improve readability, especially when dealing with long label names. The xticks() function with the rotation parameter allows you to set labels vertically or at any angle. Basic Bar Chart with Vertical Labels Here's how to create a bar chart with vertical x-axis labels − from matplotlib import pyplot as plt bars_heights = [14, 8, 10] bars_label = ["A label", "B label", "C label"] plt.bar(range(len(bars_label)), bars_heights) plt.xticks(range(len(bars_label)), bars_label, rotation='vertical') plt.show() Different Rotation Angles You can specify custom angles ...

Read More

Downsampling an image using OpenCV

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 8K+ Views

In this program, we will down sample an image. Downsampling is decreasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming out of an image. We will use the pyrDown() function in the OpenCV library to complete this task. What is Image Downsampling? Image downsampling reduces the number of pixels in an image by decreasing its spatial resolution. OpenCV's pyrDown() function performs Gaussian pyramid downsampling, which smooths the image and reduces its size by half in each dimension. Algorithm Step 1: Read the image using cv2.imread() Step ...

Read More

Calculating the mean of all pixels for each band in an image using the Pillow library

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 2K+ Views

In this tutorial, we will calculate the mean pixel values for each color channel in an image using Python's Pillow library. RGB images have three channels (Red, Green, Blue), so we'll get a list of three mean values representing the average intensity of each color channel. Original Image Algorithm Step 1: Import the Image and ImageStat libraries from PIL Step 2: Open the target image file Step 3: Create an ImageStat.Stat object from the image Step 4: Use the mean property to get average pixel values for each channel Example Here's ...

Read More

How to save a figure remotely with pylab in Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 624 Views

Using the savefig() method of the pyplot package, we can save matplotlib figures to remote locations or specific directories by providing the complete file path. Setting Up the Backend When saving figures without displaying them, it's recommended to use the 'Agg' backend, which is designed for file output ? import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt Basic Figure Saving Create a simple plot and save it to the current directory ? import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt # Create a simple plot plt.plot([1, 2, 3, ...

Read More

Performing white TopHat operation on images using OpenCV

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 783 Views

In this tutorial, we will perform the TopHat operation on images using OpenCV. TopHat operation is a morphological transformation that extracts small elements and details from images by highlighting bright objects on dark backgrounds. We will use the cv2.morphologyEx() function with the cv2.MORPH_TOPHAT operation. What is TopHat Operation? TopHat (also called White TopHat) is defined as the difference between the input image and its opening. It highlights small bright details that are smaller than the structuring element ? TopHat = Original Image - Opening Original ...

Read More

Putting a newline in Matplotlib label with TeX in Python

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 5K+ Views

When creating plots with Matplotlib, you may need to add newlines to axis labels for better formatting. This is easily achieved using the escape character in your label strings. Basic Example with Newlines in Labels Here's how to add newlines to both X and Y axis labels ? import matplotlib.pyplot as plt # Create simple data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Plot with newlines in labels plt.plot(x, y, 'b-', linewidth=2) plt.ylabel("Y-axis with newline") plt.xlabel("X-axis with newline") plt.title("Plot with Newlines in Labels") ...

Read More

Generating a movie from Python without saving individual frames to files

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 455 Views

Creating animated movies in Python using matplotlib's FuncAnimation allows you to generate smooth animations without saving individual frames to disk. This approach is memory-efficient and perfect for real-time particle simulations. Key Concepts The animation works by repeatedly calling an update function that modifies particle positions and returns updated plot elements. FuncAnimation handles the timing and display automatically. Steps to Create the Animation Initialize particles with position, velocity, force, and size properties Create a matplotlib figure with specified dimensions Add axes with appropriate x and y limits Create initial scatter plot for particle positions Define an ...

Read More

How to prevent numbers being changed to exponential form in Python Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 1K+ Views

When plotting large numbers in Matplotlib, the axis labels often switch to scientific notation (exponential form) automatically. You can prevent this by using the ticklabel_format() with style='plain' parameter. Syntax plt.ticklabel_format(style='plain') The style='plain' parameter turns off scientific notation and displays numbers in their regular decimal format. Example Here's how to prevent exponential notation when plotting data ? import matplotlib.pyplot as plt # Plot data that would normally trigger scientific notation plt.plot([1, 2, 3, 4, 5], [11000, 12000, 13000, 14000, 15000]) # Prevent scientific notation on y-axis plt.ticklabel_format(style='plain') plt.title('Numbers in ...

Read More
Showing 4091–4100 of 25,466 articles
« Prev 1 408 409 410 411 412 2547 Next »
Advertisements