Index > 50音インデックス素材の生成ツール(Jython)
Wed, June 20, 2007

50音インデックス素材の生成ツール(Jython)

ウェブサイトで辞書コンテンツをつくる必要があり、50音のインデックス素材を作成した。
何しろ数が多いので、プログラムで生成するようにしました。

画像生成コード

keyImage.py

keyImage.py

import commons

import java.io as io
import java.awt as awt
import java.util as util
import java.lang as lang

import javax.imageio as imageio

false=0
true=1

IMGS=io.File("imgs")
BACKGROUND=awt.Color(0x66,0x66,0x66)

HINTS=awt.RenderingHints(awt.RenderingHints.KEY_ANTIALIASING,awt.RenderingHints.VALUE_ANTIALIAS_ON)
HINTS.put(awt.RenderingHints.KEY_RENDERING,awt.RenderingHints.VALUE_RENDER_QUALITY)


def paintKey(label,fontsize,selected,outfile):

    if label==None:
        label="あ"

    width=25
    height=25

    bimg=commons.createImage(awt.Dimension(width,height))
    g2=bimg.getGraphics()
    #g2.setRenderingHints(HINTS)

    # background
    g2.setColor(BACKGROUND)
    g2.fillRect(0,0,width-1,height-1)


    if selected:
        g2.setColor(awt.Color.WHITE)
        gap=2
        g2.fillOval(gap,gap,width-gap*2,height-gap*2)

        g2.setColor(awt.Color.BLACK)
        g2.setFont(awt.Font(g2.getFont().name,awt.Font.PLAIN,fontsize))
        g2.drawString(label,7,height-8)

    else:
        g2.setColor(awt.Color.WHITE)
        g2.setFont(awt.Font(g2.getFont().name,awt.Font.PLAIN,fontsize))
        g2.drawString(label,7,height-8)

    g2.dispose()

    commons.outputAsPNG(bimg,outfile)


for key in [
["あ","a"],["い","i"],["う","u"],["え","e"],["お","o"],
["か","ka"],["き","ki"],["く","ku"],["け","ke"],["こ","ko"],
["さ","sa"],["し","si"],["す","su"],["せ","se"],["そ","so"],
["た","ta"],["ち","ti"],["つ","tu"],["て","te"],["と","to"],
["な","na"],["に","ni"],["ぬ","nu"],["ね","ne"],["の","no"],
["は","ha"],["ひ","hi"],["ふ","fu"],["へ","he"],["ほ","ho"],
["ま","ma"],["み","mi"],["む","mu"],["め","me"],["も","mo"],
["や","ya"],["ゆ","yu"],["よ","yo"],
["ら","ra"],["り","ri"],["る","ru"],["れ","re"],["ろ","ro"],
["わ","wa"],["を","wo"],["ん","nn"]
]:
    paintKey(key[0],12,false,io.File(IMGS,key[1]+".png"))
    paintKey(key[0],12,true,io.File(IMGS,key[1]+"_.png"))

tabImage.py

tabImage.py

import commons

import java.io as io
import java.awt as awt
import java.util as util
import java.lang as lang


false=0
true=1

IMGS=io.File("imgs")
BACKGROUND=awt.Color(0x66,0x66,0x66)

HINTS=awt.RenderingHints(awt.RenderingHints.KEY_ANTIALIASING,awt.RenderingHints.VALUE_ANTIALIAS_ON)
HINTS.put(awt.RenderingHints.KEY_RENDERING,awt.RenderingHints.VALUE_RENDER_QUALITY)


def paintTab(label,fontsize,selected,outfile):

    if label==None:
        label="あ"

    width=60
    height=25

    bimg=commons.createImage(awt.Dimension(width,height))
    g2=bimg.getGraphics()

    # background
    g2.setColor(awt.Color.WHITE)
    g2.fillRect(0,0,width-1,height-1)


    g2.setColor(awt.Color(192,192,192))
    if selected:
        g2.setColor(BACKGROUND)#awt.Color(102,153,102))

    g2.fillRoundRect(0,0,width-1,height-1,10,10)
    g2.fillRect(0,height-10,width-1,height-1)

    g2.setColor(awt.Color.BLACK)
    if selected:
        g2.setColor(awt.Color.WHITE)

    g2.drawString(label+"行",18,height-8)

    commons.outputAsPNG(bimg,outfile)


for key in [
    ["あ","a"],["か","ka"],["さ","sa"],["た","ta"],["な","na"],
    ["は","ha"],["ま","ma"],["や","ya"],["ら","ra"],["わ","wa"]
        ]:
    paintTab(key[0],12,false,io.File(IMGS,"tab_"+key[1]+".png"))
    paintTab(key[0],12,true,io.File(IMGS, "tab_"+key[1]+"_.png"))

commons.py

commons.py

import java.io as io
import java.awt as awt

import javax.imageio as imageio

false=0
true=1

def outputAsPNG(bimg,outfile):
    out=io.FileOutputStream(outfile)
    imageio.ImageIO.write(bimg,"png",out)
    out.close()


def createImage(size):
    bimg=awt.image.BufferedImage(size.width,size.height,awt.image.BufferedImage.TYPE_4BYTE_ABGR)
    return bimg
 Twitter
follow me on Twitter
 Categories