Calculate Median of Pixels for Each Band Using Pillow Library

Prasad Naik
Updated on 17-Mar-2021 08:16:31

886 Views

In this program, we will calculate the MEDIAN of all the pixels in each channel using the Pillow library. There are a total 3 channels in an image and therefore we will get a list of three values.Original ImageAlgorithmStep 1: Import the Image and ImageStat libraries. Step 2: Open the image. Step 3: Pass the image to the stat function of the imagestat class. Step 4: Print the median of the pixels.Example Codefrom PIL import Image, ImageStat im = Image.open('image_test.jpg') stat = ImageStat.Stat(im) print(stat.median)Output[41, 43, 40]

Generate a Movie from Python without Saving Individual Frames

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:14:06

374 Views

Using the FuncAnimation method, we can create a film. We will create a user-defined method, update, to keep on changing the position of particles and at the end, the method would return the scatter instance.StepsGet the particles initial position, velocity, force, and size.Create a new figure, or activate an existing figure with figsize = (7, 7).Add an axes to the current figure and make it the current axes, with xlim and ylim.Plot the scatter for initial position of the particles.Makes an animation by repeatedly calling a function *func*. We can pass a user-defined method that helps to change the position ... Read More

Eroding an Image Using the OpenCV Function Erode

Prasad Naik
Updated on 17-Mar-2021 08:05:10

570 Views

In this program, we will erode an image using the OpenCV function erode(). Erosion of image means to shrink the image. If any of the pixels in a kernel is 0, then all the pixels in the kernel are set to 0. One condition before applying an erosion function on image is that the image should be a grayscale image.Original ImageAlgorithmStep 1: Import cv2 Step 2: Import numpy. Step 3: Read the image using imread(). Step 4: Define the kernel size using numpy ones. Step 5: Pass the image and kernel to the erode function. Step 6: Display the output.Example ... Read More

Turn On Minor Ticks Only on the Y-Axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:02:26

2K+ Views

First, we can create fig, ax using subplots() and then, we can plot the lines. After that, using ax.yaxis.set_minor_locator(tck.AutoMinorLocator()), we can turn on the minor ticks.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Plot the line using two lists.Set the locator of the minor ticker.Dynamically find minor tick positions based on the positions of major ticks. The scale must be linear with major ticks evenly spaced.Using plt.show() method, we can show the figure.Exampleimport matplotlib.pyplot as plt import matplotlib.ticker as tck fig, ax = plt.subplots() plt.plot([0, 2, 4], [3, 6, 1]) ax.yaxis.set_minor_locator(tck.AutoMinorLocator()) plt.show()OutputRead More

Zoom Subplots Together in Matplotlib Pyplot

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:01:02

3K+ Views

We can use the attribute sharex = "ax1", and then, use the subplot method to zoom the subplots together.StepsAdd a subplot to the current figure with (nrow = 1, ncols = 2, index = 1).Add line on the current subplot with (nrow = 1, ncols = 2, index = 1).Add a subplot to the current figure with (nrow = 1, ncols = 2, index = 2).Add line on the current subplot with (nrow = 1, ncols = 2, index = 2), where sharex can help to share the x or y `~matplotlib.axis` with sharex and/or sharey. The axis will have ... Read More

Reading and Displaying Images Using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:00:05

2K+ Views

In this article, we will learn how to read and display images using the OpenCV library.OpenCV is a library of programming functions mainly aimed at real time computer vision. Before reading an image, make sure that the image is in the same directory as your program.AlgorithmStep 1: Import OpenCV. Step 2: Read an image using imread(). Step 3: Display the image using imshow().Example Codeimport cv2 as cv image = cv.imread ('ronaldo.jpg') cv.imshow('Cristiano Ronaldo', image)Output

Write an Image to a File using OpenCV

Prasad Naik
Updated on 17-Mar-2021 07:59:02

488 Views

In this program, we will write an image or save an image to a file using OpenCV.AlgorithmStep 1: Import cv2 Step 2: Read the image using opencv.imread() Step 3: Save the image using opencv.imwrite(filename, image)Example Codeimport cv2 import os image = cv2.imread('testimage.jpg') directory = r'C:\Users\prasa\Desktop' os.chdir(directory) cv2.imwrite('CAMERAMAN.jpg', image)OutputThis program will save the image in the directory as same as the original image directoryExplanationEnsure that you have set the proper directory in order for the program to execute without errors.

Plot Vertical Line on Time Series Plot in Pandas

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:58:29

4K+ Views

Using Pandas, we will create a dataframe and set the vertical lines on the created axes, using axvline lines.StepsUsing panda we can create a data frame.Creating a data frame would help to create help.Using axvline(), add a vertical line across the axes, where color is green, linestyle="dashed".Using axvline(), add a vertical line across the axes, where color is red, linestyle="dashed".Using plt.show(), show the plot.Exampleimport pandas as pd from matplotlib import pyplot as plt df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31")) df["y"] = 1 ax = df.plot() ax.axvline("2019-07-24", color="green", linestyle="dashed") ax.axvline("2019-07-31", color="red", linestyle="dashed") plt.show()OutputRead More

Make 3D Plot Interactive in Jupyter Notebook using Python and Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:58:03

3K+ Views

In this article, we can take a program code to show how we can make a 3D plot interactive using Jupyter Notebook.StepsCreate a new figure, or activate an existing figure.Create fig and ax variables using subplots method, where default nrows and ncols are 1, projection=’3d”.Get x, y and z using np.cos and np.sin function.Plot the 3D wireframe, using x, y, z and color="red".Set a title to the current axis.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j] x = np.cos(u) * ... Read More

Convert Image from Colour to Grayscale using OpenCV

Prasad Naik
Updated on 17-Mar-2021 07:57:34

16K+ Views

In this program, we will change the color scheme of an image from rgb to grayscaleAlgorithmStep 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2.cvtcolor() function.Example Codeimport cv2 image = cv2.imread('colourful.jpg') cv2.imshow('Original',image) grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('Grayscale', grayscale)OutputOriginal Image:Grayscale Image:

Advertisements