Why does wget'ing an image give me a file, not an image?

6

1

I do

wget http://services.runescape.com/m=itemdb_rs/3809_obj_sprite.gif?id=2

and it downloads the following file:

3809_obj_sprite.gif?id=2

How can I make it save as the following?

2.png

Szymon Toda

Posted 2012-08-04T16:11:32.437

Reputation: 1 239

16A quick note: wget does not do image conversion. Saving a GIF file with a .png extension does not turn it into a PNG file. You'll want to save the file with the proper extension (in this case, .gif), and use image manipulation software to convert it to a different format. – Indrek – 2012-08-04T16:17:56.030

12An image is a file. – Andrew J. Brehm – 2012-08-04T21:37:14.737

Answers

38

Why does wget choose this name?

Unfortunately, wget will make no assumptions about what you want to download unless you tell it to. It doesn't care if your file is an image, a document, a zipped file, et cetera.

The file is saved as whatever the URL ended with – so anything from the last slash to the end of the URL. In your case, that's .gif?id=2. The part after the actual file extension (.gif) is contained in the URL, but it's a HTTP query parameter. For wget, however, it will determine the output filename.

How can I set a different filename?

If you want to specify an output filename for wget, add the -O (uppercase O) option:

wget example.com/something.gif?id=2 -O 2.gif

This will override the default behavior and set the filename to 2.gif. Note that the -o (downcase) option specifies the output filename for any log messages wget might otherwise print to the shell.

Finally, there's the --content-disposition option which might result in the proper filename being set. But this entirely depends on the server you're downloading from sending the correct header information:

This option is useful for some file-downloading CGI programs that use Content-Disposition headers to describe what the name of a downloaded file should be.

The option is currently still marked as experimental and thus not enabled by default.


I strongly encourage you to read the manpage of the tools you're using to understand their behavior. Just enter man wget and read through it, especially the options it provides.

Also, to address what @Indrek wrote in the comment on your question: I assume you have a typo there and don't mean to download a GIF file into a file called .png — just changing the extension will not automatically make it a PNG. GIF and PNG use different encodings and you will have to use any kind of image conversion tool to convert between these formats. This conversion, however, will be lossless, so there's no harm done in downloading the files in the "wrong" format and converting later.

slhck

Posted 2012-08-04T16:11:32.437

Reputation: 182 472

1I have no idea why this has so many up votes, because the URL you posted is for a .gif file, changing the file name to 2.png will not magically make that .gif a .png. Either way an image is a file, everything saved anywhere is a file even directories are actually stored on filesystems as files. – None – 2012-08-04T22:30:48.500

4Jarrod, I now updated my answer and explicitly addressed what probably was a misunderstanding by the OP. As for you asking why this answer had so many up votes – are you inferring it should have been downvoted because I gave the OP wrong information? I never claimed that an image was not a file and that a GIF would magically turn into a PNG when renamed. I assumed it was a typo since the focus of the question was the file name. – slhck – 2012-08-04T23:02:25.570

6

There is the command line option --content-disposition which - if the website supplies a correct header - should cause your file to be saved with the right name.

My debian squeeze box it says the option is "experimental" though...

dualed

Posted 2012-08-04T16:11:32.437

Reputation: 309

1Nice find, I missed that before. It doesn't work in this case, but it could in others. – slhck – 2012-08-04T22:12:22.950

@slhck Yes, noticed that too - after writing it. But since it might work for others I left it :) – dualed – 2012-08-04T22:28:47.827

3

This is simply because look at the path, it ends in ?id=2, therefore it will save as this, you can move the file or use the -O parameter to define a file name.

Marcus Hughes

Posted 2012-08-04T16:11:32.437

Reputation: 242

1

curl also has an option to guess the file name from Content-Disposition header:

curl -OLJ http://services.runescape.com/m=itemdb_rs/3809_obj_sprite.gif?id=2

Here

-O tells curl to save to a file rather than flushing to stdout
-L makes it follow the redirects
-J enables guessing by Content-Disposition instead of dissecting the URL

Antony Hatchkins

Posted 2012-08-04T16:11:32.437

Reputation: 321

1

If the file you direct wget to download for you is indeed an image, then what it downloads is an image. wget doesn't bother to guess the correct file name and extension, it uses what it finds in the URL e.g http://foobar.com/photo.php?n=cat.png you might get something like photo.php?n=cat.png but it is still an image. To fix your problem, simply rename that file to the correct name cat.png. Note that on most operating systems programs need the correct file extension to know that they can open a given file.

user150396

Posted 2012-08-04T16:11:32.437

Reputation:

0

Also, with curl, say you don't know what the filename is going to be, due to redirects, or whathaveyou.. just follow the redirects via -L and pipe it to a file, with the correct extension..

curl -L http://links.mrgray.com/ocrtest > /files/file.png

wget may have similar functionality, dunno.

mralexgray

Posted 2012-08-04T16:11:32.437

Reputation: 668

Why the downvote? It works, and solves the guys' problem, no? – mralexgray – 2012-08-05T08:12:08.630

I didn't downvote but you clearly haven't read the question. – nikhil – 2012-08-05T12:17:42.450