draw134
draw134

Reputation: 1187

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)

Upvotes: 0

Views: 2733

Answers (2)

Juan
Juan

Reputation: 5738

The original cause of the problem is that skimage.color.rgb2gray will rescale an image to floating point values in [0, 1], even if the original image contains uint8 values in [0, 255]. This is done to preserve precision during long sequences of operations. Using skimage.io.imsave instead of cv2.imwrite in the original question would work, as well as using skimage.util.img_as_ubyte before saving.

See this page for more details:

https://scikit-image.org/docs/dev/user_guide/data_types.html

Upvotes: 1

Raviteja Narra
Raviteja Narra

Reputation: 456

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

Upvotes: 2

Related Questions