简体   繁体   English

如何在Python中保存灰度图像?

[英]How to save grayscale image in Python?

I am trying to save a grayscale image using matplotlib savefig(). 我正在尝试使用matplotlib savefig()保存灰度图像。 I find that the png file which is saved after the use of matplotlib savefig() is a bit different from the output image which is showed when the code runs. 我发现使用matplotlib savefig()之后保存的png文件与代码运行时显示的输出图像有些不同。 The output image which is generated when the code is running contains more details than the saved figure. 代码运行时生成的输出图像包含的详细信息比保存的图形还多。

How can I save the output plot in such a manner that all details are stored in the output image? 如何以所有细节都存储在输出图像中的方式保存输出图?

My my code is given below: 我的代码如下:

import cv2
import matplotlib.pyplot as plt

plt.figure(1)
img_DR = cv2.imread(‘image.tif',0)
edges_DR = cv2.Canny(img_DR,20,40)
plt.imshow(edges_DR,cmap = 'gray')
plt.savefig('DR.png')
plt.show()

The input file ('image.tif') can be found from here . 输入文件('image.tif')可以在这里找到。

Following is the output image which is generated when the code is running: 以下是代码运行时生成的输出图像:

在此处输入图片说明

Below is the saved image: 以下是保存的图像:

在此处输入图片说明

Although the two aforementioned images denote the same picture, one can notice that they are slightly different. 尽管上述两个图像表示同一张图片,但您可以注意到它们略有不同。 A keen look at the circular periphery of the two images shows that they are different. 敏锐地观察两个图像的圆形边缘,表明它们是不同的。

Save the actual image to file, not the figure. 将实际图像保存到文件中,而不是该图。 The DPI between the figure and the actual created image from your processing will be different. 图形与您根据处理实际创建的图像之间的DPI将有所不同。 Since you're using OpenCV, use cv2.imwrite . 由于您使用的是OpenCV,请使用cv2.imwrite In your case: 在您的情况下:

cv2.imwrite('DR.png', edges_DR)

Use the PNG format as JPEG is lossy and would thus give you a reduction in quality to promote small file sizes. 使用PNG格式是因为JPEG有损,因此会降低质量,从而无法缩小文件大小。 If accuracy is the key here, use a lossless compression standard and PNG is one example. 如果精度是关键,请使用无损压缩标准,PNG就是一个例子。


If you are somehow opposed to using OpenCV, Matplotlib has an equivalent image writing method called imsave which has the same syntax as cv2.imwrite : 如果您某种程度上反对使用OpenCV,则Matplotlib具有等效的图像写入方法imsave ,其语法与cv2.imwrite相同:

plt.imsave('DR.png', edges_DR, cmap='gray')

Note that I am enforcing the colour map to be grayscale for imsave as it is not automatically inferred like how OpenCV writes images to file. 请注意,我正在将颜色映射强制为灰度图以便进行imsave因为它不会像OpenCV如何将图像写入文件那样自动推断出来。

Since you are using cv2 to load the image, why not using it also to save it. 由于您正在使用cv2加载图像,所以为什么不同时使用它来保存图像。 I think the command you are looking for is : 我认为您正在寻找的命令是:

cv2.imwrite('gray.jpg', gray_image) cv2.imwrite('gray.jpg',gray_image)

Using a DPI that matches the image size seems to make a difference. 使用与图像大小匹配的DPI似乎有所不同。

The image is of size width=2240 and height=1488 ( img_DR.shape ). 图像的大小为width = 2240和height = 1488( img_DR.shape )。 Using fig.get_size_inches() I see that the image size in inches is array([7.24, 5.34]) . 使用fig.get_size_inches()我看到以英寸为单位的图像大小是array([7.24, 5.34]) fig.get_size_inches() array([7.24, 5.34]) So an appropriate dpi is about 310 since 2240/7.24=309.4 and 1488/5.34=278.65 . 由于2240/7.24=309.41488/5.34=278.65因此合适的dpi约为310。

Now I do plt.savefig('DR.png', dpi=310) and get 现在我做plt.savefig('DR.png', dpi=310)并得到

DPI = 310图片

One experiment to do would be to choose a high enough DPI, calculate height and width of figure in inches, for example width_inch = width_pixel/DPI and set figure size using plt.figure(figsize=(width_inch, height_inch)) , and see if the displayed image itself would increase/decrease in quality. 一个实验要做的是选择足够高的DPI,以英寸为单位计算图形的高度和宽度,例如width_inch = width_pixel/DPI并使用plt.figure(figsize=(width_inch, height_inch))设置图形大小,然后查看是否显示的图像本身会提高/降低画质。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM