In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.