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
Save to del.icio.us
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks