Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)

I tried mosaic art as a practice of OpenCV with Python.

like this

Original photo (made of 600 x 800 paint)

angry.png

Output (string data)

output.txt


111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111000000000011111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111000011111111111111111111100011111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111100011111111111111111111111111111111100111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111100111111111111111111111111111111100011111100011111111110111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111110110000000001111111111111111111001111111111111111001111111100111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111000111111111111100111111111111100111111111111111110001011111111001111111111111111111111111111111111111111111
111111111111111111111111111111111111111100111111111111111111100111111111011111111111111100011111100111111110111111111111111111111111111111111111111111
111111111111111111111111111111111111111011111111111111111111111011111101111111111111100111111111111011111111101111111111111111111111111111111111111111
111111111111111111111111111111111111100111111111111111111111111101111011111111111001111111111111111101111111110111111111111111111111111111111111111111
111111111111111111111111111111111111001000001111111111111111111110111011111110011111111111111111111110111111111101111111111111111111111111111111111111
111111111111111111111111111111111110011111111111000000111111111111010111100011110000111111111111111110111111111110111111111111111111111111111111111111
111111111111111111111111111111111101011111111111111111111100000011100000111110000000001111111111111110111111111111011111111111111111111111111111111111
111111111111111111111111111111110010111111111110000001111111111111100111111100000000000111111111111110111111111111101111111111111111111111111111111111
111111111111111111111111111111110110111111111100000000011111111111100111111100000000000111111111111110111111111111110111111111111111111111111111111111
111111111111111111111111111111101111011111111000000000011111111111100111111110000000001111111111111110111111111111111011111111111111111111111111111111
111111111111111111111111111111011111011111111100000000111111111111011011111111000000011111111111111101111111111111111011111111111111111111111111111111
111111111111111111111111111111011111101111111110000001111111111110111101111111111111111111111111111101111111111111111101111111111111111111111111111111
111111111111111111111111111110111111110111111111111111111111111101111110111111111111111111111111111011111111111111111101111111111111111111111111111111
111111111111111111111111111110111111111011111111111111111111111011100111011111111111111111111111101111111111111111111110111111111111111111111111111111
111111111111111111111111111101111111111110111111111111111111100000000000010111111111111111111110111111111111111111111110111111111111111111111111111111
111111111111111111111111111101111111111111100111111111111100100000000000000100111111111111110011111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111110000000001111000000000000000011110000000000111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111000000000000000011111111111111111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111000000000000000011111111111111111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111000000000000000011111111111111111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111100000000000000111111111111111111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111111000000000011111111111111111111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111111111000011111111111111111111111111111111111111111111111111011111111111111111111111111111
111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111
111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111
111111111111111111111111111110111111111111111111111111111111111000000000000000000001111111111111111111111111111111111101111111111111111111111111111111
111111111111111111111111111111011111111111111111111100000000000000000000000000000000000000000001111111111111111111111101111111111111111111111111111111
111111111111111111111111111111011111111111111100000000000000000000000000000000000000000000000000000011111111111111111011111111111111111111111111111111
111111111111111111111111111111101111111111100000000000000000000000000000000000000000000000000000000000001111111111111011111111111111111111111111111111
111111111111111111111111111111110111111110000000000000000000000000000000000000000000000000000000000000000011111111110111111111111111111111111111111111
111111111111111111111111111111110111111100000000000000000000000000000000000000000000000000000000000000000001111111101111111111111111111111111111111111
111111111111111111111111111111111001111110000000000000000000000000000000000000000000000000000000000000000011111111011111111111111111111111111111111111
111111111111111111111111111111111100111111000000000000000000000000000000000000000000000000000000000000000111111110111111111111111111111111111111111111
111111111111111111111111111111111111011111111000000000000000000000000000000000000000000000000000000000111111111101111111111111111111111111111111111111
111111111111111111111111111111111111101111111111100000000000000000000000000000000000000000000000001111111111110011111111111111111111111111111111111111
111111111111111111111111111111111111110011111111111111111000000000000000000000000000000000111111111111111111101111111111111111111111111111111111111111
111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111
111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111111
111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111100011111111111111111111111111111111100111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111000111111111111111111111100001111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111110000000000000111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

Code content

image.py


import cv2 #<=Pixel manipulation
import numpy as np #<=Array manipulation

def zero_one(image):
    
    output_width = 150 #It will be the number of characters next to the output
    font_aspect = 1.8 #1:1.8 may be the best(When you make a character, the character is vertically long, so adjust the height.)
    ikichi = 180 #Please adjust
    
    im_gray = cv2.imread(image, 0)#Graying
    
    height = im_gray.shape[0] #Get height
    width = im_gray.shape[1] #Get width
    
    if width > output_width: #When the photo is larger than the output width
        im_resized = cv2.resize(im_gray,(output_width,int((output_width/width)*height/font_aspect)))
    else:
        im_resized = cv2.resize(im_gray,(width,int(height/font_aspect)))
    
    ret,th = cv2.threshold(im_resized, ikichi, 255, cv2.THRESH_BINARY) #Binarization

#It seems that there are various settings, but please do so elsewhere.
    
    th[th != 0] = 1 #In this case, the white part is converted to 1.
    
    f = open('write1.txt', 'w') #Start reading
    
    for array in th: #About each line
        
        row = map(str,array)
        
        line = "".join(row)

        line.replace("[","")
        line.replace("]","")
        line.replace(" ","") #I'm sorry for forcibly m(_ _)m
        f.write(line)
        f.write("\n")


    f.close() #close

    print("Complete")

print("start")

zero_one("angry.png ")

at the end

It's not particularly useful, but thank you. I want to do various things with Python from now on.

Recommended Posts

Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
I tried "binarizing" the image with Python + OpenCV
[OpenCV / Python] I tried image analysis of cells with OpenCV
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I generated a lot of images like Google Calendar favicon with Python and incorporated it into Vue's project
I tried a stochastic simulation of a bingo game with Python
I tried to compare the processing speed with dplyr of R and pandas of Python
I made a server with Python socket and ssl and tried to access it from a browser
I tried to create a list of prime numbers with python
I tried to make a periodical process with Selenium and Python
Basics of binarized image processing with Python
I tried a functional language with Python
A python program that resizes a video and turns it into an image
I have 0 years of programming experience and challenge data processing with python
I tried hundreds of millions of SQLite with python
[Python] I introduced Word2Vec and played with it.
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried "differentiating" the image with Python + OpenCV
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried Jacobian and partial differential with python
I tried function synthesis and curry with python
I tried simple image processing with Google Colaboratory.
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
When I tried to create a virtual environment with Python, it didn't work
[ES Lab] I tried to develop a WEB application with Python and Flask ②
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
"Brute force of MD5 hash value of 6-digit password" I tried it with Python
I tried to extract a line art from an image with Deep Learning
Image processing with Python
I tried to make a simple image recognition API with Fast API and Tensorflow
Notes on HDR and RAW image processing with Python
I tried to draw a route map with Python
I made a LINE BOT with Python and Heroku
I tried to automatically generate a password with Python3
I tried using Python (3) instead of a scientific calculator
Python: I tried a liar and an honest tribe
I tried "morphology conversion" of images with Python + OpenCV
I tried to automate the article update of Livedoor blog with Python and selenium.
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
[Python] I tried to automatically create a daily report of YWT with Outlook mail
A beginner tried coloring line art with chainer. I was able to do it.
Image processing with Python (Part 2)
I tried fp-growth with python
I tried scraping with Python
Image processing with Python (Part 1)
Image processing with Python (Part 3)
I tried gRPC with Python
I tried scraping with python
[Python] Image processing with scikit-image
Associate Python Enum with a function and make it Callable
I tried running Movidius NCS with python of Raspberry Pi3
I tried follow management with Twitter API and Python (easy)
Detect objects of a specific color and size with Python
[Let's play with Python] Image processing to monochrome and dots
I created a class in Python and tried duck typing
Create a pixel art of Levi Captain with Python programming!
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python