Jim Lawless' Blog


Command-Line Image Format Conversion

Originally published on: Sat, 26 Sep 2009 14:44:24 +0000

I needed a quick-and-dirty command-line image format conversion utility. The .NET framework made the task pretty painless. I wrote the following utility that can convert between Windows Bitmap, JPEG, GIF, PNG, and TIFF images. ( TIFF may have a variety of subformats, so your mileage may vary when trying to convert to/from TIFF. )

imgconv.cs


// imgconv.cs - Simple command-line image file converter
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


using System ;
using System.Drawing;
using System.Drawing.Imaging;

namespace ImgConv
{
   public class ImgConv
   {
        [STAThread]
public static void Main(string[] args)
      {
         ImageFormat fmt;
         string inputFile="",outputFile="";
         int i;
         fmt=ImageFormat.Jpeg;
         Console.WriteLine("\nimgconv by Jim Lawless - jimbo@radiks.net\n");
         
         for(i=0;i<args.Length;i++)
         {
            if(args[i].ToLower().Equals("-in"))
            {
               inputFile=args[i+1];
               i++;
            }
            else
            if(args[i].ToLower().Equals("-out"))
            {
               outputFile=args[i+1];
               i++;
            }
            else
            if(args[i].ToLower().Equals("-gif"))
            {
               fmt=ImageFormat.Gif;
            }
            else
            if(args[i].ToLower().Equals("-jpg"))
            {
               fmt=ImageFormat.Jpeg;
            }
            else
            if(args[i].ToLower().Equals("-png"))
            {
               fmt=ImageFormat.Png;
            }
            else
            if(args[i].ToLower().Equals("-bmp"))
            {
               fmt=ImageFormat.Bmp;
            }
            else
            if(args[i].ToLower().Equals("-tiff"))
            {
               fmt=ImageFormat.Tiff;
            }
            else
            {
               Console.WriteLine("Unknown option " + args[i]);
               Syntax();
               return;
            }
         }
         if( (inputFile=="")||(outputFile=="")) {
            Syntax();
            return;
         }
Bitmap b=new Bitmap(inputFile);
         b.Save(outputFile,fmt);
      }
      public static void Syntax()
      {
         Console.WriteLine("Syntax:\timgconv.exe -in input_file_name -out output_file_name [ -gif -png -tiff -jpg -bmp ]\n");
         Console.WriteLine("The default output format is -jpg.\n");
      }
   }
}


imgconv by Jim Lawless - jimbo@radiks.net

Syntax: imgconv.exe -in input_file_name -out output_file_name [ -gif -png -tiff -jpg -bmp ]

The default output format is -jpg.

To load a BMP file and save as a JPEG, issue the following command. ( The default output format is JPEG. You may optionally specify the -jpg parameter. )


imgconv -in file.bmp -out file.jpg

To load a JPEG file and save as a PNG, issue the following command.


imgconv -in file.jpg -out file.png -png

I haven't tested this with Mono, yet.

The source and executable file for imgconv can be downloaded in a single archive at: http://www.mailsend-online.com/wp/imgconv.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Yet Another Enhanced Echo Command
Next post:Auto Save Images from the Clipboard


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Invoking the Default Windows Screen-Saver

An Interview with the Creator of the BDS C Compiler

A DSL in JavaScript

A Command-Line CD Tray Opener

WSH JavaScript Includes

Site Tracking with Perl

Directory Traversal in Rhino JavaScript

Charging by the Byte

Speeding up JRuby with NailGun

Computers I Have Known


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer