简体   繁体   中英

How to Display Images in a particular order using OpenCV in Python?

I am writing a program that takes a string from user and displays sign language images of each character in that string. This code displays images which overlap on each other. Is there a way I can display these images in the order that they were taken an input? This is the output I am currently getting. This is how I want the output to look

import cv2

print("Say something !!!")
say = input()
i=1

for x in say :
    if x == " ":
        continue
    img = cv2.imread("TrainData\\" + x + "_1.jpg")
    cv2.imshow(x + str(i), img)
    i= i+1
cv2.waitKey(0)

I want to display images from left to right according to the input.

Rather than trying to position windows, you could concatenate the images side-by-side and display them in a single window:

#!/usr/local/bin/python3
import numpy as np
import cv2

# Load the 4 letters we need
h = cv2.imread('h.png',0)
e = cv2.imread('e.png',0)
l = cv2.imread('l.png',0)
o = cv2.imread('o.png',0)

# Append images side-by-side
result = np.concatenate((h,e,l,l,o),axis=1)

# Save to disk, or display as a single, wide image
cv2.imwrite('result.png',result)

在此输入图像描述


Of course, you can make a spacer shim if you want:

#!/usr/local/bin/python3
import numpy as np
import cv2

# Load the 4 letters we need
h = cv2.imread('h.png',0)
e = cv2.imread('e.png',0)
l = cv2.imread('l.png',0)
o = cv2.imread('o.png',0)

shim = np.ones((200,10),dtype=np.uint8)*255

# Append images side-by-side
result = np.concatenate((h,shim,e,shim,l,shim,l,shim,o),axis=1)
cv2.imwrite('result.png',result)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM