[PYTHON] I made a dot picture of the image of Irasutoya. (part1)

Motive As you know, Irasutoya continues to release trendy free materials. Suddenly I was wondering what would happen if these images were made into pixel art, so I created it.

Method As I wrote in Wizard's Forest, it is said that the NES display color scheme is about 52. (Not deterministic.) Here, in order to make it look like a NES, I will try to reduce the color scheme of each of R, G, B into 4 parts and display it.

Each range is 0 to 255, but if you simply divide it, you get[0, 85, 170, 255].

All color combinations have a 64 pattern, but when you actually output it as an image, it will be displayed as follows.

import cv2
import numpy as np

if __name__ == "__main__":
	height = 576
	width = 1024
	range_color = [0, 85, 170, 255]

	mono = np.zeros((height, width, 3), np.uint8)
	mono[:] = tuple((0,0,0))

	for r in range(4):
		for g in range(4):
			for b in range(4):
				mono[144*b:144*(b+1), 64*(r+g*4):64*(r+(g*4+1))] = tuple((85*b,85*g,85*r))

	cv2.imwrite("out.png ", mono)

out.png

As an aside, I thought that there was no gray color at first glance, but it is displayed in 2 rows and 6 columns (85,85,85) and 3 rows and 11 columns(170,170,170), respectively.

So, how to judge by the number of the original color data, but as an example, if you want to distribute the value of 110, judge which is closer to 85 ~ 170. Since 85 is the closest here, convert it to 110-> 85 and apply this algorithm to all pixels.

Develop

import cv2
import numpy as np
import sys


def resize(src):
	h,w = mat.shape[:-1]
	height = (h // 16) * 16
	width = (w // 16)* 16
	return cv2.resize(mat,(width,height))

def convertReduceColor(src):
	thresholds = [42,127,212]
	range_color = [0, 85, 170, 255]

	count = 0
	for th in thresholds:
		if src <= th:
			break
		count += 1
	return range_color[count]


if __name__ == "__main__":
	__CELL_SIZE__ = 4
	path = sys.argv[1]
	mat = cv2.imread(path,cv2.IMREAD_UNCHANGED)
	mat = resize(mat)

	height, width = mat.shape[:-1]

	for w in range(width//__CELL_SIZE__-1):
		for h in range(height//__CELL_SIZE__-1):
			c = np.mean(
						np.mean(
									mat[h*__CELL_SIZE__:(h+1)*__CELL_SIZE__, 
									w*__CELL_SIZE__:(w+1)*__CELL_SIZE__], axis=0
								), 
						axis=0
					)
			mat[
				h*__CELL_SIZE__:(h+1)*__CELL_SIZE__, 
				w*__CELL_SIZE__:(w+1)*__CELL_SIZE__
				] = tuple([convertReduceColor(c[0]), convertReduceColor(c[1]), convertReduceColor(c[2]), c[3]])

	cv2.imwrite("output.png ",mat)

np.mean(np.mean(mat[h*__CELL_SIZE__:(h+1)*__CELL_SIZE__, w*__CELL_SIZE__:(w+1)*__CELL_SIZE__], axis=0),axis=0) Here, the average value of pixels is obtained from the cell size. In addition, convertReduceColor is used to make an approximation judgment for each of RGB.

Result

Source image Pixel size After treatment
group_young_people.png 4 output_group.png
group_young_people.png 8 output.png
group_young_people.png 16 output.png
animal_chara_computer_penguin.png 4 output.png
animal_chara_computer_penguin.png 8 output.png
animal_chara_computer_penguin.png 16 output.png
cat_koubakozuwari_gray.png 4 output.png
cat_koubakozuwari_gray.png 8 output.png
cat_koubakozuwari_gray.png 16 output.png

――The most successful image is a cat with a cell size of 16. The right end has not been processed, but I think that it is a dot picture like that as a whole. If you want to make the right edge dot-like, you have to reconsider the resizing process. ――When converting to a pixel art, it is easier to succeed if there is one person / object in one illustration. ――Basically, it is an image of Irasutoya, but the feature is that the section is filled with uneven felt tone. Therefore, it looks like an object is not filled with a single color and contains noise. --The eyes of the penguins image with the cell size set to 4 are (´ ・ ω ・ `). Please note that the character of Irasutoya basically has eyes as dots and is small, so it disappears when the cell size is increased.

Future ――Since I'm worried about noise, it seems quite so with irregular binarization and morphology conversion. ――The actual NES color is different from the color scheme used this time, so it seems better to embed about 50 color schemes to be used with fixed values and match them by neighborhood search.

ファミコンの画面について.png

Reference -Irasutoya -About the NES screen-Wizard's Forest

Recommended Posts

I made a dot picture of the image of Irasutoya. (part1)
I made a dot picture of the image of Irasutoya. (part2)
I made a function to check the model of DCGAN
I made a function to crop the image of python openCV, so please use it.
I made a slack bot that notifies me of the temperature
[Kaggle] I made a collection of questions using the Titanic tutorial
I made a Line bot that guesses the gender and age of a person from an image
I made a calendar that automatically updates the distribution schedule of Vtuber
I made a kind of simple image processing tool in Go language.
I made a GAN with Keras, so I made a video of the learning process.
I made a program to check the size of a file in Python
I made a mistake in fetching the hierarchy with MultiIndex of pandas
I made a function to see the movement of a two-dimensional array (Python)
I tried to correct the keystone of the image
I tried using the image filter of OpenCV
[Python + OpenCV] Whiten the transparent part of the image
I made a QR code image with CuteR
I made a tool to estimate the execution time of cron (+ PyPI debut)
I made a LINE BOT that returns a terrorist image using the Flickr API
I made an appdo command to execute a command in the context of the app
I made a tool to automatically back up the metadata of the Salesforce organization
I made a script to record the active window using win32gui of Python
I made a github action that notifies Slack of the visual regression test
〇✕ I made a game
Cut a part of the string using a Python slice
I made a threshold change box of Pepper's Dialog
I tried to erase the negative part of Meros
I made a command to markdown the table clipboard
I made a twitter app that decodes the characters of Pricone with heroku (failure)
[Python / C] I made a device that wirelessly scrolls the screen of a PC remotely.
I made a calendar that automatically updates the distribution schedule of Vtuber (Google Calendar edition)
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I made a twitter app that identifies and saves the image of a specific character on the twitter timeline by pytorch transfer learning
I scraped the Organization member team and made a ranking
I made a tool to get the answer links of OpenAI Gym all at once
Image crawling summary performed at the speed of a second
I made a VGG16 model using TensorFlow (on the way)
I tried a little bit of the behavior of the zip function
I made a python text
Does TensorFlow change the image of deep learning? What I thought after touching a little
I made a discord bot
The image is a slug
I wrote a script that splits the image in two
The goodness of the touch screen disappeared when the tablet PC was made into a Manjaro i3 environment
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
A method of converting the style of an image while preserving the color
I made a program to solve (hint) Saizeriya's spot the difference
Find the optimal value of a function with a genetic algorithm (Part 2)
[Statistics] Grasp the image of the central limit theorem with a graph
I made a program that solves the spot the difference in seconds
I tried to build the SD boot image of LicheePi Nano
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I made a lot of files for RDP connection with Python
[Go] Create a CLI command to change the extension of the image
I made AI think about the lyrics of Kenshi Yonezu (pre-processing)
I studied with Kaggle Start Book on the subject of kaggle [Part 1]
I wrote the basic operation of Pandas with Jupyter Lab (Part 1)
I made AI think about the lyrics of Kenshi Yonezu (implementation)
I tried to cut out a still image from the video
I made a scaffolding tool for the Python web framework Bottle