5

I have the following code, which is supposed to

  • go through all files
  • Collect all photos which belong in folders "0" or "1"
  • Transform the photos a bit
  • Place them into a new folders,---all "0-photos" go to a unique folder. Same for 1

Here it is

import cv2
import os
import random as rnd

outdir0='cancerdata\\0'
outdir1='cancerdata\\1'
readdir='sample'
j=0;
for dirs in os.listdir(readdir):
    dpath=os.path.join(readdir,dirs)
    for subdir in os.listdir(dpath): 
        fpath=os.path.join(dpath,subdir)
        for file in os.listdir(fpath):

            rfile=os.path.join(fpath,file)
            image=cv2.imread(rfile,0)
            size=rnd.randint(50,100)
            img=cv2.resize(image,(size,size),interpolation=cv2.INTER_AREA)
            print(img)
            if subdir == '0':
                wfile=os.path.join(outdir0,file)
                cv2.imwrite(os.path.join(outdir0,file),img)
            elif subdir == '1':
                wfile=os.path.join(outdir1,file)
                cv2.imwrite(os.path.join(outdir1,file),img)

It does NOT give an error, but for some reason does not save any of the files. Whenever I try to save a single image via

 cv2.imwrite(r'sample\cancerdata\1\8865_idx5_x2101_y851_class1.png',img)

All I get is FALSE.

1
  • Not the correct answer in your case, since I can see the filename is not too long, even after the os.path.join. However, in my case I found that I could get the same FALSE return value from cv2.imwrite if the final filename (absolute path) was too long. Commented Sep 12, 2022 at 18:55

4 Answers 4

5

Using backslashes (\) in python strings can lead to surprises. In the code above, the backslash is producing a character with an octal value that's invalid in a filename on Windows. See https://docs.python.org/3/reference/lexical_analysis.html#literals for details.

The Python runtime is perfectly happy with forward slashes (/) on Windows. Use those.

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

Comments

4

The folders that you are saving to probably do not exist.

Make sure that these folders exist:

cancerdata\0
cancerdata\1
sample\cancerdata\1

1 Comment

This was the issue in my case, i needed to create the folder where I store my images. False as a response makes sense, but there should be a better way to capture error messages.
0

I had the same exact problem. My path contained some Hebrew characters, which are probably not supported in that library. I changed it to English and the images had been saved. Path before change: r"C:\פרויקט\dataset"
Path after change: r"C:\project\dataset"

Comments

-1

first set the directory where you want to store your images as the current working directory. Then just simply pass the name of the image and your image will be save.

Comments

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.