PythonでOpenCVの練習としてモザイクアートに挑戦してみました。
こんなかんじ
元写真(600×800 ペイント製)
出力(文字列データ)
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
コードの内容
image.py
import cv2 #<=画素操作
import numpy as np #<=配列操作
def zero_one(image):
output_width = 150 #出力時の横の文字数になります
font_aspect = 1.8 #1:1.8が最適かも(文字にしたとき文字は縦長なので高さを調節します。)
ikichi = 180 #調節してください
im_gray = cv2.imread(image, 0)#グレー化
height = im_gray.shape[0] #高さの取得
width = im_gray.shape[1] #幅の取得
if width > 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) #二値化
# いろいろ設定はあるようですが、それはまた別のところにお願いします。
th[th != 0] = 1 #この場合白色部分は1に変換されます。
f = open('write1.txt', 'w') #読み込み開始
for array in th: #各行について
row = map(str,array)
line = "".join(row)
line.replace("[","")
line.replace("]","")
line.replace(" ","") #強引ですみませんm(_ _)m
f.write(line)
f.write("\n")
f.close() #閉じる
print("Complete")
print("start")
zero_one("angry.png")
さいごに
特に役立つものではないですが、ありがとうございました。
これからPythonでいろいろやってみたいです。