[PYTHON] I want to change the Japanese flag to the Palau flag with Numpy

Introduction

In order to change the Japanese flag to the Palau flag, we performed simple image processing with numpy.

What is the Japanese flag?

** Japanese flag-Wikipedia ** The Japanese flag (Nippon no Kokki, Nihon no Kokki) is legally called the Nissho flag, and since ancient times in Japan, it has been generally called the Hinomaru flag.

--Aspect ratio 2: 3 --Circle diameter 3/5 vertical -Position of the circle Center of the national flag

What is the Palau flag?

** Flag of Palau-Wikipedia ** The flag of the Republic of Palau (Beluu er a Belau) is a flag with a yellow circle arranged slightly closer to the flagpole in the center and a bright blue background.

--Aspect ratio 5: 8 --The position of the circle is 1/10 off the center of the flagpole

code

kokki.py


import cv2
import numpy as np

img = cv2.imread("./test/japan.png ").astype(np.float32)
H, W, C = img.shape

b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

# Gray scale
img = 0.2126 * r + 0.7152 * g + 0.0722 * b
# Binarization
img[img < 100] = 0

H_new = np.ceil((H+W) * 5 / 13).astype(np.int)
W_new = np.ceil((H+W) * 8 / 13).astype(np.int)

_img = np.zeros((H_new, W_new, C), dtype=np.float)
_img[..., 0] = 214
_img[..., 1] = 173
_img[..., 2] = 74

for i in range(H):
	for j in range(W):
		if img[i, j] == 0:
			h = H // 2 - i
			w = W // 2 - j
			_img[H_new//2 - h, W_new//2 - w - H_new//10, 0] = 0
			_img[H_new//2 - h, W_new//2 - w - H_new//10, 1] = 209
			_img[H_new//2 - h, W_new//2 - w - H_new//10, 2] = 255


out = _img.astype(np.uint8)

# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("./test/palau.png ", out)

Create an array in which the size of the Japanese flag is converted to the aspect ratio of the Palau flag. After that, add the background color to the empty array. Since the color code is # 4AADD6, use the value obtained by converting this to BGR. Here, a circle is detected from the binarized Japanese flag using raster scan, the coordinates of the Palau flag are obtained from the x and y coordinates, and BGR is inserted. (Color code is # FFD100)

Execution result

Input image japan.png

Output image palau.png

Bonus (Bangladesh flag)

I'm lonely in Palau alone, so I'll try it with the Bangladesh flag.

kokki2.py


import cv2
import numpy as np

img = cv2.imread("./test/japan.png ").astype(np.float32)
H, W, C = img.shape

b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

# Gray scale
img = 0.2126 * r + 0.7152 * g + 0.0722 * b
# Binarization
img[img < 100] = 0

H_new = np.ceil((H+W) * 3 / 8).astype(np.int)
W_new = np.ceil((H+W) * 5 / 8).astype(np.int)

_img = np.zeros((H_new, W_new, C), dtype=np.float)
_img[..., 0] = 81
_img[..., 1] = 102
_img[..., 2] = 0

for i in range(H):
	for j in range(W):
		if img[i, j] == 0:
			h = H // 2 - i
			w = W // 2 - j
			_img[H_new//2 - h, W_new//2 - w - H_new//10, 0] = 73
			_img[H_new//2 - h, W_new//2 - w - H_new//10, 1] = 6
			_img[H_new//2 - h, W_new//2 - w - H_new//10, 2] = 233


out = _img.astype(np.uint8)

# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("./test/bangladesh.png ", out)

The aspect ratio for Palau is set to 3: 5 and the color is changed.

Execution result bangladesh.png

Summary

Palau and Bangladesh are not much different ...

Recommended Posts

I want to change the Japanese flag to the Palau flag with Numpy
I want to inherit to the back with python dataclass
[Python] I want to use the -h option with argparse
I want to judge the authenticity of the elements of numpy array
I want to get an error message in Japanese with django Password Change Form
I want to do ○○ with Pandas
I want to align the significant figures in the Numpy array
I want to debug with Python
I want to know the weather with LINE bot feat.Heroku + Python
I want to output the beginning of the next month with Python
For the time being, I want to convert files with ffmpeg !!
I want to check the position of my face with OpenCV!
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to display the progress bar
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to change the color by clicking the scatter point in matplotlib
I want to express my feelings with the lyrics of Mr. Children
I want to stop the automatic deletion of the tmp area with RHEL7
I want to analyze songs with Spotify API 2
I tried to save the data with discord
I want to mock datetime.datetime.now () even with pytest!
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I wanted to play with the Bezier curve
I want to be an OREMO with setParam!
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to customize the appearance of zabbix
I want to write to a file with Python
I want to use the activation function Mish
I want to display the progress in Python!
The file edited with vim was readonly but I want to save it
When I tried to change the root password with ansible, I couldn't access it.
I want to write an element to a file with numpy and check it.
I want to use mkl with numpy and scipy under pyenv + poetry environment
I tried to learn the sin function with chainer
I want to convert an image to WebP with lollipop
I want to see the file name from DataLoader
I want to detect unauthorized login to facebook with Jubatus (1)
I also wanted to check type hints with numpy
I want to transition with a button in flask
I want to grep the execution result of strace
I want to scroll the Django shift table, but ...
I want to climb a mountain with reinforcement learning
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I want to fully understand the basics of Bokeh
I want to work with a robot in python.
I want to split a character string with hiragana
[Python3] I want to generate harassment names from Japanese!