SmallChess
SmallChess

Reputation: 8106

Unable to composite two images in Python

import cv2
import keras
from matplotlib import pyplot as plt

def writeImage(file, x):
    cv2.imwrite(file, x)

def showImage(data):
    plt.imshow(data)
    plt.show()

def composite(X, Y, out):
    writeImage(X, "/tmp/A.png")
    writeImage(Y, "/tmp/B.png")
    A = cv2.imread("/tmp/A.png", 0)
    B = cv2.imread("/tmp/B.png", 0)
    C = np.dstack((A, B))
    writeImage(C, out)
    return C

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
showImage(x_train[0])
showImage(x_train[5])
composite(x_train[0], x_train[5], "/tmp/ImageCompositeExample.jpg")

I'm trying to merge two images into one like How can I make the composite of two images in OpenCV with Python?. But I'm getting:

SystemError: <built-in function imwrite> returned NULL without setting an error 

---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)
<ipython-input-15-f533fdd71378> in <module>
----> 1 composite(x_train[0], x_train[5], "/tmp/ImageCompositeExample.jpg")

<ipython-input-11-ff434edc5914> in composite(X, Y, out)
      1 def composite(X, Y, out):
----> 2     writeImage(X, "/tmp/A.png")
      3     writeImage(Y, "/tmp/B.png")
      4     A = cv2.imread("/tmp/A.png", 0)
      5     B = cv2.imread("/tmp/B.png", 0)

<ipython-input-9-5ef37533556e> in writeImage(file, x)
      1 def writeImage(file, x):
----> 2     cv2.imwrite(file, x)

SystemError: <built-in function imwrite> returned NULL without setting an error

Upvotes: 0

Views: 143

Answers (2)

Mario Abbruscato
Mario Abbruscato

Reputation: 819

There are errors passing arguments to function writeImage

def composite(X, Y, out):

    #writeImage(X, "/tmp/A.png") <-- error
    #writeImage(Y, "/tmp/B.png") <-- error

    writeImage("/tmp/A.png",X)
    writeImage("/tmp/B.png",Y)
    
    
    A = cv2.imread("/tmp/A.png", 0)
    B = cv2.imread("/tmp/B.png", 0)
    C = np.dstack((A, B))

    # def writeImage(file, x):  how is defined 
    # writeImage(C, out)  <--- error

    writeImage(out, C)
    return C
    

Upvotes: 1

SuperStormer
SuperStormer

Reputation: 5387

Swap the arguments in the function call, you're passing (x, file) right now.

writeImage("/tmp/A.png", X)
writeImage("/tmp/B.png", Y)

Upvotes: 1

Related Questions