パーソナルツール
現在の場所: ホーム takalog Stuff create_tiles.py
書いた本
Plone 完全活用ガイド の Chapter 1, 2, 3, 11 を執筆しました。
plone のインストール、使い方から、機能・デザインのカスタマイズ、プロダクトの作り方まで、 plone のすべてがぎゅっと詰まっている書籍になっていると思います。
plone に興味がある人から、すでに使いこなしている方まで、ぜひ読んでみてください。
Plone 完全活用ガイドのサポートページ
« 2012May »
Su Mo Tu We Th Fr Sa
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
このBlogについて
鈴木たかのりです。とりあえず日記っぽく雑多なことを書き込んでいこうと思っています。 zope/plone関係の技術的な内容については http://takanory.net の方にまとめていこうと思います。 コメント・ツッコミはご自由にどうぞ。
twitter facebook linkedin foursquare
カテゴリ
android (16)
av (27)
books (35)
coreblog (49)
ds (22)
emacs (8)
ferret (24)
google (34)
icecream (44)
lego (70)
mac (22)
misc (74)
moblog (277)
movie (33)
pc (44)
plone (338)
puzzle (42)
python (57)
server (67)
snowscoot (9)
software (125)
sports (32)
suidou (30)
winds (47)
 
文書操作

create_tiles.py

作成者 takanori 最終変更日時 2008年06月04日 12時10分

Click here to get the file

サイズ 2.7 kB - File type text/python-source

ファイルのコンテンツ

# -*- coding: utf-8 -*-
import Image
import os, sys
import math

# タイル画像を生成する
def create_tiles(basename, suffix, image, zoom):
    # グレーのベース画像を作っておく
    gray = Image.new("RGB", (256, 256), (231, 227, 222))

    try:
        os.mkdir("%s/%d" % (basename, zoom))
    except:
        pass

    (width, height) = image.size
    x_l = int(math.ceil(width/256.0))
    y_l = int(math.ceil(height/256.0))

    print "%d x %d tiles" % (x_l, y_l)

    for x in range(x_l):
        for y in range(y_l):
            tile = ""
            if (x == x_l-1 or y == y_l-1):
                # はじっこはサイズを 256x256 に整える
                tile = gray.copy()
                
                right = (x+1)*256
                low = (y+1)*256
                if right > width:
                    right = width
                if low > height:
                    low = height
                tile.paste(image.crop((x*256, y*256, right, low)), (0, 0))
            else:
                tile = image.crop((x*256, y*256, (x+1)*256, (y+1)*256))
            tile.save("%s/%d/%s_%s.%s" % (basename, zoom, x, y, suffix))

def create(basename, suffix, image):
    # タイル画像を保存するディレクトリを作成
    try:
        os.mkdir(basename)
    except:
        pass
    
    # 画像の pixel 数から zoom サイズを決める
    (width, height) = image.size
    length = float(height)
    if (width > height):
        length = float(width)
    length = length/256
    # log2(x)をもとめて zoom を決める
    zoom_size = int(math.ceil(math.log(length) / math.log(2)))

    (width, height) = image.size
    
    for count in range(zoom_size+1):
        zoom = zoom_size - count
        print "zoom", zoom

        map_image = ""
        if count != 0:
            # 1/2^n に縮小した画像を生成する
            map_image = image.resize((width/(2**count), height/(2**count)), Image.ANTIALIAS)
        else:
            map_image = image.copy()
        map_image.save("%s-%d.%s" % (basename, zoom, suffix))

        # 各 zoom 単位のタイル画像を生成する
        create_tiles(basename, suffix, map_image, zoom)

if __name__ == "__main__":
    try:
        # ファイル名を取得
        map_file = sys.argv[1]
        # ファイルの名前と拡張子に分離
        basename = os.path.basename(map_file)
        dot = basename.rfind(".")
        suffix = basename[dot+1:]
        basename = basename[:dot]

        # 画像を読み込む
        image = Image.open(map_file)
        # タイル画像を生成
        create(basename, suffix, image)
    except IndexError:
        print "Usage: create_tiles.py image_file"


Powered by vine linux, python, zope, plone, coreblog