-3

I've tried converting my image to grayscale using multiple methods, but my image won't convert

I tried:

image = cv2.imread(r"path\shoe.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray image',gray)

But the image stays the same

2 Answers2

0

you can read images as grayscale directly

import cv2
import matplotlib.pyplot as plt
img_path=r'your path'
img=cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
plt.imshow(img)
Omar rai
  • 29
  • 5
0

This works for me in Python/OpenCV with your image on my Mac desktop along with the script. Two issues. 1) Your image is webp not png (at least what I can download from your link). 2) You need to add cv2.waitKey(0).

import cv2

image = cv2.imread("shoe.webp")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray image',gray)
cv2.waitKey(0)
fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Oh, so the image must be in the wrong format, I used the snipping tool to get the image. Do all images have to be PNG for it to be converted to grayscale image? Also, I added the cv2.waitKey(0) just forgot to put it in the code snippet – Ife Makinde Jan 23 '23 at 22:32
  • No all images do not have to be PNG. Any image that is compatible with your Python/OpenCV. Perhaps you do not have a version that includes WEBP library. If you computer can handle webp, then convert it from webp to png and try the png instead in your code. – fmw42 Jan 23 '23 at 22:41
  • When posting code, be sure you have listed a fully reproducible set of code. Don't leave out parts. – fmw42 Jan 23 '23 at 22:42
  • I'm not sure exactly what format the image is in because it doesn't convert to grayscale for me. I even tried using snipping tool and snipped the picture and ensured it was in PNG but it still didn't convert to grayscale – Ife Makinde Jan 23 '23 at 22:46
  • the images are taken from here: https://www.nike.com/ie/t/air-force-1-07-shoes-GjGXSP/CW2288-111?cp=35596902505_search_%7c%7c10564800177%7c103186519374%7c%7cc%7cEN%7ccssproducts%7c454138247005&ds_rl=1252249&gclid=Cj0KCQiA_bieBhDSARIsADU4zLceuuR6aeCT5fP3dQDMIHgqEha3OayYqOBOEIJHNvv0Xvs1gwiPbeIaAm_kEALw_wcB&gclsrc=aw.ds. But for some reason, it just won't convert. – Ife Makinde Jan 23 '23 at 23:09
  • The file I downloaded from that web site is a JPG. Try changing the file format to JPG in the file and in your python command – fmw42 Jan 24 '23 at 00:26
  • I tried changing it to jpg, but it didn't work Is it definitely working for you? I was playing around with the grayscale conversion and it seems to only work when the picture has other colours inside it too for example black. If it's pure white like the picture it doesn't seem to convert – Ife Makinde Jan 24 '23 at 00:35
  • As I showed above, it converts fine for me (your webp version). It also works from the jpg file I downloaded from the web site link: air-force-1-07-shoes-GjGXSP.png.jpeg – fmw42 Jan 24 '23 at 01:03
  • Is your path correct? – fmw42 Jan 24 '23 at 01:07
  • try double \\ in place of \ and without the r prefix. also try forward slashes / with and without the r prefix – fmw42 Jan 24 '23 at 01:12
  • Are you sure that the code you have written in the answer works for you? The conversion will surely work, but you are visualizing `image` instead of `gray`. So with this code, how can we see the grayscale image? – sgarizvi Jan 24 '23 at 07:37
  • @fmw42 the path is fine, I even tried replacing the \ with /, I'm still getting the same issue. The issue doesn't occur with images that have more colour – Ife Makinde Jan 24 '23 at 12:16
  • Is the path is sub folder `path\shoe.png`? should be `shoe.png`. – toyota Supra Jan 24 '23 at 12:28
  • The path should be where ever you have the image located. – fmw42 Jan 24 '23 at 17:27
  • 1
    @sgarizvi. You are correct. My typo. But it does work if I imshow("gray", gray) and the result is just grayscale, not color. I have corrected my answer. Thanks for catching that. – fmw42 Jan 24 '23 at 17:31
  • So what was the solution? – fmw42 Jan 25 '23 at 16:28