Get the App
SLTechnology News&Howtos  ›  Development  › 

How to obtain the BGR value of picture pixels and generate a solid color map by Python

Shulou Source: shulou.com Published: 2022-06-02 03:16:07 09月29日 Update

This article "Python how to get picture pixel BGR value and generate a solid color map" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Python how to get picture pixel BGR values and generate a solid color map" article.

Dependent installation

You need to use opencv and numpy. The installation commands are as follows:

Pip install opencv-python-I https://pypi.douban.com/simplepip install numpy-I https://pypi.douban.com/simple code

No nonsense, go to the code.

#! / user/bin/env python# coding=utf-8 "" @ project: csdn@author: swordsman A Liang _ ALiang@file: make_pic_tool.py@ide: PyCharm@time: 2022-01-11 08:34:31 "import cv2import osimport numpy as npimport uuid # gets the image coordinate bgr value def get_pix_bgr (image_path: str, x: int Y: int): ext = os.path.basename (image_path). Strip (). Split ('.') [- 1] if ext not in ['png',' jpg']: raise Exception ('format error') img = cv2.imread (image_path) px = img [x, y] blue = img [x, y, 0] green = img [x, y, 1] red = img [x, y, 2] return blue, green Red # build a solid color map def make_one_color_pic (output_dir: str, image_path: str, coordinates: tuple, resolution: tuple): blue, green, red = get_pix_bgr (image_path, coordinates [0], coordinates [1]) img = np.zeros ((resolution [1], resolution [0], 3), np.uint8) # create BGR solid color diagram img [:] = [blue, green Red] result_image = os.path.join (output_dir,'{} .jpg '.format (uuid.uuid1 () hex)) cv2.imwrite (result_image, img) return result_image if _ _ name__ =' _ _ main__': print (make_one_color_pic (ritual C:\ Users\ huyi\ Desktop', ritual C:\ Users\ huyi\ Desktop\ 2054146.jpg, (300,300), (1080, 1920)

Code description:

1. The input parameters of the get_pix_bgr method are the image address and coordinate location, respectively, to obtain the BGR value.

2. The make_one_color_pic method is the final method to generate a solid color graph. The parameters include the output directory address, picture address, coordinate location, final picture resolution, and output final image path.

3. Use uuid for the final picture name to avoid repetition.

4. Do a simple file suffix check. If you need to modify it, you can add it yourself.

Verify it.

Prepared picture

Execution result

The final picture

The above is about "Python how to obtain picture pixel BGR value and generate solid color map" this article content, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Tags: Picture solid color content generation pixel address coordinate method code location article knowledge article output previous generation value resolution swordsman parameter suffix Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Shulou Information Shulou Technology Docker