'How to combine multiple images into one image using python
I am trying to create an image collage from multiple images, have been able to build up successfully if all the images are in single folder, but the problem is that when the images are in multiple folders , the collage is not making perfectly its only storing the last image in all the position of the collage.
within this make collage function i am just going to create 2x2 collage right now.
def makecollage(image):
collage = Image.new("RGBA", (5000,2500), color=(255,255,255,255))
max = 8
l = list(range(0, max))
random.shuffle(l)
c=0
for i in range(0,1000,500):
for j in range(0,1000,500):
photo = image.resize((500,500))
collage.paste(photo, (i,j))
print(i,j)
c+=1
# Make a directory to save the collage image
collage.save("collage"+str(i+j)+".png")
through the randomfile() I am been able to pick any random file inside in the sub directories, each sub directory contains only one file right now.
def randomFile(directory):
files_list = os.listdir(directory)
random_num = random.choice(files_list)
img_1=Image.open((os.path.join(directory,random_num))).convert("RGBA")
if img_1 is not None:
images.append(img_1)
files_list.remove(random_num)
makecollage(img_1)
for x in directories[1:]:
randomFile(x)
So the collage works perfectly when all the images are in single folder
but when images are in multiple sub folders it repeats the last image in all position of collage

Solution 1:[1]
#you should place all the images in a row + column #and call the function like:
import cv2
import NumPy as np
def stackImages(scale, imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range(0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
imgArray[x][y]=cv2.resize(imgArray[x][y],(0,0),None,scale,scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0]
[0].shape[1], imgArray[0][0].shape[0]),
None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y] =
cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank] * rows
hor_con = [imageBlank] * rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1],
imgArray[0].shape[0]), None, scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] =
cv2.cvtColor(imgArray[x],cv2.COLOR_GRAY2BGR)
hor = np.hstack(imgArray)
ver = hor
return ver
###... call the image by cv2
img1 = cv2.imread("Name_of_Image.Extention")
imgarray = ([img1, img2], [img3, img4])
stackImage = stackImages(0.3, imgarray)
cv2.imshow("img1", stackImage)
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Syed Aoon Hussain |

