0

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!

Pressing_Keys_24_7
  • 1,755
  • 2
  • 7
  • 33
  • most likely your datatypes and or scaling. Type needs to be unit8 and values from 0 to 255 – Eumel Apr 08 '22 at 13:28
  • You can use `saliency_map_int= cv2.normalize(src=saliency_map, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)` and then save your image – Jeru Luke Apr 08 '22 at 13:31
  • Does this answer your question? [OpenCV imwrite saving complete black jpeg](https://stackoverflow.com/questions/10571874/opencv-imwrite-saving-complete-black-jpeg) – Christoph Rackwitz Apr 08 '22 at 13:43

2 Answers2

2

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
1

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'
toyota Supra
  • 3,181
  • 4
  • 15
  • 19