LoginSignup
3
3

More than 5 years have passed since last update.

PythonでETC用にマスク画像を作るツール

Posted at

以前の記事で紹介したマスクを作るツールをpythonで実装しました。

開発環境

Python 2.7.9
Python Imaging Library 1.1.7

概要

画像ファイルを入れるとマスク画像を出力する
CreateMask.py [マスク元のファイル]

オプション

[-w] 横にマスクを持つ画像を生成
[-h] 縦にマスクを持つ画像を生成
[-o] [出力ファイル] 指定されたファイル名で出力(この指定がない場合は、入力ファイル名に_mをつけて出力)

CreateMask.py

# -*- coding: utf-8 -*-

if __name__ == "__main__":
  import sys
  import os
  import Image

  # ------------------------------------------------------------------------------------------------------
  # 通常のマスク画像生成
  # ------------------------------------------------------------------------------------------------------
  def CreateMaskNormal(src, dst) :
    srcdata = list(src.getdata())    # 元のデータを取得
    for i in range(len(data)) :
      c = data[i][3]
      data[i] = (c, c, c)
    dst.putdata(data)


  # ------------------------------------------------------------------------------------------------------
  # 横にマスクがある画像生成
  # ------------------------------------------------------------------------------------------------------
  def CreateMaskWidth(src, dst, width, height) :
    srcdata = list(src.getdata())    # 元のデータを取得
    dstdata = srcdata + srcdata      # 書き込み先の領域を確保

    for y in range(height) :
      soff = y * width;
      doff = y * width * 2;
      for x in range(width) :
        ct = srcdata[soff]
        c = ct[3]
        dstdata[doff] = (ct[0], ct[1], ct[2])
        dstdata[doff + width] = (c, c, c)
        soff += 1
        doff += 1
    dst.putdata(dstdata)


  # ------------------------------------------------------------------------------------------------------
  # 縦にマスクがある画像生成
  # ------------------------------------------------------------------------------------------------------
  def CreateMaskHeight(src, dst, width, height) :
    srcdata = list(src.getdata())    # 元のデータを取得
    dstdata = srcdata + srcdata      # 書き込み先の領域を確保

    hlen = len(srcdata)
    for y in range(height) :
      off = y * width;
      for x in range(width) :
        ct = srcdata[off]
        c = ct[3]
        dstdata[off] = (ct[0], ct[1], ct[2])
        dstdata[off + hlen] = (c, c, c)
        off += 1
    dst.putdata(dstdata)

  # ------------------------------------------------------------------------------------------------------
  # マスク画像の生成
  # ------------------------------------------------------------------------------------------------------
  def CreateMask(in_file, out_file, w_scale, h_scale) :
    src = Image.open(in_file)
    if src.mode != "RGBA":
      return False

    dst = Image.new("RGB", (src.size[0] * w_scale, src.size[1] * h_scale))

    if w_scale != 1 :
      CreateMaskWidth(src, dst, src.size[0], src.size[1])
    elif h_scale != 1 :
      CreateMaskHeight(src, dst, src.size[0], src.size[1])
    else :
      CreateMaskNormal(src, dst)

    dst.save(out_file, "png")

    return True

  # ------------------------------------------------------------------------------------------------------
  # メイン処理
  # ------------------------------------------------------------------------------------------------------
  args = sys.argv  # コマンドライン引数を格納したリストの取得
  argc = len(args) # 引数の個数

  w = False; # 横にマスクを作成の有無
  h = False; # 縦にマスクを作成の有無
  o = False; # 出力ファイル指定
  in_file = "";    # 入力ファイル名
  out_file = "";   # 出力ファイル名

  # 引数を確認
  for i in range(1, argc):
    if args[i] == "-w":
      w = True
    elif args[i] == "-h":
      h = True
    elif args[i] == "-o":
      o = True
      out_file = args[i + 1]
    elif o:
      o = False
      out_file = args[i]
    else:
      in_file = args[i]

  if in_file == "":
    print u"CreateMask.py     [読み込む画像ファイル名]"
    print u"-w                横にマスクを生成"
    print u"-h                縦にマスクを生成"
    print u"-o 出力ファイル名 出力ファイル名を設定"
    print u"                  設定がない場合は、入力ファイルに_mが追加されて出力"
    sys.exit()

  path = os.path.dirname(in_file)
  if path != "" :
    path += u"/"

  if out_file == "" :
    # 出力ファイル名がない場合は入力ファイルに_mを追加したものを設定
    file, ext = os.path.splitext(os.path.basename(in_file))
    out_file = path + file + u"_m.png";
  elif os.path.exists(out_file) != u".png" :
    # 拡張子に.pngを設定
    out_file = path + os.path.basename(out_file) + ".png";

  # 幅の設定
  w_scale = 1
  if w :
    w_scale = 2

  # 高さの設定
  h_scale = 1
  if h and not w:
    h_scale = 2

  # マスクを作成
  CreateMask(in_file, out_file, w_scale, h_scale);

最後に

これでmacでも変換できますよ!
Python Imaging Libraryをインストールしないと動かないので注意してください。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3