Python image color detection

After downloading the image from the site, I need to determine the color of the downloaded image. I successfully uploaded the image, but I need to determine the color of the corresponding image and save it in the name of the corresponding color. The code used is given below. Tell me how I can achieve this from my current position.

imageurl='http://www.example.com/' opener1 = urllib2.build_opener() page1=opener1.open(imageurl) my_picture=page1.read() fout = open('images/tony'+image[s], "wb") fout.write(my_picture) fout.close() 
+18
python
Feb 16 '10 at 5:31
source share
6 answers

Use the PIL histogram (Python Image Library). Scroll through the histogram and take the average color value of a pixel weighted by the number of pixels.

+12
Feb 16 '10 at 5:37
source share

As others have mentioned, PIL is the right library. Here is the function that opens the image and searches for the primary color.

 def get_main_color(file): img = Image.open(file) colors = img.getcolors(256) #put a higher value if there are many colors in your image max_occurence, most_present = 0, 0 try: for c in colors: if c[0] > max_occurence: (max_occurence, most_present) = c return most_present except TypeError: raise Exception("Too many colors in the image") 

I hope this helps

Update: passing 256 to getcolors is ok for very small images, but may not work in most cases. This value should be increased for large images. for example, 1024 * 1024 is suitable for 400 pixels * 300 pixels of an image.

+9
Feb 16 '10 at 6:17
source share

You must use the PIL Parser from the ImageFile class to read the file from the URL. Then life is quite simple, because you said that the whole image is the same color. Here is the code that builds on your code:

 import urllib2 import ImageFile image_url = "http://plainview.files.wordpress.com/2009/06/black.jpg" opener1 = urllib2.build_opener() page1=opener1.open(image_url) p = ImageFile.Parser() while 1: s = page1.read(1024) if not s: break p.feed(s) im = p.close() r,g,b = im.getpixel((0,0)) fout = open('images/tony'+image[s]+"%d%_d%_d"%(r,g,b), "wb") fout.write(my_picture) fout.close() 

This should add the red green and blue color values ​​of the first pixel of the image to the end of the image name. I tested everything up to the fout lines.

+6
Feb 16 '10 at 6:17
source share

All answers discuss methods for finding a single color in an image, but it’s always useful to know how to find several colors in an image. Especially when you are dealing with images of segmentation tasks.

Let's take an image for our explanation

Segmentation task

Obviously, each class of objects in this figure has its own color.

Let's write a function to load an image from a URL and convert it to an empty array. Thus, it becomes very easy to work with images.

 import numpy as np import cv2 import urllib from urllib.request import urlopen import webcolors import time def getImageArray(mask): req = requestObject(mask) arr = np.asarray(bytearray(req.read()), dtype=np.uint8) im = cv2.imdecode(arr, -1) im = im[:, :, :3] return im def requestObject(mask): temp_req = {'status': 403} retry_counter = 1 while((temp_req['status'] != 200) and (retry_counter <= 10)): try: req = urlopen(mask) temp_req = {"status": 200} except: print("Retrying for: ", retry_counter) temp_req = {"status": 403} time.sleep(4) retry_counter = retry_counter + 1 return req 

Now let’s get the image:

 url = '/img/2e3a2f883613d9cc35e15875a7eb399f.jpg' image = getImageArray(url) 

Let's write a function to find all the colors:

 def bgr_to_hex(bgr): rgb =list(bgr) rgb.reverse() return webcolors.rgb_to_hex(tuple(rgb)) def FindColors(image): color_hex = [] for i in image: for j in i: j = list(j) color_hex.append(bgr_to_hex(tuple(j))) return set(color_hex) color_list = FindColors(image) 



Try running the above script in your terminal and you will get a list of hexadecimal codes for all colors in the color_list variable.

Let me know if the code works / not for you :)

+1
Jul 07 '19 at 19:00
source share

You can use the PIL library image module for this. See: http://effbot.org/imagingbook/image.htm .

0
Feb 16 '10 at 5:37
source share

You can use the K-means algorithm to get the primary colors of the K image. Here is an example of how to do this: K-means using OpenCV (Python)

0
Oct 13 '16 at 10:25
source share



All Articles