
《はじめに》
コマンドラインから呼び出し可能な複数画像を連結して書きだすソフトって,ググっても案外無いので作成してみました.
単純に画像をひっつけて書き出すだけです.
ちょっとした事務的な処理で作成したので,もしよろしければ^^
エラー処理とか超適当なので,実行時エラーが出まくりだと思いますが,きちんと使えば大丈夫…なはず(笑
一応コード(C#)も載せています.
《実行ファイルのダウンロード》
ダウンロードはこちらから.
《使い方》
このソフトはCUIベースです.なので,GUIでは利用できません.
コマンドプロンプトを起動(すべてのプログラム→アクセサリ→コマンドプロンプト)し,
①本ソフトが存在するディレクトリに移動(cd [対象ディレクトリへの絶対パス])し,(②dirでそのフォルダに存在するファイル郡を参照)
③ConcatImage.exeを実行して,プログラムの使い方を参照し,
④③に従ってファイル名を指定していくと,

次のような出力ファイルが得られます.

《コード》
プログラムはC#で書いています.
windows環境でちょっとしたアプリケーション作るにはC#便利ですよ^^
002 | using System.Collections.Generic; |
005 | using System.Threading; |
007 | using System.Drawing; |
008 | using System.Drawing.Imaging; |
009 | using System.Runtime.InteropServices; |
010 | using System.Runtime.Serialization; |
011 | using System.Runtime.Serialization.Formatters.Binary; |
013 | namespace ConcatImageCSharp |
017 | static void Main( string [] args) { |
020 | if (args.Length < 3) { |
021 | Console.WriteLine( "コマンドライン引数に問題があります." ); |
022 | Console.WriteLine( " concatImage.exe 出力ファイル名、配置モード、入力ファイルパス1、入力ファイルパス2、..." ); |
023 | Console.WriteLine( "と入力してください." ); |
024 | Console.WriteLine( "ただし、配置モードは次のとおりです." ); |
025 | Console.WriteLine( " 1:横に並べる(入力ファイル2~枚)" ); |
026 | Console.WriteLine( " 2:縦に並べる(入力ファイル2~枚)" ); |
027 | Console.WriteLine( " 3:正方形状Z順に並べる" ); |
028 | Console.WriteLine( " 4:正方形状N順に並べる" ); |
029 | Console.WriteLine( " 5:n列Z型に並べる" ); |
030 | Console.WriteLine( " 6:n行N型に並べる" ); |
034 | Console.WriteLine( "出力ファイル:" + args[0]); |
035 | Console.WriteLine( "配置モード:" + args[1]); |
036 | for ( int i = 2; i < args.Length; i++) { |
037 | Console.WriteLine( "入力ファイル" + (i - 2) + ":" + args[i]); |
042 | string outputFilePath = args[0]; |
043 | int mode = Convert.ToInt32(args[1]); |
046 | List< string > inputFilePaths = new List< string >(); |
047 | for ( int i = 2; i < args.Length; i++) { |
048 | inputFilePaths.Add(args[i]); |
051 | Bitmap resultImage = new Bitmap(inputFilePaths[0]); |
054 | resultImage = ConcatHorizontalMultipleImages(inputFilePaths); |
057 | resultImage = ConcatVerticalMultipleImages(inputFilePaths); |
060 | int col = ( int )Math.Sqrt(inputFilePaths.Count); |
061 | if (col * col < inputFilePaths.Count) { |
064 | resultImage = ConcatZMultiplaImages(inputFilePaths, col); |
067 | int row = ( int )Math.Sqrt(inputFilePaths.Count); |
068 | if (row * row < inputFilePaths.Count) { |
071 | resultImage = ConcatNMultiplaImages(inputFilePaths, row); |
074 | Console.Write( "列数を入力してください:" ); |
075 | string colStr = Console.ReadLine(); |
076 | resultImage = ConcatZMultiplaImages(inputFilePaths, Convert.ToInt32(colStr)); |
079 | Console.Write( "行数を入力してください:" ); |
080 | string rowStr = Console.ReadLine(); |
081 | resultImage = ConcatNMultiplaImages(inputFilePaths, Convert.ToInt32(rowStr)); |
084 | Console.WriteLine( "mode:" + mode + " は存在しません" ); |
085 | Console.WriteLine( "利用できる配置モードは次のとおりです." ); |
086 | Console.WriteLine( " 1:横に並べる(入力ファイル2~枚)" ); |
087 | Console.WriteLine( " 2:縦に並べる(入力ファイル2~枚)" ); |
088 | Console.WriteLine( " 3:正方形状Z順に並べる" ); |
089 | Console.WriteLine( " 4:正方形状N順に並べる" ); |
090 | Console.WriteLine( " 5:n列Z型に並べる" ); |
091 | Console.WriteLine( " 6:n行N型に並べる" ); |
095 | resultImage.Save(outputFilePath); |
099 | public static Bitmap ConcatHorizontalTwiceImages(Bitmap leftBmp, Bitmap rightBmp) { |
102 | if (leftBmp.Height != rightBmp.Height) { |
103 | Console.WriteLine( "左右の画像の高さが異なります." ); |
108 | int width = leftBmp.Width + rightBmp.Width; |
109 | int height = leftBmp.Height; |
110 | Bitmap concatImage = new Bitmap(width, height); |
112 | for ( int x = 0; x < width; x++) { |
113 | for ( int y = 0; y < height; y++) { |
114 | if (x < leftBmp.Width) { |
115 | concatImage.SetPixel(x, y, leftBmp.GetPixel(x, y)); |
118 | concatImage.SetPixel(x, y, rightBmp.GetPixel(x - leftBmp.Width, y)); |
127 | public static Bitmap ConcatVerticalTwiceImages(Bitmap topBmp, Bitmap bottomBmp) { |
130 | if (topBmp.Width != bottomBmp.Width) { |
131 | Console.WriteLine( "上下の画像の高さが異なります." ); |
136 | int width = topBmp.Width; |
137 | int height = topBmp.Height + bottomBmp.Height; |
138 | Bitmap concatImage = new Bitmap(width, height); |
140 | for ( int x = 0; x < width; x++) { |
141 | for ( int y = 0; y < height; y++) { |
142 | if (y < topBmp.Height) { |
143 | concatImage.SetPixel(x, y, topBmp.GetPixel(x, y)); |
146 | concatImage.SetPixel(x, y, bottomBmp.GetPixel(x, y - topBmp.Height)); |
155 | public static Bitmap ConcatHorizontalMultipleImages(List< string > inputFilePaths) { |
156 | Bitmap tmp = new Bitmap(inputFilePaths[0]); |
157 | for ( int i = 0; i < inputFilePaths.Count - 1; i++) { |
159 | tmp = ConcatHorizontalTwiceImages(_tmp, new Bitmap(inputFilePaths[i + 1])); |
166 | public static Bitmap ConcatVerticalMultipleImages(List< string > inputFilePaths) { |
167 | Bitmap tmp = new Bitmap(inputFilePaths[0]); |
168 | for ( int i = 0; i < inputFilePaths.Count - 1; i++) { |
170 | tmp = ConcatVerticalTwiceImages(_tmp, new Bitmap(inputFilePaths[i + 1])); |
177 | public static Bitmap ConcatZMultiplaImages(List< string > filePaths, int col) { |
180 | int row = filePaths.Count % col == 0 ? filePaths.Count / col : filePaths.Count / col + 1; |
184 | List< string > horizontalFilePaths = new List< string >(); |
185 | for ( int w = 0; w < col; w++) { |
186 | horizontalFilePaths.Add(filePaths[count++]); |
188 | Bitmap concatImage = ConcatHorizontalMultipleImages(horizontalFilePaths); |
192 | for ( int h = 0; h < row; h++) { |
193 | _tmp = new Bitmap(concatImage); |
194 | horizontalFilePaths = new List< string >(); |
195 | for ( int w = 0; w < col; w++) { |
196 | if (count < filePaths.Count) { |
197 | horizontalFilePaths.Add(filePaths[count++]); |
203 | Bitmap __tmp = ConcatHorizontalMultipleImages(horizontalFilePaths); |
204 | concatImage = ConcatVerticalTwiceImages(_tmp, __tmp); |
212 | public static Bitmap ConcatNMultiplaImages(List< string > filePaths, int row) { |
215 | int col = filePaths.Count % row == 0 ? filePaths.Count / row : filePaths.Count / row + 1; |
219 | List< string > verticalFilePaths = new List< string >(); |
220 | for ( int h = 0; h < row; h++) { |
221 | verticalFilePaths.Add(filePaths[count++]); |
223 | Bitmap concatImage = ConcatVerticalMultipleImages(verticalFilePaths); |
227 | for ( int w = 0; w < col; w++) { |
228 | _tmp = new Bitmap(concatImage); |
229 | verticalFilePaths = new List< string >(); |
230 | for ( int h = 0; h < row; h++) { |
231 | if (count < filePaths.Count) { |
232 | verticalFilePaths.Add(filePaths[count++]); |
238 | Bitmap __tmp = ConcatVerticalMultipleImages(verticalFilePaths); |
239 | concatImage = ConcatHorizontalTwiceImages(_tmp, __tmp); |
《ちょっとした説明》
コードが冗長でお見せするべきようなものではないのですが,自分自身の思いとしましては,
そういうコードでもなんか欲しいということって結構ありますので,そういう方向けです.
本プログラムはバッチプログラムを書けば,複数の画像に対しても一気に処理を行えますので,案外便利かもしれないです.
1 | > for /l %i in (1,1,100) do ConcatImage.exe output_%i.png 2 input1_%i.png input2_%i.png |
こんなかんじで書きます.
《おわりに》
割りと単純なプログラムですので,プログラミングをあまりやったことのない人でも
何とか頑張れば,理解できるんじゃないかな?と思います.
私は,非プログラマにこそプログラミングを試して欲しいっていう考えがありますので,
何かしら興味の有ることがあれば,気軽にコメントやメールをください.
できるだけ対応を頑張ります^^
こういうのって,やってみたいと思ってるのに,素朴な疑問が分からなくて断念してるんじゃないかな?と思っています.
例えば,どういう開発環境なの?とか,どうやって学ぶの?とか,なんでC#にしたの?とか….
なので,そういうことに関しても,気軽に質問ください.
プログラミングはやらないと損だと思います.
生物学を専攻しようが物理学を専攻しようが,マルチメディア系のを趣味にしようが
プログラミングできると反復作業を自動化できるので,人生得になると思います.
本サイトをご覧になることで人生少しでもハッピーになる機会が得られれば私自身とても嬉しく思います.
ではではまた^^ノシ
- 関連記事