0

i have data set of images first i read the image then i convert it to grey and i want to save it after dividing each grey image by 255 i try

files = os.listdir(path) 
for file in files:
folder = os.listdir(newdir+'\\'+file)
for f in folder:
    folder2 = os.listdir(newdir+'\\'+file+'\\'+f)
    if not os.path.exists(dstpath+'\\'+file+'\\'+f):
        os.makedirs(dstpath+'\\'+file+'\\'+f)
    for image in folder2:
            img = cv2.imread(os.path.join(newdir+'\\'+file+'\\'+f+'\\',image))
            pic = asarray(img)
            pic = pic.astype('float32')
            gray = cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
            dist = dstpath+'\\'+file+'\\'+f+'\\'+image
            cv2.imwrite(dist,(gray / 255).astype('uint8'))

but when i check photos they are all black

  • why shouldn't they be all black? you just reduced all pixel values to 0 or 1, or a range of 255. common image file types store 8 bit integer values, not floats. you did not disclose the file type. – Christoph Rackwitz Dec 30 '22 at 16:48

1 Answers1

0

if you have an 8bit image the highest pixel value is 255, so if you divide all by 255, you are going to get all pixels of 0 or 1, which is all black. to convert from an array to 8bit, use the following:

array = array / np.max(array)
array = array * 255
img = array.astype('uint8')
BH10001
  • 52
  • 10