[画像を、別の画像に、重ね塗りする] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり
Python(パイソン) プログラム作ってみた インデックス へ
-----
2024.9.24 presented in [note] ( //note.com/runningWater/ )
----------
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。
以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。
しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。
その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。
このプログラムは、Python(パイソン) 言語を使って、記述されている。
----------
2 どのようなものを作ったのか
以下、使用例で、説明する。
以下に説明するプログラムを使って、
下図 [Fig 1] の画像と、下図 [Fig 2] の画像より、下図 [Fig 3]の画像が、作成された。
Fig 3 画像は、Fig 1 画像を、Fig 2 画像の上に、重ね塗りしたような内容になっている。
以降、下記のような用語を用いる。
[コピー元・画像] Fig 1 に示されている画像
[挿入される側・ソース画像] Fig 2 に示されている画像
[挿入される側・生成画像] Fig 3 に示されている画像
この処理においては、以下のようなパラメーターを、調整できるようになっている。
color_copying
[コピー元・画像]中のピクセルのうち、ここで指定された色の範囲を持つものだけが、コピー対象となる
CopySource_From_and_To_xy
[コピー元・画像]中のピクセルのうち、ここで指定された位置(XY座標)の範囲を持つものだけが、コピー対象となる
scale_rate_CopySource_to_InsertedSide
[コピー元・画像] から [挿入される側・生成画像] への出力の際の、縮尺を、指定する
color_rate_CopySource_to_InsertedSide
[コピー元・画像] から [挿入される側・生成画像] への出力の際の、
[コピー元・画像] と [挿入される側・ソース画像] との(色の)混合比率を、指定する
Center_xy_of_inserting_on_InsertedSide_Source
[挿入される側・生成画像]の中の、挿入対象となる範囲の中心位置(XY座標)を、指定する
----------
このプログラムは、適宜、下記で説明されているモジュールを、使用している。
[[画像処理, OpenCV2 使用] を 行う, 機能追加版, 多角形(内部ぬりつぶし)描画機能を追加] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり
----------
3 以降に、このモジュールの内容を、記す。
ファイル名 [ CopyAndInsertImageData.py ]
----------
import ImageDataTwoDimensionHandlingV3
MODULE_NAME = "Copy_and_Insert_ImageData"
#=================================
def copy_and_insert_ImageData (
arg_abs_path_of_CopySource_ImageData
, arg_abs_path_of_InsertedSide_Source_ImageData
, arg_abs_path_of_InsertedSide_Generated_ImageData
, arg_list_color_copying
, arg_list_CopySource_From_and_To_xy
, arg_scale_rate_CopySource_to_InsertedSide
, arg_color_rate_CopySource_to_InsertedSide
, arg_list_Center_xy_of_inserting_on_InsertedSide_Source
) :
function_name = "copy_and_insert_ImageData"
print ( "==================================" )
print ( "Enter into , Module = " + MODULE_NAME
+ " , function = " + function_name )
print ( "==================================" )
ins_ImageDataTwoDimensionHandling_CopySource = \
ImageDataTwoDimensionHandlingV3 \
.ImageDataTwoDimensionHandlingV3 ( MODULE_NAME , function_name )
ins_ImageDataTwoDimensionHandling_CopySource \
.load_image_data ( \
MODULE_NAME \
, function_name
, arg_abs_path_of_CopySource_ImageData
)
#-------------------------
ins_ImageDataTwoDimensionHandling_InsertedSide_Source = \
ImageDataTwoDimensionHandlingV3 \
.ImageDataTwoDimensionHandlingV3 ( MODULE_NAME , function_name )
ins_ImageDataTwoDimensionHandling_InsertedSide_Source \
.load_image_data ( \
MODULE_NAME \
, function_name
, arg_abs_path_of_InsertedSide_Source_ImageData
)
#-------------------------
ins_ImageDataTwoDimensionHandling_InsertedSide_Generated = \
ImageDataTwoDimensionHandlingV3 \
.ImageDataTwoDimensionHandlingV3 ( MODULE_NAME , function_name )
ins_ImageDataTwoDimensionHandling_InsertedSide_Generated \
.load_image_data ( \
MODULE_NAME \
, function_name
, arg_abs_path_of_InsertedSide_Source_ImageData
)
#-------------------------
list_center_xy_of_copying_area = [ \
# from x
( arg_list_CopySource_From_and_To_xy [ 0 ] [ 0 ] \
+ \
# to x
arg_list_CopySource_From_and_To_xy [ 1 ] [ 0 ] \
) / 2.0 \
, \
# from y
( arg_list_CopySource_From_and_To_xy [ 0 ] [ 1 ] \
+ \
# to y
arg_list_CopySource_From_and_To_xy [ 1 ] [ 1 ] \
) / 2.0 \
]
from_x = arg_list_CopySource_From_and_To_xy [ 0 ] [ 0 ] \
- list_center_xy_of_copying_area [ 0 ]
to_x = arg_list_CopySource_From_and_To_xy [ 1 ] [ 0 ] \
- list_center_xy_of_copying_area [ 0 ]
from_y = arg_list_CopySource_From_and_To_xy [ 0 ] [ 1 ] \
- list_center_xy_of_copying_area [ 1 ]
to_y = arg_list_CopySource_From_and_To_xy [ 1 ] [ 1 ] \
- list_center_xy_of_copying_area [ 1 ]
#-------------------------------------------------
x = from_x
while ( x <= to_x ) :
print ( "==================================" )
print ( "Module = " + MODULE_NAME
+ " , function = " + function_name )
print ( "x = " , x )
print ( "==================================" )
y = from_y
while ( y <= to_y ) :
process_for_one_location_x_y ( \
x
, y
, ins_ImageDataTwoDimensionHandling_CopySource
, ins_ImageDataTwoDimensionHandling_InsertedSide_Source
, ins_ImageDataTwoDimensionHandling_InsertedSide_Generated
, arg_list_color_copying
, list_center_xy_of_copying_area
, arg_scale_rate_CopySource_to_InsertedSide
, arg_color_rate_CopySource_to_InsertedSide
, arg_list_Center_xy_of_inserting_on_InsertedSide_Source
)
#-----------------------------
y += 1
#-----------------------------
x += 1
#-------------------------------------
ins_ImageDataTwoDimensionHandling_InsertedSide_Generated \
.write_loaded_image_data_to_directed_path ( \
MODULE_NAME \
, function_name
, arg_abs_path_of_InsertedSide_Generated_ImageData
)
print ( "==================================" )
print ( "Exit from , Module = " + MODULE_NAME
+ " , function = " + function_name )
print ( "==================================" )
#=================================
def process_for_one_location_x_y ( \
arg_x
, arg_y
, arg_ins_ImageDataTwoDimensionHandling_CopySource
, arg_ins_ImageDataTwoDimensionHandling_InsertedSide_Source
, arg_ins_ImageDataTwoDimensionHandling_InsertedSide_Generated
, arg_list_color_copying
, arg_list_center_xy_of_copying_area
, arg_scale_rate_CopySource_to_InsertedSide
, arg_color_rate_CopySource_to_InsertedSide
, arg_list_Center_xy_of_inserting_on_InsertedSide_Source
) :
function_name = "process_for_one_location_x_y"
# print ( "==================================" )
# print ( "Enter into , Module = " + MODULE_NAME
# + " , function = " + function_name )
# print ( "arg_x = " , arg_x )
# print ( "arg_y = " , arg_y )
# print ( "==================================" )
get_location_x = int ( arg_list_center_xy_of_copying_area [ 0 ] + arg_x )
get_location_y = int ( arg_list_center_xy_of_copying_area [ 1 ] + arg_y )
w_x = arg_x * arg_scale_rate_CopySource_to_InsertedSide
w_y = arg_y * arg_scale_rate_CopySource_to_InsertedSide
put_location_x = int ( arg_list_Center_xy_of_inserting_on_InsertedSide_Source [ 0 ]\
+ w_x )
put_location_y = int ( arg_list_Center_xy_of_inserting_on_InsertedSide_Source [ 1 ] \
+ w_y )
#--------------------------------------
list_color_data_for_put = decide_color_for_put ( \
arg_ins_ImageDataTwoDimensionHandling_CopySource
, arg_ins_ImageDataTwoDimensionHandling_InsertedSide_Source
, arg_list_color_copying
, arg_color_rate_CopySource_to_InsertedSide
, get_location_x
, get_location_y
, put_location_x
, put_location_y
)
if ( list_color_data_for_put [ 0 ] == "N" ) :
# on this location, not draw color exist
# so, this location must not be draw
return "N"
#------------------------------------
#------------------------------------
arg_ins_ImageDataTwoDimensionHandling_InsertedSide_Generated \
.put_data_of_one_pixel ( \
MODULE_NAME
, function_name
# in the commonly used mathematical XY cordinates
, put_location_x
, put_location_y
, list_color_data_for_put [ 1 ] # arg_color_data_Blue
, list_color_data_for_put [ 2 ] #arg_color_data_Green
, list_color_data_for_put [ 3 ] #arg_color_data_Red
)
#------------------------------------------------------------
def decide_color_for_put ( \
arg_ins_ImageDataTwoDimensionHandling_CopySource
, arg_ins_ImageDataTwoDimensionHandling_InsertedSide_Source
, arg_list_color_copying
, arg_color_rate_CopySource_to_InsertedSide
, arg_get_location_x
, arg_get_location_y
, arg_put_location_x
, arg_put_location_y
) :
function_name = "decide_color_for_put"
#print ( "==================================" )
#print ( "Enter into , Module = " + MODULE_NAME
# + " , function = " + function_name )
#print ( "arg_abs_path_of_ImageData_Basic = " \
# + arg_abs_path_of_ImageData_Basic )
#print ( "==================================" )
list_return = [ "Y" , 0 , 0 , 0 ]
color_data_CopySource = \
arg_ins_ImageDataTwoDimensionHandling_CopySource \
.get_data_of_one_pixel ( \
MODULE_NAME
, function_name
# in the commonly used mathematical XY cordinates
, arg_get_location_x
, arg_get_location_y
)
#-----------------------------------
if ( \
# Blue
# from
( color_data_CopySource [ 0 ] < arg_list_color_copying [ 0 ] [ 0 ] ) \
or \
# to
( color_data_CopySource [ 0 ] > arg_list_color_copying [ 1 ] [ 0 ] ) \
or \
# Green
# from
( color_data_CopySource [ 1 ] < arg_list_color_copying [ 0 ] [ 1 ] ) \
or \
# to
( color_data_CopySource [ 1 ] > arg_list_color_copying [ 1 ] [ 1 ] ) \
or \
# Red
# from
( color_data_CopySource [ 2 ] < arg_list_color_copying [ 0 ] [ 2 ] ) \
or \
# to
( color_data_CopySource [ 2 ] > arg_list_color_copying [ 1 ] [ 2 ] ) \
) :
# this location has color , that indicate, must not be drawn
list_return [ 0 ] = "N"
list_return [ 1 ] = color_data_CopySource [ 0 ]
list_return [ 2 ] = color_data_CopySource [ 1 ]
list_return [ 3 ] = color_data_CopySource [ 2 ]
return list_return
#----------------------------------------------------
color_data_InsertedSideSource = \
arg_ins_ImageDataTwoDimensionHandling_InsertedSide_Source \
.get_data_of_one_pixel ( \
MODULE_NAME
, function_name
# in the commonly used mathematical XY cordinates
, arg_put_location_x
, arg_put_location_y
)
#----------------------------------------------------
for i in range ( 0 , 3 ) :
w_color_CopySource = \
color_data_CopySource [ i ] \
* arg_color_rate_CopySource_to_InsertedSide
w_color_InsertedSideSource = \
color_data_InsertedSideSource [ i ] \
* ( 1.0 - arg_color_rate_CopySource_to_InsertedSide )
w_color_mixed = int ( w_color_CopySource + w_color_InsertedSideSource )
index_for_return = i + 1
list_return [ index_for_return ] = w_color_mixed
if ( w_color_mixed < 0 ) :
list_return [ index_for_return ] = 0
if ( w_color_mixed > 255 ) :
list_return [ index_for_return ] = 255
#----------------------------------------
return list_return
#=====================================
#=====================================
# Test
#=====================================
module_name = "___"
function_name = "Test"
#***************************************
abs_path_of_CopySource_ImageData \
= "E:ForPython/ForTest/Test_12/ImageData_1.png"
abs_path_of_InsertedSide_Source_ImageData \
= "E:ForPython/ForTest/Test_12/ImageData_2.png"
abs_path_of_InsertedSide_Generated_ImageData \
= "E:ForPython/ForTest/Test_12/ImageData_3.png"
list_CopySource_From_and_To_xy = [ [ 0 , 0 ] , [ 1500 , 780 ] ]
list_color_copying = [ [ 0 , 0 , 0 ] , [ 250 , 250 , 250 ] ]
scale_rate_CopySource_to_InsertedSide = 0.5
color_rate_CopySource_to_InsertedSide = 0.7
list_Center_xy_of_inserting_on_InsertedSide_Source = [ 500 , 250 ]
copy_and_insert_ImageData (
abs_path_of_CopySource_ImageData
, abs_path_of_InsertedSide_Source_ImageData
, abs_path_of_InsertedSide_Generated_ImageData
, list_color_copying
, list_CopySource_From_and_To_xy
, scale_rate_CopySource_to_InsertedSide
, color_rate_CopySource_to_InsertedSide
, list_Center_xy_of_inserting_on_InsertedSide_Source
)
#-------------------------------------------------------------
----------
この記事が気に入ったらサポートをしてみませんか?