python PIL圖片拼接

        w_num = 5
        h_num = int(len(imgs)/w_num) + 1
        UNIT_SIZE = 200 # 一張圖的大小是200*200
        target_shape = (w_num * (UNIT_SIZE + 10), h_num * (UNIT_SIZE + 10)) # shape[0]表示橫座標,shape[1]表示縱座標
        target = Image.new('RGB', target_shape)
        width = 0
        print(target_shape)
        for img in imgs:
            x, y = int(width%target_shape[0]), int(width/target_shape[0])*(UNIT_SIZE+10) # 左上角座標,從左到右遞增
            target.paste(Image.open(osp.join(task, img)).resize((UNIT_SIZE, UNIT_SIZE)), (x, y, x+UNIT_SIZE, y+UNIT_SIZE))
            width += (UNIT_SIZE+10)
        target.save(osp.join('of_vis', task.split('/')[-1]+'.jpg'))

以上是從一個小項目摘下來的python PIL圖片拼接代碼,實現的功能是將若干張圖片拼接到一張大圖,該圖固定5列,圖片之間加了10個像素點的間隔。

PIL Image的shape中第一個座標表示寬度,第二個座標表示高度。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章