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.
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)
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)
Then, convert it to a pixel art. 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]])
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