a violet pig being the logo of this site

upload_image – Command line image uploading

upload_image automates some image hosting sites to allow uploading files from command line.

Download

upload_image

Installation

You need Python (at least version 2.6) installed and working.

Installation of the script itself is rather easy, just place it somewhere in your $PATH. More complex is the installation of its dependencies:

  • argparse: Easy and convenient command line parsing
  • ClientForm: Webform automation
  • lxml: HTML parsing and scraping

All these packages are avialable on the PyPI: and can therefore be installed with easy_install:

easy_install argparse ClientForm lxml>=2

If you want to have clipboard support, you will also need to manually install the xsel utility.

Usage

upload_image is very simple. It uploads the specified input file, or reads the image data from standard input. A single dash (-) as file name will read from standard input, too:

# upload_image foobar.png

There are some options that fine-tune the behaviour of this script. The --help gives an overview over all supported options, the most important are presented here.

-f name, --filename name

Sends name as filename to the server. This option defaults to the filename of the local file or to stdin, if data is read from standard input. The following will read a image file from standard input and send it to the server as foobar.png:

# upload_image -f foobar.png < my_image.png
-m type, --mime-type type
Allows to overwrite the mimetype send to the server. Normally the type is detected from the image header using python’s standard imghdr module.
-s {UbuntuPics,ImageBanana}, --service service {UbuntuPics,ImageBanana}

This option allows to specify the image hosting service where to upload the image file. Currently two services are supported, Image Banana and Ubuntu Pics. The default is read from UPLOAD_IMAGE_DEFAULT_SERVICE. If this is unset, Ubuntu Pics is used.

Warning

Use of this script with any of the above services is at your own risk! The existence of this script doesn’t mean, that the admins of these services appreciate its use with their sites.

UPLOAD_IMAGE_DEFAULT_SERVICE

This environment variable provides the default service for upload_image. Set this in your shell initialisation file (like ~/.bashrc or ~/.zshrc) to switch to a specific service permanently.

Note

The -s option overrules this setting!

After a successful upload the script parses all links returned by the website and prints them on standard output. The link pointing to the image itself and the phpbb-compatible bbcode are printed first. Two options exist to work with the results from the web server:

-c {no,bbcode,image}, --copy {no,bbcode,image}
This option selects a link to be copied into clipboard. This needs the xsel utility. no copies nothing, bbcode copies a phpbb-compatible bbcode into clipboard, image copies to full url to the image.
-b, --browser
This options opens the image in your favourite webbrowser. You should set $BROWSER to something reasonable for this to work correctly.

Example

You often want to quickly take some screenshot to illustrate your problems or explanations in some webforum or in the usenet. I use the following command line to accomplish this conventiently:

import png:- | upload_image -f shot.png -c bbcode

This takes a screenshot using the import utility from the ImageMagick toolkit to take a screenshot and write as PNG file to standard output. This output is then piped to upload_image, which uploads the image as shot.png and copies the bbcode link into clipboard for use in your webbrowser. If creating a screenshot for the usenet, I use -c image instead to copy the full image url.

Additional services

Extending the script to support additional services shouldn’t be that difficult. Contact me or send me a patch, if you want support for another image hosting site.

License

This programm is free software, you can redistribute and/or modify it under the terms of the WTFPL 2.

Table Of Contents

Previous topic

pydo – What to do now?

Next topic

gboquotes – Get the quotes out of GBO

This Page