Python application-image segmentation

Description

Previously downloaded the comics in the zip package, the pictures inside are all two together:

However, some comic viewing software does not support automatic screen splitting, which may seem uncomfortable, so you can only split it by yourself.

 

Instructions

Python has many libraries that support image manipulation, and one of the more famous is OpenCV.

OpenCV is a cross-platform computer vision library, with its interface implementation under Python.

Python does not include OpenCV by default, so you need to download it with pip first:

OpenCV is powerful, and the image segmentation used here is actually a small test.

Regarding the functions of OpenCV, there is not much introduction here. Those who are interested can find other information.

In order to use OpenCV in your code, you first need to import related libraries:

import cv2  # Should be install independently.

Then read the picture:

img1 = cv2.imread(filename)

Then do the cutting:

    # shape[0]:height shape[1]:width shape[2]:channel
    # img[y0:y1, x0:x1] 0=(left up) 1=(right low)
    slice1 = img[0:height, width/2:width]

This is actually to specify the picture frame. The two values ​​needed are the coordinates of the upper left corner and the lower right corner, but the corresponding method is a bit weird. I don't know why it is corresponding.

Then write back the picture:

    cv2.imwrite(getname(index1), slice1, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])

In addition, in order to ensure that the picture is not too big, you can also do some compression:

    img = cv2.resize(img1, (0, 0), fx=compressratio, fy=compressratio, interpolation=cv2.INTER_NEAREST)

The above is the basic code related to the picture.

 

Code

The following is all the code, save it in a py file, and then put it in the same directory as the picture, double-click the py file to execute it, and split the picture:

#!/usr/bin/env python
# ---------------------------------------------------------------------------------
# coding=utf-8
# @File    : sliceImage.py
# @Author  : Jiangwei
# @Date    : 2020/4/18
# @Desc    : Slice images.
# @History :
#     Date       Author      Description
#   20200418    Jiangwei     Created.
# @Warning:
#   Tested in Python 2.7.
# ---------------------------------------------------------------------------------


import os
import sys
import cv2  # Should be install independently.


todir = "tmp"
exts  = ['.jpg', '.JPG', '.png', '.PNG']
compressratio = 0.75


def listimage(adir):
    '''
    adir    : The directory name.
    '''
    list = []
    for i in os.listdir(adir):
        if os.path.splitext(i)[1] in exts:
            list.append(os.path.join(adir, i))

    return list


def getname(index):
    page = "Image%03d.png" % index
    return os.getcwd() + "\\" + todir + "\\" + page


def doslice(filename, index1, index2):
    img1 = cv2.imread(filename)
    img = cv2.resize(img1, (0, 0), fx=compressratio, fy=compressratio, interpolation=cv2.INTER_NEAREST)
    height,width = img.shape[0:2]
    # shape[0]:height shape[1]:width shape[2]:channel
    # img[y0:y1, x0:x1] 0=(left up) 1=(right low)
    slice1 = img[0:height, width/2:width]
    cv2.imwrite(getname(index1), slice1, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
    print getname(index1)
    slice2 = img[0:height, 0:width/2]
    cv2.imwrite(getname(index2), slice2, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
    print getname(index2)

    return


if __name__ == "__main__":
    '''
    Slice images.
    '''

    # Temperature directory for sliceped images.
    if not os.path.exists(todir):
        os.mkdir(todir)

    # Transverse all files and do the slice.
    imagelist = listimage (os.getcwd())
    index = 1
    for i in imagelist:
        print "Processing %s" % i
        doslice(i, index, index + 1)
        index += 2

The files after splitting will be placed in the newly created tmp directory.

The following is the effect after switching:

The code is not well written, but it can be used...

 

Guess you like

Origin blog.csdn.net/jiangwei0512/article/details/105605512