I made a dot picture of the image of Irasutoya. (part2)

Background It's not about direct improvement of pixel art, but I made the image of Irasutoya into pixel art. (part1), the surrounding image could not be converted to dots, so I will explain the solution.

neko16.png

Method

As a method, prepare an image that is twice the width and height of the original image. The image is an image of putting a sandwich on a wrap: hamburger:


def wrapMat(height, width):
	dst = np.zeros((height, width, 4), np.uint8)
	dst[:] = tuple((0,255,0,128))
	return dst

if __name__ == "__main__":
	#The original image
	mat = cv2.imread(path,cv2.IMREAD_UNCHANGED)

	#Fold the width and height in half
	top = int(mat.shape[0] * 0.5)
	left = int(mat.shape[1] * 0.5)
	wrap = wrapMat(top*4,left*4)

	cv2.imwrite("mono_wrap.png ", wrap)

mono_wrap.png

It is easy to understand by appearance, and G = 255 and α (transparency) = 128 are set.

Next, place the image you want to make into a pixel art in the center.

	wrap[top:top + mat.shape[0], left:left + mat.shape[1]] = mat
	cv2.imwrite("wrap.png ", wrap)

wrap.png

Then, convert it to a pixel art. output.png A gray line is generated around it, but I think it is because the average value of the color scheme and transparent color of the wrap image was calculated on the boundary line.

Then crop this image to the size of the original image.

cv2.imwrite("trim.png ",wrap[top:top + mat.shape[0], left:left + mat.shape[1]])

trim.png

I was wondering if a gray line would remain on the edge, but I was able to output as expected. If it is displayed, there is no problem if you set the color scheme of the wrap image to (R, G, B, A) = (0,0,0,0).

Development

The whole code.

import cv2
import numpy as np
import sys


def wrapMat(height, width):
    dst = np.zeros((height, width, 4), np.uint8)
    dst[:] = tuple((0, 255, 0, 128))
    return dst


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]


def main():
    CELL_SIZE = 16

    path = sys.argv[1]
    mat = cv2.imread(path, cv2.IMREAD_UNCHANGED)

    top = int(mat.shape[0] * 0.5)
    left = int(mat.shape[1] * 0.5)
    #Wrap image creation
    wrap = wrapMat(top * 4, left * 4)
    #Place the original image
    wrap[top:top + mat.shape[0], left:left + mat.shape[1]] = mat

    #Pixel art creation
    height, width = wrap.shape[:-1]
    for w in range(width // CELL_SIZE - 1):
        for h in range(height // CELL_SIZE - 1):
            c = np.mean(
                np.mean(
                    wrap[h * CELL_SIZE:(h + 1) * CELL_SIZE,
                         w * CELL_SIZE:(w + 1) * CELL_SIZE], axis=0
                ),
                axis=0
            )
            wrap[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]])

    #trimming
    trim = wrap[top:top + mat.shape[0], left:left + mat.shape[1]]
    cv2.imwrite("trim.png ", trim)


if __name__ == "__main__":
    main()

Future Next time, let's think about how to make a monochrome image into four color schemes.

Reference -How to generate a monochromatic image with Python / OpenCV (using numpy)

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
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 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 "gamma correction" of the image with Python + OpenCV
I scraped the Organization member team and made a ranking
I just changed the sample source of Python a little.
I made a tool to get the answer links of OpenAI Gym all at once
Dot according to the image
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
I analyzed the image of the Kyudo scoring book (a booklet that records the results of the hits). (Google Colaboratory)
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)
[Python] I made an image viewer with a simple sorting function.
I studied with Kaggle Start Book on the subject of kaggle [Part 1]
Count the maximum concatenated part of a random graph with NetworkX
I wrote the basic operation of Pandas with Jupyter Lab (Part 1)
I tried to cut out a still image from the video
I made a scaffolding tool for the Python web framework Bottle
I tried running the DNN part of OpenPose with Chainer CPU