万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    正方形画像リサイズスクリプト Python

    Python2だけw

    square_images.py

    # -*- coding: utf-8 -*-
    from glob import glob
    from wand.image import Image
    import os, sys
    
    def __squareImage( image ) :
        '''
        画像の正方形化
        @param image 画像オブジェクト
        '''
    
        width  = image.width
        height = image.height
    
        # 短辺に合わせたサイズ設定
        left = right = top = bottom = 0
        if width > height :
            left   = ( width - height ) / 2
            right  = left + height
            top    = 0
            bottom = height
        else :
            left   = 0
            right  = width
            top    = ( height - width ) / 2
            bottom = top + width
    
        image.crop( left, top, right, bottom )
    
    def __resizeSquareImage( image, size ) :
        '''
        正方形画像のリサイズ
        @param image 画像オブジェクト (正方形)
        @param size  リサイズ後の1辺のサイズ
        '''
        image.resize( size, size )
    
    def __saveImage( image, path ) :
        '''
        指定されたパスに画像を保存
        @param image
        @param path
        '''
        image.save( filename = path )
    
    def squareImage( src_path, dest_path, size ) :
        '''
        入力パスの画像を正方形変換し、出力パスに保存する。
        @param src_path
        @param dest_path
        @param size      変換サイズ
        '''
        try :
            image = Image( filename = src_path )
    
            __squareImage( image )
            __resizeSquareImage( image, size )
    
            __saveImage( image, dest_path )
            print "Save image \"{0}\".".format( dest_path )
    
        except Exception as e :
            print "Failed to transform image \"{0}\"./{1}".format( src_path, e )
    
    def squareImages( src_dir, dest_dir, size ) :
        '''
        入力ディレクトリ内の画像を正方形変換し、出力ディレクトリ内に保存する。
        ディレクトリは全て存在していることを前提とする。
        @param src_dir
        @param dest_dir
        @param size     変換後サイズ
        '''
    
        src_filenames = glob( src_dir + "/*" )
        for name in src_filenames :
    
            basename  = os.path.basename( name )
            dest_path = "{0}/{1}".format( dest_dir, basename )
            squareImage( name, dest_path, size )
            print(size )
            
    if __name__ == '__main__' :
    
        argv = sys.argv
        argc = len( argv )
    
        if argc < 4 :
            print "Usage: $ python %s <src_dir> <dest_dir> <size>".format( argv[0] )
            quit()
    
        src_dir  = argv[1]
        dest_dir = argv[2]
        size     = argv[3]
    
        # 入力ディレクトリ無ければエラー
        if not os.path.exists( src_dir ) :
            print "Source directory \"{0}\" is not found.".format( src_dir )
    
        # 出力ディレクトリ無ければ作成
        if not os.path.exists( dest_dir ) :
            print "Create destination directory \"{0}\".".format( dest_dir )
            os.mkdir( dest_dir )
    
        # サイズが正しくなければエラー
        try :
            size = int( size )
        except Exception as e :
            print "Size parameter \"{0}\" is incorrect format./ {1}".format( size, e )
            quit()
    
        # 実行
        squareImages( src_dir, dest_dir, size )

    src_dir フォルダを作って512x512なら

    python square_images.py src_dir dest_dir 512