天天看點

python圖檔切割與合并

python圖檔切割與合并

    • 1.tif圖檔切割為jpg
    • 2.圖檔合并

1.tif圖檔切割為jpg

  • 将圖檔切割為1024大小的小圖檔
  • 大小不夠1024的,填充為黑色
    import os
    import numpy as np
    import cv2 as cv
    import PIL.Image as Image
    
    
    def cutting(img_path, data_path,image_name, clip_path):
    
        """
    
        :param img_path: 圖檔檔案夾
        :param data_path: 資料檔案夾
        :param image_name: 切割原始圖檔名
        :param clip_path: 切割圖檔儲存路徑
        :return:
        """
        img_size = 1024
        n, m, max_n = 0, 0, 0
        img = cv.imread(os.path.join(img_path, image_name), 1)
        clip_img_path = os.path.join(data_path, 'clip', clip_path)
        if not os.path.exists(clip_img_path):
            os.makedirs(clip_img_path)
        h, w = img.shape[0], img.shape[1]
        # 圖檔切割
        for i in range(0, h, img_size):
            img_h = (h % img_size) if (i + img_size) > h else 1024
            for j in range(0, w, img_size):
                max_n = n if max_n <= n else max_n
                end_i, end_j = min(h, i + img_size), min(w, j + img_size)
                cropped = img[i:end_i, j:end_j]
                img_w = (w % img_size) if (j + img_size) > w else 1024
                img_orig = Image.fromarray(cv.cvtColor(cropped, cv.COLOR_BGR2RGB)) # bgr 轉 rgb
                to_image = Image.new('RGB', (img_size, img_size))  # 建立1024大小圖檔
                to_image.paste(img_orig, (0, 0))
                if img_w != 1024:  # 寬度填充
                    img_w_new = np.zeros(shape=((img_size - img_w), img_h, 3), dtype=np.uint8)
                    img_w_new = Image.fromarray(img_w_new)
                    to_image.paste(img_w_new, (img_w, 0))
                    if img_h != 1024:  # 寬度填充 and 高度填充
                        img_h_new = np.zeros(shape=(img_size, (img_size - img_h), 3), dtype=np.uint8)
                        img_h_new = Image.fromarray(img_h_new)
                        to_image.paste(img_h_new, (0, img_h))
                    to_image.save(clip_img_path + '/' + str(m) + '_' + str(n) + ".jpg")
                elif img_w == 1024 and img_h != 1024:  # 隻填充高度
                    img_h_new = np.zeros(shape=(img_size, (img_size - img_h), 3), dtype=np.uint8)
                    img_h_new = Image.fromarray(img_h_new)
                    to_image.paste(img_h_new, (0, img_h))
                    to_image.save(clip_img_path + '/' + str(m) + '_' + str(n) + ".jpg")
                else:  # 不填充
                    cv.imwrite(clip_img_path + '/' + str(m) + '_' + str(n) + ".jpg", cropped)
                n += 1
            n = 0
            m += 1
    
    
    if __name__ == '__main__':
        root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        data_path = os.path.join(root, 'data')
        img_path = os.path.join(data_path, 'bhfx1')
        cutting(img_path, data_path, "fw1_2021.tif", "clip_2021")
    
               

2.圖檔合并

  • 合并的圖檔大小一緻
    import os
    import PIL.Image as Image
    
    def image_compose(IMAGE_SIZE=1024, IMAGE_ROW=24, IMAGE_COLUMN=19):
        to_image = Image.new('RGB', (IMAGE_SIZE*(IMAGE_COLUMN+1), IMAGE_SIZE*(IMAGE_ROW+1)))  # 建立新圖檔大小
        for y in range(0, IMAGE_ROW + 1):  # 循環周遊,把每張圖檔按順序粘貼到對應位置上
            for x in range(0, IMAGE_COLUMN + 1):
                file_name = str(y) + '_' + str(x) + ".jpg"
                img_name = os.path.join(clip_img_path, file_name) # 圖檔名
                from_image = Image.open(img_name) # 打開合并的圖檔
                to_image.paste(from_image, ((x) * IMAGE_SIZE, (y) * IMAGE_SIZE))  # 粘貼image到im的position(左上角)位置。w,h
        to_image.save(IMAGE_SAVE_PATH) # 儲存圖檔
        
    if __name__ == '__main__':
        root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        data_path = os.path.join(root, 'data')
        clip_img_path = os.path.join(data_path, 'clip', 'clip_2020')
        IMAGE_SAVE_PATH = os.path.join(data_path, 'img_merge', 'clip_2020.jpg')
        image_compose()