0

Only way I know to save images in grayscale is by using imshow, but the problem with this is it displays/prints the image which I don't want. Is there another way to save in grayscale without printing? This is the code:

savethis=gen_imgs

for i in range(r*c):
    #img=1
    img=plt.imshow(savethis[i, :,:,0],cmap='gray_r')
    img=plt.axis('off')
    img=plt.savefig('/Users/User/Documents/UCSDproject/SV_cluster/' + str(i)+'.jpg')
srv_77
  • 547
  • 1
  • 8
  • 20
  • You can, for example, use `cv2`: `cv2.imwrite('file_path.jpg', savethis[i,:,:,0])`. – Quang Hoang Feb 12 '21 at 04:08
  • 1
    Do you want to save the ACTUAL IMAGE? Then, follow [Mark's answer](https://stackoverflow.com/a/66168313/11089932). If you want to save the PLOT OF THE IMAGE simply without showing, follow [this Q&A](https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib). – HansHirse Feb 12 '21 at 08:14

1 Answers1

1

Amongst others, you can use:

  • PIL Image.fromarray(grey).save('result.jpg')

  • OpenCV cv2.imwrite('result.jpg', grey)

  • imageio

  • wand

  • vips

  • tifffile

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432