6

I want to concatenate vertically a lot of images. I used skimage to read images then in each iteration io read image and vconcat concatenate the new image on the old image vertically. The result of my code didn't concatenate the images, just combine the images. Any ideas how to make each image in each iteration concatenate vertically on each other?

I want to concatenate the first image and the second image vertically:

fist image

second image

but I got this result:

result

enter image description here

data = []
if nSpectogram < 3765:
    for k in range(0, 21):
        path = io.imread('E:\\wavelet\\spectrograms\\paz05\\'+'spec_'+isPreictal+'_'+str(nSpectogram+1)+'_'+str(k+1)+'.png')
        im_v_array = np.array(im_v)
        data.append(path)
    res = np.concatenate(data)
    plt.imshow(res, cmap='inferno', aspect='auto', interpolation='nearest')
4
  • Can you explain the difference between "concat" and "combine"? Examples will goa long way w.r.t what you are getting and what you want Commented Sep 11, 2020 at 13:16
  • @VivekKalyanarangan done. I want to concatenate a lot of images vertically. but i got image like the image i added in the post as a result. I just want to concatenate the images vertically not to combine them. Commented Sep 11, 2020 at 13:33
  • So your result is a single image without x and y axes? I tried many ways and cannot reproduce. Commented Sep 11, 2020 at 15:40
  • @Parfait Exactly.it was a single image without x and y axes. Commented Sep 11, 2020 at 18:14

2 Answers 2

6

Use -

merged_img = []
for i in range(3):
    img = io.imread('https://machinelearningblogs.com/wp-content/uploads/2018/03/849825_XL-830x400.jpg')
    merged_img.append(img)

merge = np.concatenate(merged_img)

plt.imshow(merge)

enter image description here

Just add all the images to a list in the for loop after reading them and pass the list to np.concatenate

Sign up to request clarification or add additional context in comments.

9 Comments

it didn't work with me. ``` data=[] if nSpectogram <3765: for k in range (0,21): path=io.imread('E:\\wavelet\\spectrograms\\paz05\\'+'spec_'+isPreictal+''+str(nSpectogram+1)+''+str(k+1)+'.png') im_v_array=np.array(im_v) plt.imshow(im_v,cmap='inferno', aspect='auto', interpolation='nearest') data.append(path) res=np.concatenate(data) ```
I didn't work with me. I have edited my code in the post with you answer. please take a look on it.
@nadamedhat you have to imshow() the res object outside the loop. Please copy paste the solution provided as-is and check; post that adjust your code accordingly
@Vivek Kalyanarangan imshow and res are outside the loop. it didn't concatenate the images.
@Parfait I mean the images didn't concatenate vertically just sowing the image in a different look
|
2

Instead of skimage.io (which may be due to a version or CPU issue), consider using matplotlib.pyplot.imread with list comprehension or map. Below demonstrates with OP's two images.

import numpy as np
import matplotlib.pyplot as plt

img_paths = ["OP_Image_1.png", "OP_Image_2.png"]

data = [plt.imread(img) for img in img_paths]
# data = list(map(mpimg.imread, img_paths))

res = np.concatenate(data)
plt.imshow(res, cmap='inferno', aspect='auto', interpolation='nearest')

plt.axis('off')
plt.show()

Plot Output


Specifically, for OP's iteration of files:

import os
import numpy as np
import matplotlib.pyplot as plt

...
spec_path = r"E:\wavelet\spectrograms\paz05"                     # RAW STRING FOR BACKSLASHES
spec_file = f"spec_{isPreictal}_{str(nSpectogram+1)}_{{}}.png"   # F STRING FOR INTERPOLATION

if nSpectogram <3765:
   data = [plt.imread(os.path.join(spec_path, spec_file.format(k+1))) for k in range(21)]

   res = np.concatenate(data)
   plt.imshow(res, cmap='inferno', aspect='auto', interpolation='nearest')
   
   plt.axis('off')
   plt.savefig(os.path.join(spec_path, "Output.png"))

3 Comments

But I have a lot of images not only 2. can i put path inside img_paths to read each image in each iteration?
See extended answer for your current setup. Also, avoid printing to screen with plt.show() but save to file: plt.savefig(...). Not sure why you want 1,000 concatenated images which may become a large file and take a while to process.
Great to hear and glad to help. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.