Split Python images and arrange them side by side

background

I am creating a Sudoku app. Somehow in the process in the middle, I wanted to divide the image and arrange it side by side, so I created it while googled. I hope it will be helpful to someone.

Sample code

split_img_and_layout_Horizontally.py


import numpy as np
import cv2

#Read the input image
img = cv2.imread("sudoku_wiki.png ")
#wiki https://en.wikipedia.org/wiki/Sudoku

#Define split size
vertical_split = 9
horizonal_split = 9

#Specify resizing
size = (vertical_split * 200, horizonal_split * 200)
img = cv2.resize(img, size)

#List initialization
split_img = []

#Divided vertically and horizontally
[split_img.extend(np.hsplit(img, horizonal_split)) for img in np.vsplit(img, vertical_split)]

#Arrange the divided images horizontally
img_list = [split_img[i] for i in range(len(split_img))]
merge_img = cv2.hconcat(img_list)

#Save image
cv2.imwrite('merge_img.jpg', merge_img)

Execution result

Target image: sudoku_wiki.png

sudoku_wiki.png

Execution result: merge_img.jpg It's hard to see, so please enlarge it. .. .. merge_img.jpg

Further improvements

・ It will be easier to see if you erase the border with the numbers. → Detects straight lines and paints white

・ Character string from the image of numbers → Incorporate MNIST (machine learning)

I worked on each of them, but the accuracy was not so good, so I hope it will be posted as soon as it can be improved.

Recommended Posts

Split Python images and arrange them side by side
Python and Ruby split
Difference between Ruby and Python split
[Python] Implementation of Nelder–Mead method and saving of GIF images by matplotlib
[Python] Automatically and seamlessly combine cropped images
Capturing images with Pupil, python and OpenCV
Function to save images by date [python3]
Socket communication by C language and Python
[python] How to display list elements side by side
ffmpeg-Build a python environment and split the video
Notify error and execution completion by LINE [Python]
Split camel case string word by word in Python
Python> Sort by number and sort by alphabet> Use sorted ()
[Keras] Personal memo to classify images by folder [Python]
Get media timeline images and videos with Python + Tweepy
Python learning memo for machine learning by Chainer Chapters 1 and 2
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Shuffle the images in any directory with Python and save them in another folder with serial numbers.