Search code examples
pythonseleniumopencvimage-processingscikit-image

skimage convert to grayscale resulting to a black image


I am scraping image and trying to convert them into grayscale. But if i view the image. It is plain black. Can someone tell me what is wrong with this? Thanks

Here is my snippet

import cv2
from skimage import io
from skimage import data
from skimage.color import rgb2gray

#...some codes

for i in elements:
    try:
        i.click()
    except ElementClickInterceptedException:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        i.click()
    url = driver.find_element_by_xpath('//*[@class="slider-list"]/div/img').get_attribute('src').replace('height=600','height=1200').replace('quality=70','quality=100')
    print (url)
    name = driver.find_element_by_xpath('//*[@class="slider-list"]/div/img').get_attribute('alt') + '.jpg'

    print (name)
    img = io.imread(url)
    new = rgb2gray(img)
    cv2.imwrite(os.path.join(fldrnm, name), new)

Solution

  • Import cv2
    Img = cv2.inread(filepath)
    Img = cv2.cvtColor(Img, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(path,img)