0

I'm working on a program that reads csv file to get the names of colors, compares RGB values with RGB values of an image from URL. I think the program doesn't get image from URL since I tried to imshow() to check whether image is passed into program or not. I get this error (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

This is the code:

import numpy as np #needed to work with matrix of an image
import pandas as pd #needed to work with color.csv
import cv2 #needed to work with image
import matplotlib.pyplot as pl #needed to work with plotting
import urllib.request#needed to work with image url

#step 1. Read csv file with name, RGB and HEX values.
#step 2. Set color detection function. Get value of pixels in a NumPy array
#step 3. Compare RGB value of a pixel with dataframe.
#step 4. Save the name and RBG value inside a file. 

#image from url
def url_to_image(url): #doesn't get file, need to work upon this 
    resp = urllib.request.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype='uint8')
    image = cv2.imdecode(image,cv2.IMREAD_COLOR)
    return image

#dataframe with 864 colors 
index = ['color', 'color_name', 'hex','R','G','B'] 
csv = pd.read_csv('colors.csv', names = index, header = None)



def getColor(R,G,B): 
    minimum = 10000
    for i in range(len(csv)):
        distance = abs(R-int(csv.loc[i, 'R'])) + abs(G-int(csv.loc[i, 'G'])) + abs(B-int(csv.loc[i,'B']))
        if(distance<=minimum):
            minimum = distance
            color_name = csv.loc[i, 'color_name']
        return color_name

img = url_to_image("https://upload.wikimedia.org/wikipedia/commons/2/24/Solid_purple.svg")
cv2.imshow("image", img)
cv2.waitKey(0)














1 Answer 1

2

It doesn't work because you are trying to use an svg Image (which is vector based) to open in an Matrix like an JPEG or PNG image (which are raster based). It doesn't work like that with these.

Try loading a different Image like this

https://miro.medium.com/max/800/1*bNfxs62uJzISTfuPlOzOWQ.png EDIT sry wrong link

https://htmlcolorcodes.com/assets/images/colors/purple-color-solid-background-1920x1080.png

this will work because this is an png

As far as i know Opencv has no good support for SVG based Images

Sign up to request clarification or add additional context in comments.

4 Comments

When I try o get PNG image, there is another error: urllib.error.HTTPError: HTTP Error 403: Forbidden
@Nighttwinkle Sry I am not sure why it behaved like this. It works fine for me. Maybe the Site blocked the request to block scrapers. But as long you use links for jpeg/jpg/png images your script should work. These Links often ends with the corresponding Fileending
@Nighttwinkle I realized that I copied the wrong link this was not the one which worked for me I messed up. But I have updated the Answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.