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 ...


Follow me on Twitter

http://twitter.com/lawlessGuy


My GitHub Repository

https://github.com/jimlawless


Recent Posts

Compiling C from the Command Line with Pelles C

A Forthcoming Marvel Movie Villain

Uninstalling Problematic Windows Software

Don't be Hatin'

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug


Random Posts

Don't be Hatin'

Spellbound by Web Programming

Understanding TRS-80 CMD Files

Tracing XSLT with a Tiny Java Web Server

Java in a Windows EXE with launch4j

A Simple Parser for a Small Command Line Interface

We've Moved!

Thwarting HTTP Referer Trackbacks

Hiding Batch File Console Windows

A Simple Media Control Interface Script Processor


Full List of Posts

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


Recent Posts from my Other Blog / GitHub

Using Standard C Libraries with Yosemite JavaScript

JavaScript Libraries in Yosemite

Command Line JavaScript in Yosemite

Text to Speech, Yosemite, and JavaScript

A Simple (but useful) TCP Client Written in Go

Yet Another Seven Languages in Seven Weeks

Happy 50th Birthday, BASIC

Extending Commodore 64 BASIC

Hide the HTTP-Referer using HTML and JS

RSS Feed Processing in Python

A Chromecast Slideshow using Python

A Simple Perl REPL

Linux Mint on a Toshiba Netbook

Find and View All Images on a HD with Perl

Pretty-Printing an s-expression in Go

My Personal Text to HTML Utility

1985 Computing : Atari and Commodore

My Mac has Mono

Yet Another Config File Reader for Go (Golang)

Filling a Slice Using Command-line Flags in Go (Golang)

An RPN Interpreter in Go (Golang)

Simulating Try-Catch in Go (Golang)

Sending GMail with Go (Golang)

Variant Types in Golang

My First C64 Interrupt Program

The Triangles Puzzle

Happy 25th, Perl !

My Favorite BASIC One-Liner

Playing with OS/X Text to Speech

The Villain at the end of Marvel's Avengers Move is...

Chicken! Fight like a Robot!

Processing GMail items with Ruby

The Squares Puzzle

Happy 30th Birthday, Commodore 64

Scripting Safari

MailWrench CSharp Command Line Mailer for Windows is now Free Software

Welcome Back, M.U.L.E. !

Rainy Day Fun with the HTML DOM

Building a World War II Foxhole Radio

Prototerp Unleashing a JavaScript REPL via the Mobile Browser

Steal This Bookmarklet

Happy Birthday, Miles