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



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

CP/M Days

A DSL in JavaScript

My Foray into Shareware

A Quine in Forth

Choose your own Adventure with Sinatra

PHP, Transparent GIF's, and Web Tracking

Checking Shift States with DEBUG

Envy

Screen Captures with Java and Clojure

We've Moved!


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service