1

For batch image conversion from jpg to bmp, I workon the following code but it faces with some errors:

f=dir('./input')
fil={f.name};
for k=1:numel(fil)
file=fil{k};
new_file=strrep(file,'.jpg','.bmp');
im=imread(file);
imwrite(im,new_file);
end

the errors:

Error using imread>get_full_filename (line 513)

Cannot open file "." for reading. You might not have read permission.

Error in imread (line 340)

fullname = get_full_filename(filename);

Error in format_conversion (line 6)

im=imread(file);

Where is the problem?

Community
  • 1
  • 1
Ben25
  • 131
  • 1
  • 7
  • The . and .. cause the problem, explained here: https://stackoverflow.com/questions/27337514/matlab-dir-without-and – Daniel Dec 11 '19 at 19:05
  • @Daniel , The problem still is there even when I use **f=dir('E:\input');**. Moreover, I tried it by removing "." or ".." but it still doesn't work. – Ben25 Dec 11 '19 at 19:19
  • @beaker, Yes, it's true. – Ben25 Dec 11 '19 at 20:14

1 Answers1

4

Your problem is the . and .. entries in the folder, and Daniel pointed correctly to a solution in the comments.

However, since you use

new_file=strrep(file,'.jpg','.bmp');

in your code, it seems that you want to convert files with the extension .jpg only (not .jpeg or .JPG or .JPEG). Your code above would also run into problems if there is a .txt file or whatever in the directory. Fortunately, MATLAB's dir function allows to use wildcards, so the solution might be as easy as:

f=dir('./input/*.jpg')

Of course, later you will have to add the folder again:

im=imread(fullfile('input', file));
imwrite(im,fullfile('input',new_file));

A more flexible way than using the fixed 'input' is to use f.folder, see the docs of dir.

Patrick Happel
  • 1,336
  • 8
  • 18
  • Thanks for the answer but unfortunately, the folowing errors occur: Error using imread>get_full_filename (line 516) File "1.jpg" does not exist. Error in imread (line 340) fullname = get_full_filename(filename); Error in format_conversion (line 6) im=imread(file); ///// But I have 1.jpg in the folder. – Ben25 Dec 11 '19 at 20:24
  • Thanks for the perfect solution. – Ben25 Dec 11 '19 at 20:35