vieyahn2017 / pypy

python trial collections
1 stars 1 forks source link

拆分合并(CSDN code JS) #10

Open vieyahn2017 opened 1 year ago

vieyahn2017 commented 1 year ago

只有一段代码的时候 var selector = "#content_views > pre > code"; console.log(Array.prototype.slice.call(document.querySelectorAll(selector)).map(x => x.outerText).join("\n"));

vieyahn2017 commented 1 year ago

var selector = "#content_views > pre:nth-child(4) > code"; console.log(Array.prototype.slice.call(document.querySelectorAll(selector)).map(x => x.outerText).join("\n"));

vieyahn2017 commented 1 year ago

import cv2
import os
import numpy as np

BASEDIR = 'D:\\javaway\\viey2017\\Image-inpainting-by-FMM-and-criminisi-master\\py\\'
source_path = BASEDIR
target_path = BASEDIR + "\\slice"

def mkdir(path):
    path = path.strip()
    path = path.rstrip("\\")

    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print(path + ' 创建成功')
        return True
    else:
        print(path + ' 目录已存在')
        return False

# https://blog.csdn.net/haogejida/article/details/128133098
def split_img(img_file):
    img = cv2.imread(img_file)
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    img_h = img.shape[0]  # 高度
    img_w = img.shape[1]  # 宽度
    # h1_half = int(img_h / 2)
    # w1_half = int(img_w / 2)

    h1_half = img_h // 2
    w1_half = img_w // 2

    img_name = os.path.basename(img_file)
    for i in range(4):
        img1 = img[int(i / 2) * h1_half: h1_half * (int(i / 2) + 1), int(i % 2) * w1_half: (int(i % 2) + 1) * w1_half]

        img1_path = os.path.join(target_path, f"{img_name[:-4]}_0{i+1}.jpg")
        print("spilt img:", img1_path)
        cv2.imwrite(img1_path, img1)

# https://www.cnblogs.com/yang888/p/15365940.html
def merge_img():
    # 导入四张图片
    image1 = cv2.imread(target_path + '\\3_01.jpg')
    image2 = cv2.imread(target_path + '\\3_02.jpg')
    image3 = cv2.imread(target_path + '\\3_03.jpg')
    image4 = cv2.imread(target_path + '\\3_04.jpg')

    img_h = image1.shape[0]
    img_w = image1.shape[1]
    # 创建一个纯黑的大图
    # mage = np.zeros((3648, 5472, 3), np.uint8)
    mage = np.zeros((img_h * 2, img_w * 2, 3), np.uint8)

    # 将要拼接的四张图像覆盖到大图上
    # mage[0:1824, 0:2736] = image1
    # mage[0:1824, 2736:5472] = image2
    # mage[1824:3648, 0:2736] = image3
    # mage[1824:3648, 2736:5472] = image4
    mage[0:img_h, 0:img_w] = image1
    mage[0:img_h, img_w:img_w * 2] = image2
    mage[img_h:img_h * 2, 0:img_w] = image3
    mage[img_h:img_h * 2, img_w:img_w * 2] = image4

    # 保存图像
    cv2.imwrite(BASEDIR + "3-merge.jpg", mage)

if __name__ == '__main__0':
    mkdir(target_path)
    # for file in pathlib.Path(source_path).glob('**/*'):
    file = "2.jpg"
    split_img(os.path.join(source_path, file))

if __name__ == '__main__':
    merge_img()