[Let's play with Python] Image processing to monochrome and dots

Introduction

I wanted to change the image of my SNS account soon, so I did it in Python. Leave the code. The contents are the following three

--Load image --Output the image in monochrome --Output the image as dots

Will be. There is not much attention, but dot conversion will take some time. (About 1 minute? Depending on the specifications of the PC)

Preparation

Please note the following points when executing by yourself.

--Upload the original image --Renamed image file in code (hereinafter referred to as shiro.jpg)

Load image

Prepare and load the image. I'm doing it on the Jupiter Notebook. In that case, prepare an image and upload it to Notebook in advance.

import numpy as np
import matplotlib.pyplot as plt
import cv2

#Loading images
img = plt.imread('shiro.jpg') 
type(img) 
img.size
plt.imshow(img) #Image display
plt.show()

This is the original image. 2020-02-24 (2).png

Monochrome processing and output

Next, monochrome (black and white) processing is performed and output.

def img_show(img : np.ndarray, cmap = 'gray', vmin = 0, vmax = 255, interpolation = 'none') -> None: 
    plt.imshow(img, cmap = cmap, vmin = vmin, vmax = vmax, interpolation = interpolation) #Display image
    plt.show()
    plt.close()
    img = plt.imread('shiro.jpg')

img_mid_v = np.max(img, axis = 2)/2 +np.min(img, axis = 2)/2
img_show(img_mid_v)
img = plt.imread('shiro.jpg')

2020-02-24.png

Dot

Make dots. It may be interesting to play with the numbers "dst = pixel_art (img, 0.1, 4)" as you like. When executed, a file such as'shiro_mozaiku.jpg' will be output to Jupiter Notebook.

#Color reduction processing
def sub_color(src, K):   
    Z = src.reshape((-1,3))
    Z = np.float32(Z)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    center = np.uint8(center)
    res = center[label.flatten()]
    return res.reshape((src.shape))

#Mosaic processing
def mosaic(img, alpha):
    h, w, ch = img.shape
    img = cv2.resize(img,(int(w*alpha), int(h*alpha)))
    img = cv2.resize(img,(w, h), interpolation=cv2.INTER_NEAREST)
    return img

#Pixel art
def pixel_art(img, alpha=2, K=4):
    img = mosaic(img, alpha)
    return sub_color(img, K)
#Get input image
img = cv2.imread("shiro.jpg ") 

#Pixel art Image roughness,Color roughness
dst = pixel_art(img, 0.1, 4)
    
#Output result
cv2.imwrite("shiro_mozaiku.jpg ", dst)

from PIL import Image
mozaiku_shiro = Image.open('shiro_mozaiku.jpg')
mozaiku_shiro

2020-02-24 (1).png

Chat

It feels like I've made something pretty good. This is a picture of when I went on a trip, but it seems that buildings such as castles can be converted into mosaic dots. Other than that, I don't know what will happen to people and landscapes, but it may be interesting to try it.

Recommended Posts

[Let's play with Python] Image processing to monochrome and dots
Fractal to make and play with Python
Image processing with Python
Image processing with Python (Part 2)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
[Python] Image processing with scikit-image
Notes on HDR and RAW image processing with Python
Image processing with Python 100 knocks # 3 Binarization
Let's play with Excel with Python [Beginner]
Let's do image scraping with Python
Image processing with Python 100 knocks # 2 Grayscale
Basics of binarized image processing with Python
[Introduction to WordCloud] Let's play with scraping ♬
HTML email with image to send with python
Image processing with Python 100 knocks # 8 Max pooling
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
[Introduction to Python] Let's use foreach with Python
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
I want to play with aws with python
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Sample to convert image to Wavelet with Python
Play with 2016-Python
Let's make an image recognition model with your own data and play!
Python hand play (let's get started with AtCoder?)
Scraping tabelog with python and outputting to CSV
[REAPER] How to play with Reascript in Python
Convert PDF to image (JPEG / PNG) with Python
Image characters and post to slack (python slackbot)
Image processing from scratch with python (5) Fourier transform
How to crop an image with Python + OpenCV
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
Let's control EV3 motors and sensors with Python
Post an article with an image to WordPress with Python
Let's play with Python Receive and save / display the text of the input form
Image processing with MyHDL
First Python image processing
Image Processing with PIL
Procedure to load MNIST with python and output to png
How to scrape image data from flickr with python
I want to handle optimization with python and cplex
[Let's play with Python] Make a household account book
Try to operate DB with Python and visualize with d3
[Chapter 5] Introduction to Python with 100 knocks of language processing
An introduction to Python distributed parallel processing with Ray
Let's make a simple game with Python 3 and iPhone
Convert the image in .zip to PDF with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
Let's play with JNetHack 3.6.2 which is easier to compile!
How to loop and play gif video with openCV
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Something to enjoy with Prim Pro (X-Play) and Python
[How to!] Learn and play Super Mario with Tensorflow !!
[Python] Road to a snake charmer (5) Play with Matplotlib