Python Articles

Page 197 of 855

How to find the HSV values of a color using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 14K+ Views

To find the HSV values of a color, we can use color space conversion from BGR to HSV. First we define the color value in BGR format as numpy.ndarray and then convert it to HSV space. We can also find the lower and upper limits of HSV value as [H-10, 100, 100] and [H+10, 255, 255] respectively. These lower and upper limits can be used to track an object of particular color. Steps Import the required libraries. In all the following Python examples, the required Python libraries are OpenCV and NumPy. Make sure you have already installed ...

Read More

How to create a trackbar as the RGB color palette using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 1K+ Views

In OpenCV, a trackbar can be created using cv2.createTrackbar() function. To access the value of the selected trackbar position, we use cv2.getTrackbarPos() function. Using these two functions, we create a window that contains the trackbars for R, G, B colors and a color window to display the selected color. By changing the position of trackbars RGB colors change between 0 and 255. Syntax cv2.createTrackbar(trackbar_name, window_name, default_value, max_value, callback_func) cv2.getTrackbarPos(trackbar_name, window_name) Parameters trackbar_name − It's the trackbar name. This name is used to access the trackbar position value. window_name − It is the ...

Read More

How to convert an RGB image to HSV image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 25K+ Views

An RGB (colored) image has three channels: Red, Green, and Blue. A colored image in OpenCV has a shape in [H, W, C] format, where H, W, and C are image height, width and number of channels. All three channels have a value range between 0 and 255. The HSV image also has three channels: Hue, Saturation and Value. In OpenCV, the values of the Hue channel range from 0 to 179, whereas the Saturation and Value channels range from 0 to 255. In OpenCV, to convert an RGB image to HSV image, we use the cv2.cvtColor() function. ...

Read More

How to create a black image and a white image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 21K+ Views

To create a black image, we use the np.zeros() method which creates a numpy array with all elements as 0. When displayed using cv2.imshow(), it appears as a black image since 0 represents black pixels. To create a white image, we use np.ones() method and multiply by 255 to get maximum pixel intensity. This creates a white image since 255 represents white pixels in 8-bit images. Note − We pass dtype = np.uint8 to create 8-bit unsigned integer arrays suitable for image data. Creating a Black Image Black images are created using np.zeros() which initializes all ...

Read More

How to join two images horizontally and vertically using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 5K+ Views

Images in OpenCV are represented as numpy.ndarray. OpenCV provides two functions − cv2.hconcat() and cv2.vconcat() to join images horizontally and vertically respectively. These functions have the following requirements ? They can join two or more images All images must have the same dimensions (height and width) All images must have the same number of channels Syntax cv2.hconcat(img_list) cv2.vconcat(img_list) Where img_list is a list of images [img1, img2, …]. Steps to Join Images Step 1: Import the required libraries ? ...

Read More

What is the use of the WITH statement in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 7K+ Views

The with statement in Python provides an elegant way to handle resources like files, database connections, and network sockets. It automatically manages resource cleanup and replaces complex try-catch blocks with cleaner, more readable code. What is the WITH Statement? The with statement works with context managers to ensure resources are properly opened and closed. Key benefits include ? Automatic resource cleanup Exception safety Cleaner, more readable code No need for explicit close() calls Reading Files with WITH Statement The most common use case is file handling. Here's how to read a file using ...

Read More

What are compound data types and data structures in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 5K+ Views

In this article, we will explain what are the compound datatypes and data structures in Python. Variables have so far only stored one value. What if we wish to save many related values? We could simply create distinct variables for each. But what if we don't know how many values will be present? What if we wish to use these values within a loop? Compound data structures are data types that can store a large number of values. In Python, there are various types of compound data structures. We will mostly concentrate on ...

Read More

How do I access the serial (RS232) port in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 24K+ Views

To access the serial (RS232) port in Python, use the pyserial module, which provides Python Serial Port Extension for Win32, OSX, Linux, BSD, Jython, and IronPython. PySerial Features Access to the port settings through Python properties Support for different byte sizes, stop bits, parity and flow control with RTS/CTS and/or Xon/Xoff Working with or without receive timeout The files in this package are 100% pure Python The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. Installation To install pyserial, use pip − pip install pyserial ...

Read More

How do I find the current module name in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 14K+ Views

In Python, every module has a built-in variable __name__ that contains the current module's name. When a script is run directly, __name__ equals '__main__'. When imported as a module, it contains the actual module name. Basic Usage of __name__ Let's check the value and type of __name__ in a running script: print(__name__) print(type(__name__)) __main__ Using __name__ for Script Entry Point The most common use is to create code that only runs when the file is executed directly: def main(): print('Testing...') ...

Read More

How do I parcel out work among a bunch of worker threads in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 243 Views

To parcel out work among a bunch of worker threads in Python, use the concurrent.futures module, especially the ThreadPoolExecutor class. Alternatively, if you want fine control over the dispatching algorithm, you can write your own logic manually using the queue module. The Queue class maintains a list of objects and has a .put(obj) method that adds items to the queue and a .get() method to return them. The class will take care of the locking necessary to ensure that each job is handed out exactly once. Using ThreadPoolExecutor The simplest approach is using ThreadPoolExecutor ? ...

Read More
Showing 1961–1970 of 8,549 articles
« Prev 1 195 196 197 198 199 855 Next »
Advertisements