[寄木細工(よせぎざいく)]のような、スタイルの画像を、作る] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

Python(パイソン) プログラム作ってみた インデックス へ

-----

2024.10.13 presented in [note] ( //note.com/runningWater/ )

----------
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。

以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。

しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。

その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。

このプログラムは、Python(パイソン) 言語を使って、記述されている。

----------
2 どのようなものを作ったのか

下記 [Fig 1] にあるように、[Fig 1]上部にある3個の画像から、[Fig 1]下部にある1個の画像を、作成した。

Fig 1

[寄木細工(よせぎざいく)]を、連想するような、スタイルのものを、作った、という感じだ。

この作成は、以下のように、二段がまえで、行った。

-----
第1段階(Phase_1)

下記 [Fig 2] にあるように、
[3個の画像 ( [画像1]、[画像2]、[画像3] ] から、
 [Phase_1 出力画像] を、作成した。
[パターン画像]中の、各部分の色に応じて、
それに対応する、[ベース画像1]の各部に、
[3個の画像]の、それぞれを、コピーすることにより、
[Phase_1 出力画像]が、作成された。

Fig 2

-----
第2段階(Phase_2)

下記 [Fig 3] にあるように、
[Phase_1 出力画像]を、
[ベース画像2] の上に、
規則的に、順にコピーしていった(回転させながら)

そのようにして、[Fig 1]下部にある画像が、作成された。

Fig 3

----------
3 構成

このモジュール群は、以下のモジュールから、構成されている。

 [ CopyAndInsertImageDataV2 ]
 [ MakeYosegi ]
 
これらのモジュールは、適宜、下記で説明されているモジュールを、使用している。

[画像処理, OpenCV2 使用] を 行う, 機能追加版, 多角形(内部ぬりつぶし)描画機能を追加] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた ,[エラー処理] を 行う

以降に、これらのモジュールの内容を、記す。

----------
4 [ CopyAndInsertImageDataV2 ]

このモジュールは、

[画像を、別の画像に、重ね塗りする] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

に解説されている、
ファイル名 [ CopyAndInsertImageDataV2.py ]
のモジュールを、そのままコピーして、流用した。

----------
5 [ MakeYosegi ]

ファイル名 [ MakeYosegi.py ]

下記ソースリスト中の、
[def do_phase_1_process]
が、
2 で述べた、 第1段階(Phase_1)
に、対応する処理であり、

[def do_phase_2_process]
が、
2 で述べた、 第2段階(Phase_2)
に、対応する処理である。

----------


import  CopyAndInsertImageDataV2

#=====================================
# MakeYosegi
#=====================================

global_module_name = "MakeYosegi"

#------------------------------------------------
def  main ( )  :

    function_name = "main"

    global  global_module_name

    print ( "==================================" )
    print ( "Enter into , Module = " + global_module_name
                      + " , function = " + function_name )
    print ( "==================================" )

    ins_ImageDataTwoDimensionHandlingV3_output_of_phase_1  \
           = do_phase_1_process ( )
    do_phase_2_process (   \
           ins_ImageDataTwoDimensionHandlingV3_output_of_phase_1 )

    print ( "==================================" )
    print ( "Exit from , Module = " + global_module_name
                      + " , function = " + function_name )
    print ( "==================================" )

#***************************************
#-----------------------------------------------
# Phase 1
#-----------------------------------------------
def  do_phase_1_process ( ) :

    function_name = "do_phase_1_process"

    global  global_module_name

    print ( "==================================" )
    print ( "Enter into , Module = " + global_module_name
                      + " , function = " + function_name )
    print ( "==================================" )

    list_using_ImageData =  [     \
                 [ "Wood_1_200_200.png"  ,  67   ,  33 ]   \
             ,   [ "Wood_2_200_200.png"  ,  31   ,  34 ]    \
             ,   [ "Wood_3_200_200.png"  ,  12   ,  13 ]    \
                                         ]

    abs_path_of_CopySource_Pattern_ImageData_for_phase_1      \
            = "E:ForPython/ForTest/Test_B/Input/Yosegi_Pattern_200_200.png"
    abs_path_of_InsertedSide_Base_ImageData_for_phase_1    \
            = "E:ForPython/ForTest/Test_B/Input/BaseImage_1_200_200.png"

    list_CopySource_From_and_To_xy_for_phase_1 = [ [ 0 , 0 ] , [ 199 , 199 ] ]
    list_color_copying_for_phase_1 = [ [ 0 , 0 , 0 ] , [ 253 , 253 , 253 ] ]
    list_Center_xy_of_inserting_on_InsertedSide_Source_for_phase_1 \
            = [ 100 , 100 ]

    ins_CopyAndInsertImageDataV2_for_phase_1 = \
            CopyAndInsertImageDataV2        \
                   .CopyAndInsertImageDataV2 ( )
               #--------------------
    ins_CopyAndInsertImageDataV2_for_phase_1            \
               .construct_ImageData ( 1 )
    ins_CopyAndInsertImageDataV2_for_phase_1            \
               .load_ImageData (  1 \
                   , abs_path_of_CopySource_Pattern_ImageData_for_phase_1 )
               #-------------------
    ins_CopyAndInsertImageDataV2_for_phase_1            \
               .construct_ImageData ( 2 )
    ins_CopyAndInsertImageDataV2_for_phase_1            \
               .load_ImageData (  2   \
                   , abs_path_of_InsertedSide_Base_ImageData_for_phase_1 )
               #-------------------
    ins_CopyAndInsertImageDataV2_for_phase_1            \
               .construct_ImageData ( 3 )
    ins_CopyAndInsertImageDataV2_for_phase_1            \
               .load_ImageData (  3 ,  abs_path_of_InsertedSide_Base_ImageData_for_phase_1 )
               #-------------------
    list_directed_x_and_y_on_PatternImageData = [ 0 , 0 ]

               #-------------------
    for i in range ( 0 , 3 ) :
         file_name = list_using_ImageData  [ i ] [ 0 ]
         abs_path_of_CopySource_ImageData      \
                = "E:ForPython/ForTest/Test_B/Input/"   \
                           + file_name
         list_directed_x_and_y_on_PatternImageData [ 0 ]  \
                 =  list_using_ImageData [ i ] [ 1 ]
         list_directed_x_and_y_on_PatternImageData [ 1 ]  \
                 =  list_using_ImageData [ i ] [ 2 ]

         do_drawing_of_phase_1_process (     \
                   ins_CopyAndInsertImageDataV2_for_phase_1

               ,   abs_path_of_CopySource_ImageData  \

               ,  list_directed_x_and_y_on_PatternImageData

               ,  list_CopySource_From_and_To_xy_for_phase_1
               ,  list_color_copying_for_phase_1
               ,  list_Center_xy_of_inserting_on_InsertedSide_Source_for_phase_1
               )
   #-------------------------------
    abs_path_of_InsertedSide_Generated_ImageData_for_phase_1    \
            = "E:ForPython/ForTest/Test_B/Output/ImageData_Output_1.png"
    ins_CopyAndInsertImageDataV2_for_phase_1   \
            .write_InsertedSide_Generated_ImageData (   \
                        abs_path_of_InsertedSide_Generated_ImageData_for_phase_1
                                                                      )
    #-----------------------------------
    return_instance = ins_CopyAndInsertImageDataV2_for_phase_1   \
                                         .get_ImageData ( 3 )

    print ( "==================================" )
    print ( "Exit from , Module = " + global_module_name
                      + " , function = " + function_name )
    print ( "==================================" )

    return  return_instance

#-------------------------------------------------------------
#-----------------------------------------------
# Phase 2
#-----------------------------------------------
def  do_phase_2_process (  \
           arg_ins_ImageDataTwoDimensionHandlingV3_output_of_phase_1 ) :

    function_name = "do_phase_2_process"

    global  global_module_name

    print ( "==================================" )
    print ( "Enter into , Module = " + global_module_name
                      + " , function = " + function_name )
    print ( "==================================" )

    #----------------------------------------
    list_rotating_angle_degree =  [  ]
    list_rotating_angle_degree  \
         .append ( [ 0 , 90 , 180 , 270 , 0 , 90 ] )
    list_rotating_angle_degree  \
         .append ( [ 90 , 180 , 270 , 0 , 90 , 180 ] )
    list_rotating_angle_degree  \
         .append ( [ 180 , 270 , 0 , 90 , 180 , 270 ] )
    list_rotating_angle_degree  \
         .append ( [ 270 , 0 , 90 , 180 , 270 , 0 ] )
    list_rotating_angle_degree  \
         .append ( [ 0 , 90 , 180 , 270 , 0 , 90 ] )
    list_rotating_angle_degree  \
         .append ( [ 90 , 180 , 270 , 0 , 90 , 180 ] )
    list_rotating_angle_degree  \
         .append ( [ 180 , 270 , 0 , 90 , 180 , 270 ] )
    list_rotating_angle_degree  \
         .append ( [ 270 , 0 , 90 , 180 , 270 , 0 ] )

    #--------------------------------
    abs_path_of_InsertedSide_Base_ImageData_2    \
            = "E:ForPython/ForTest/Test_B/Input/BaseImage_2_1200_1600.png"

#-------------------------------------
    ins_CopyAndInsertImageDataV2_for_phase_2 = \
                CopyAndInsertImageDataV2        \
                       .CopyAndInsertImageDataV2 ( )
               #--------------------
    ins_CopyAndInsertImageDataV2_for_phase_2            \
               .construct_ImageData ( 0 )

#************************************************************
    ins_CopyAndInsertImageDataV2_for_phase_2            \
            .set_ImageData (  0                                      \
                  ,  arg_ins_ImageDataTwoDimensionHandlingV3_output_of_phase_1 )
#************************************************************

               #-------------------
    ins_CopyAndInsertImageDataV2_for_phase_2            \
               .construct_ImageData ( 2 )
    ins_CopyAndInsertImageDataV2_for_phase_2            \
               .load_ImageData (  2 ,  abs_path_of_InsertedSide_Base_ImageData_2 )
               #-------------------
    ins_CopyAndInsertImageDataV2_for_phase_2            \
               .construct_ImageData ( 3 )
    ins_CopyAndInsertImageDataV2_for_phase_2            \
               .load_ImageData (  3 ,  abs_path_of_InsertedSide_Base_ImageData_2 )
               #-------------------
    use_or_not_PatternImageData_phase_2 = "N"
    list_directed_x_and_y_on_PatternImageData_phase_2 = [ 0 , 0 ]
    list_CopySource_From_and_To_xy_phase_2 = [ [ 0 , 0 ] , [ 199 , 199 ] ]
    list_color_copying_phase_2 = [ [ 0 , 0 , 0 ] , [ 253 , 253 , 253 ] ]

    list_Center_xy_of_inserting_on_InsertedSide_Source_phase_2 = [ 100 , 100 ]

    scale_rate_CopySource_to_InsertedSide_phase_2 = 1.0
    color_rate_CopySource_to_InsertedSide_phase_2 = 1.0

    do_rotation_phase_2 = "Y"

#-----------------------------------------------
    for i in range ( 0 , 8 ) :
        for j in range ( 0 , 6 ) :
        
            print ( "==================================" )
            print ( "Module = " + global_module_name
                      + " , function = " + function_name )
            print ( "i = " , i , " , j = " , j )
            print ( "==================================" )
    
            rotation_degree_of_inserting_on_InsertedSide_Source_phase_2  \
                     = list_rotating_angle_degree [ i ] [ j ]

            center_x = 100 + ( j * 200 )
            center_y = 100 + ( i * 200 )
            list_Center_xy_of_inserting_on_InsertedSide_Source_phase_2 \
                = [ center_x , center_y ]

            ins_CopyAndInsertImageDataV2_for_phase_2  \
                .copy_and_insert_ImageData (   \
                            use_or_not_PatternImageData_phase_2
                        ,   list_directed_x_and_y_on_PatternImageData_phase_2
                         ,  list_color_copying_phase_2
                         ,  list_CopySource_From_and_To_xy_phase_2
                         ,  scale_rate_CopySource_to_InsertedSide_phase_2
                         ,  color_rate_CopySource_to_InsertedSide_phase_2

                         ,  list_Center_xy_of_inserting_on_InsertedSide_Source_phase_2
                         ,  do_rotation_phase_2
                         ,  rotation_degree_of_inserting_on_InsertedSide_Source_phase_2
                                           )
   #-------------------------------
    abs_path_of_InsertedSide_Generated_ImageData_phase_2    \
            = "E:ForPython/ForTest/Test_B/Output/ImageData_Output_2.png"
    ins_CopyAndInsertImageDataV2_for_phase_2   \
            .write_InsertedSide_Generated_ImageData (   \
                        abs_path_of_InsertedSide_Generated_ImageData_phase_2
                                                                      )

    print ( "==================================" )
    print ( "Exit from  , Module = " + global_module_name
                      + " , function = " + function_name )
    print ( "==================================" )

#===============================================
def  do_drawing_of_phase_1_process (     \
                  arg_ins_CopyAndInsertImageDataV2_for_phase_1

               ,  arg_abs_path_of_CopySource_ImageData  \

               ,  arg_list_directed_x_and_y_on_PatternImageData

               ,  arg_list_CopySource_From_and_To_xy
               ,  arg_list_color_copying
               ,  arg_list_Center_xy_of_inserting_on_InsertedSide_Source
                                   ) :

    function_name = "do_drawing_of_phase_1_process"

    global  global_module_name

    print( "==================================" )
    print( "Enter into , Module = " , global_module_name )
    print( "                  function = " , function_name )
    print ( "arg_abs_path_of_CopySource_ImageData = "  \
                            , arg_abs_path_of_CopySource_ImageData )
    print( "==================================" )

    arg_ins_CopyAndInsertImageDataV2_for_phase_1            \
               .construct_ImageData ( 0 )
    arg_ins_CopyAndInsertImageDataV2_for_phase_1            \
               .load_ImageData (  0 ,  arg_abs_path_of_CopySource_ImageData )
          #----------------------
    use_or_not_PatternImageData = "Y"   # use PatternImageData
       #-----------
    scale_rate_CopySource_to_InsertedSide = 1.0
    color_rate_CopySource_to_InsertedSide = 1.0
    do_rotation = "N"
    rotation_degree_of_inserting_on_InsertedSide_Source = 0
       #-----------
    arg_ins_CopyAndInsertImageDataV2_for_phase_1  \
        .copy_and_insert_ImageData (   \
                            use_or_not_PatternImageData
                        ,   arg_list_directed_x_and_y_on_PatternImageData
                         ,  arg_list_color_copying
                         ,  arg_list_CopySource_From_and_To_xy
                         ,  scale_rate_CopySource_to_InsertedSide
                         ,  color_rate_CopySource_to_InsertedSide
                         ,  arg_list_Center_xy_of_inserting_on_InsertedSide_Source
                         ,  do_rotation
                         ,  rotation_degree_of_inserting_on_InsertedSide_Source
                                           )

    print( "==================================" )
    print( "Exit from , Module = " , global_module_name )
    print( "                  function = " , function_name )
    print( "==================================" )

#==============================
if __name__ == "__main__":
    main ( )


----------

Python(パイソン) プログラム作ってみた インデックス へ


この記事が気に入ったらサポートをしてみませんか?