Tool to make mask image for ETC in Python

I implemented the mask making tool introduced in the previous article in python.

Development environment

Python 2.7.9 Python Imaging Library 1.1.7

Overview

A mask image is output when an image file is inserted. CreateMask.py [Mask source file]

option

[-w] Generate an image with a horizontal mask [-h] Generate an image with a vertical mask [-o] [Output file] Output with the specified file name (If this is not specified, output with _m added to the input file name)

CreateMask.py



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

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

  # ------------------------------------------------------------------------------------------------------
  #Normal mask image generation
  # ------------------------------------------------------------------------------------------------------
  def CreateMaskNormal(src, dst) :
    srcdata = list(src.getdata())    #Get the original data
    for i in range(len(data)) :
      c = data[i][3]
      data[i] = (c, c, c)
    dst.putdata(data)


  # ------------------------------------------------------------------------------------------------------
  #Image generation with a mask on the side
  # ------------------------------------------------------------------------------------------------------
  def CreateMaskWidth(src, dst, width, height) :
    srcdata = list(src.getdata())    #Get the original data
    dstdata = srcdata + srcdata      #Secure the write destination area

    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)


  # ------------------------------------------------------------------------------------------------------
  #Image generation with vertical mask
  # ------------------------------------------------------------------------------------------------------
  def CreateMaskHeight(src, dst, width, height) :
    srcdata = list(src.getdata())    #Get the original data
    dstdata = srcdata + srcdata      #Secure the write destination area

    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)

  # ------------------------------------------------------------------------------------------------------
  #Generation of mask image
  # ------------------------------------------------------------------------------------------------------
  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

  # ------------------------------------------------------------------------------------------------------
  #Main processing
  # ------------------------------------------------------------------------------------------------------
  args = sys.argv  #Get a list of command line arguments
  argc = len(args) #Number of arguments

  w = False; #Whether to create a mask next to it
  h = False; #Whether or not to create a vertical mask
  o = False; #Output file specification
  in_file = "";    #Input file name
  out_file = "";   #Output file name

  #Check the arguments
  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     [Image file name to read]"
    print u"-w Generate a horizontal mask"
    print u"-h Generate a vertical mask"
    print u"-o Output file name Set the output file name"
    print u"If there is no setting, in the input file_m is added and output"
    sys.exit()

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

  if out_file == "" :
    #If there is no output file name, it will be in the input file_Set with m added
    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 " :
    #To the extension.Set png
    out_file = path + os.path.basename(out_file) + ".png ";

  #Width setting
  w_scale = 1
  if w :
    w_scale = 2

  #Height setting
  h_scale = 1
  if h and not w:
    h_scale = 2

  #Create a mask
  CreateMask(in_file, out_file, w_scale, h_scale);

Finally

Now you can convert it on your mac! Please note that it will not work without the Python Imaging Library installed </ b>.

Recommended Posts

Tool to make mask image for ETC in Python
How to adjust image contrast in Python
A note I looked up to make a command line tool in Python
How to use Python Image Library in python3 series
Try to calculate RPN in Python (for beginners)
How to make Python faster for beginners [numpy]
How to make Python Interpreter changes in Pycharm
Image format in Python
[For beginners] How to use say command in python!
Try to make a Python module in C language
Make each PowerPoint page an image file in Python
Try to make a command standby tool with python
Note assigning image textures to materials in Maya python
Explain in detail how to make sounds with python
How to run python in virtual space (for MacOS)
Convert the image in .zip to PDF with Python
How to make an interactive CLI tool in Golang
How to create an image uploader in Bottle (Python)
Python3> slice copy / slice notation> used in for statements, etc.
Python OpenCV tried to display the image in text.
To flush stdout in Python
Login to website in Python
Search for strings in Python
Techniques for sorting in Python
Don't make test.py in Python!
Tweet with image in Python
Make a bookmarklet in Python
Make Opencv available in Python
Speech to speech in python [text to speech]
Make python segfault in 2 lines
Image Processing Collection in Python
How to develop in Python
~ Tips for beginners to Python ③ ~
Introduction to Python For, While
About "for _ in range ():" in python
Post to Slack in Python
Try to make it using GUI and PyQt in Python
Experiment to make a self-catering PDF for Kindle with Python
A simple way to avoid multiple for loops in Python
How to define multiple variables in a python for statement
Tips for coding short and easy to read in Python
How to make a Python package (written for an intern)
How to specify Cache-Control for blob storage in Azure Storage in Python
Useful tricks related to list and for statements in Python
How to implement Python EXE for Windows in Docker container
Create a tool to check scraping rules (robots.txt) in Python
I tried to make a stopwatch using tkinter in python
I want to make input () a nice complement in python
Convert Excel file to text in Python for diff purposes
[5th] I tried to make a certain authenticator-like tool with python
Check for memory leaks in Python
Things to watch out for when using default arguments in Python
Do you want to wait for general purpose in Python Selenium?
Make python segfault in one line
[Python] How to do PCA in Python
Check for external commands in python
Install Cheminformatics Tool RDKit for Python
Implemented image segmentation in python (Union-Find)
Convert markdown to PDF in Python
[2nd] I tried to make a certain authenticator-like tool with python
Start commands to make CentOS 7 unobtrusive in Parallels Desktop 16 for Mac