I implemented the mask making tool introduced in the previous article in python.
Python 2.7.9 Python Imaging Library 1.1.7
A mask image is output when an image file is inserted. CreateMask.py [Mask source file]
[-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);
Now you can convert it on your mac! Please note that it will not work without the Python Imaging Library installed </ b>.
Recommended Posts