Home > Back-end >  cv2.imwrite Image being saved as black .jpg (OpenCV)
cv2.imwrite Image being saved as black .jpg (OpenCV)

Time:04-11

When I am trying to save an image, the saved image is black. This is the code for saving the image:-

sm = pySaliencyMap.pySaliencyMap(frame_width, frame_height)
saliency_map = sm.SMGetSM(frame)

cv2.imshow('Saliency Map', saliency_map)
filename_saliency =  'saliency_map'   str(saliency_num)   '.png'
cv2.imwrite(filename_saliency, saliency_map)
saliency_num  = 1

The cv2.imshow displays the image correctly. But, cv2.imwrite operation returns a black image. Any help why is this issue occuring and how to resolve it? Appreciate it!

CodePudding user response:

Used string format python 3.8 or later. I used 3.9.2 on raspberry pi linux. Don't operator.

filename_saliency =  'saliency_map'   str(saliency_num)   '.png'

to:

filename_saliency =  f'{saliency_map} {str(saliency_num)}.png'

CodePudding user response:

you can save your image : for Example :

import cv2
img = cv2.imread ("Example.png", cv2.IMREAD_COLOR)
cv2.imwrite ("New image.png", img)

How to read the image in this module is different. There are many ways to read an image in different forms. Like black and white, color and alpha

# Open CV's flags :
IMREAD.UNCHANGED #: alpha channel
IMREAD.GRAYSCALE #: black and white
IMREAD.COLOR     #: colored image
  • Related