Skip to content
Advertisement

Saving an image as jpg gives me plain black

I am implementing gabor kernels, when I display the kernels while running the code (before saving them) they give me a picture like this

enter image description here

But after saving the kernels as jpg images using cv2.imwrite, I get like that

enter image description here

Any explanations? and how to save the kernels as in the first image?

Advertisement

Answer

There could be different causes. So I have two suggestions:

  1. If you display the first picture with plt.imshow(), export it with plt.savefig(). This should easily be working.

  2. If you still want to export the image with cv2.imwrite() make sure that the picture is correctly rescaled first. (mind that if you have only one channel, you will get a grayscale picture).

If we call the original picture org_img:

img = org_img
min_val,max_val=img.min(),img.max()
img = 255.0*(img - min_val)/(max_val - min_val)
img = img.astype(np.uint8)
cv2.imwrite(img,"picture.png")
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement