Python_python读写图片以及对应的库比较

图片读写

通过numpy 来做数据计算的沟通
    JPEG 是一种有损格式,
	图像 PNG,是一种无损格式
cv2.imdecode()  作用是将图像数据从存储格式中解析出来并转化为OpenCV中的图像格式	imdecode得到的影像波段顺序是RGB
np.fromfile  将文本或二进制文件中数据构造成数组	
	
cv2.imencode 将图像编码到内存缓冲区中   将图片编码为二进制数组 _, img_buf = cv2.imencode(".png", img)
np.tofile函数是一个简便的方法,可以将numpy数组存储到二进制格式的文件中。 numpy.ndarray.tofile()

opencv2

 01. 图像的读取(cv2.imread) 是BGR
 02. 图像的保存(cv2.imwrite)
 03. 图像的显示(cv2.imshow)
 04. 用 matplotlib 显示图像(plt.imshow)

 Read and write images in color (BGR)Read an image file with cv2.imread()
 Write ndarray as an image file with cv2.imwrite()
C++: CV_IMWRITE_JPEG_QUALITY : imgcodecs_c.h 
    cv2.CV_IMWRITE_JPEG_QUALITY  设置图片格式为.jpeg或者.jpg的图片质量,其值为0---100(数值越大质量越高),默认95 
Python
    cv2.IMWRITE_JPEG_QUALITY 类型为Long,必须转换成int	 
 例如: cv2.imwrite("123.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
 
本地:读取本地图像文件时,往往会使用cv2.imread函数
在网络传输中,需要对图像数据进行编码,然后再进行传输,最后在解码显示。
   cv2.imdecode函数就是用来对编码后的图像二进制数据进行解码 
   cv2.imdecode(imgbuf, flags=-1)

pillow

pip install pillow
from PIL import Image

转换

 img = cv2.imread("test.jpg")
 img= Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
 img.save(filename, 'jpeg', icc_profile=im.info.get('icc_profile'))

使用Image模块的open()函数打开后,返回的图像对象的模式都是“RGB”
Saving
  The save() method supports the following options:
    quality
     The image quality, on a scale from 0 (worst) to 95 (best),  or the string keep. The default is 75. 
      Values above 95 should be avoided; 
      100 disables portions of the JPEG compression algorithm, and results in large files with hardly any gain in image quality. 
      The value keep is only valid for JPEG files and will retain the original image quality level, subsampling, and qtables.
     
    progressive
       If present and true, indicates that this image should be stored as a progressive JPEG file.
        图片是由模糊到清晰慢慢呈现的。这个就是ProgressiveJpeg所展示的渐进式加载
     exif
        If present, the image will be stored with the provided raw EXIF data.

参考

https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html 
https://docs.opencv.org/4.x/d8/d6a/group__imgcodecs__flags.html
posted @ 2024-01-18 17:05  辰令  阅读(119)  评论(0)    收藏  举报