如何將文件夾中的所有圖像垂直合併/附加/堆疊/粘貼到新圖像 (How to merge/append/stack/paste all images in folder vertically to a new image)


問題描述

如何將文件夾中的所有圖像垂直合併/附加/堆疊/粘貼到新圖像 (How to merge/append/stack/paste all images in folder vertically to a new image)

我想合併一個文件夾中的所有圖片並將它們堆疊,水平附加。

import os
from PIL import Image
allfiles = os.listdir(os.getcwd())
imlist =[filename for filename in allfiles if filename[‑4:] in [".png", ".PNG"]]
N = len(imlist)
w, h = Image.open(imlist[0]).size

total_width = w * N
max_height = h

new_im = Image.new('RGB', (total_width, max_height))

for i in range(1,N):
    img = Image.open(imlist[i])
    offset = 0
    appendedimages.paste(img, (x_offset,0))
    offset += img.size[0]

appendedimages.save('test.jpg')

看起來,使用這個,它只顯示最後一張圖片。有人知道為什麼會這樣嗎?

我也試過

import cv2
import os
import numpy as np


allfiles = os.listdir(os.getcwd())
imlist =[filename for filename in allfiles if filename[‑4:] in [".png", ".PNG"]]
N = len(imlist)
for i in range(1,N):
    img = cv2.imread(imlist[i])

horizontalAppendedImg = np.hstack(img)
cv2.imshow('Horizontal Appended', horizontalAppendedImg)
cv2.waitKey(0)
cv2.destroyAllWindows()

但這也不起作用。

誰能幫我解決這個問題?還是有更簡單的解決方案?


參考解法

方法 1:

images are numpy arrays. As long as they have the same dimensions, you can np.hstack them.

imlist =[cv2.imread(filename) for filename in allfiles if filename[‑4:] in [".png", ".PNG"]]
concat_img = np.hstack(imlist)

cv2.imshow('Horizontal Appended', concat_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>

Explanation:

  • read in all images into a list with list comprehension: list of images/np.arrays
  • use np.hstack() to concatenate all images along the horizontal dimension. You could use more general concatenation tools with np.concatenate() or np.stack() if you run into issues.

(by JolanthanDorian)

參考文件

  1. How to merge/append/stack/paste all images in folder vertically to a new image (CC BY‑SA 2.5/3.0/4.0)

#Python #Numpy #python-imaging-library #OpenCV






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論