If I have a txt file, which contains the url that downloads images, how do we write the script to do that?
e.g. txt file information:
.. .. ...

 Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Feb. de 2016
Editada: Walter Roberson el 5 de Feb. de 2016

0 votos

projectdir = fullfile(pwd, 'saved_images'); %location to store the files
if ~exist(projectdir, 'dir')
mkdir(projectdir);
end
fid = fopen('TheFile.txt', 'rt');
imcount = 0;
while true
this_url = fgetl(fid);
if ~ischar(this_url)
break; %end of file
end
if isempty(strtrim(this_url))
continue %empty line, probably near end of file
end
[url_path, url_img, url_ext] = fileparts(this_url);
dest_file = fullfile(projectdir, [url_img url_ext]);
if exist(dest_file, 'file')
fprintf('skipping existing %s\n', dest_file);
imcount = imcount + 1;
urls{imcount} = this_url;
img_files{imcount} = dest_file;
%img_content{imcount} = imread(dest_file);
continue;
end
fprintf('loading ... %s\n', url_img);
try
urlwrite(this_url, dest_file);
imcount = imcount + 1;
urls{imcount} = this_url;
img_files{imcount} = dest_file;
%img_content{imcount} = imread(dest_file);
catch
fprintf('failed downloading %s from "%s"\n', url_img, this_url);
end
end

6 comentarios

matlabgeek
matlabgeek el 5 de Feb. de 2016
Editada: Walter Roberson el 5 de Feb. de 2016
Thank you very much for your code.
But, the speed to download the image is very slow, it also will output fail download although it can download successfully?
You may try to download this 5 image to see the result?
http://forensics.inf.tu-dresden.de/ddimgdb/images/gallery/Nikon_D200_0_15434.NEF
http://forensics.inf.tu-dresden.de/ddimgdb/images/gallery/Nikon_D200_0_15436.NEF
http://forensics.inf.tu-dresden.de/ddimgdb/images/gallery/Nikon_D200_0_15438.NEF
http://forensics.inf.tu-dresden.de/ddimgdb/images/gallery/Nikon_D200_0_15440.NEF
http://forensics.inf.tu-dresden.de/ddimgdb/images/gallery/Nikon_D200_0_15442.NEF
Walter Roberson
Walter Roberson el 5 de Feb. de 2016
Editada: Walter Roberson el 5 de Feb. de 2016
I have tightened up the code a bit, improved the handling of empty lines (which can happen at end of file for example.) Also I added in a check to skip existing files, as an optimization, so if you want a file re-downloaded then delete the saved version. I still add the skipped file to the list of URLs and matching destination files, urls and img_files
matlabgeek
matlabgeek el 5 de Feb. de 2016
Wonderful code. I learn a lot from your code, like fullfile(), fgetl(), and so on, these functions I first use. Thank you. You are truly number one in Matlab Answer this domain.
matlabgeek
matlabgeek el 5 de Feb. de 2016
Sir, do you have any personal website or social network account for us to follow. I think I can learn a lot from your experienced coding ability. It looks like you write matlab as you do the computing for 1 + 1 = 2, very excellent matlab coding ability
Walter Roberson
Walter Roberson el 5 de Feb. de 2016

On social media I do not talk about programming much. I talk about programming so much here that social media is my escape from that, where I post about politics or post random science stories or (more often) post weird news like escaping Llamas, or Florida Man. It is not a "professional" presence and it won't help you learn to program. But if I haven't discouraged you sufficiently, you can find me on Google Plus under the same name I use here. My icon is a Japanese stone statue of a type of Japanese daemon.

matlabgeek
matlabgeek el 5 de Feb. de 2016
Haha, the more weired news, the more I am interested. I already followed your Google +. Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Downloads en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Feb. de 2016

Editada:

el 11 de Feb. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by