python手记(28)
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import cv2
import numpy as np
fn="test2.jpg"
if __name__ == '__main__':
print 'http://blog.csdn.net/myhaspl'
print 'myhaspl@qq.com'
print
print 'loading %s ...' % fn
img = cv2.imread(fn)
sp=img.shape
print sp
#height
sz1=sp[0]
#width
sz2=sp[1]
print 'width:%d\nheight:%d'%(sz2,sz1)
#创建一个窗口并显示图像
cv2.namedWindow('img')
cv2.imshow('img', img)
#复制图像
myimg2= img.copy();
cv2.namedWindow('myimg2')
cv2.imshow('myimg2', myimg2)
#复制并转换为黑白图像
myimg1=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.namedWindow('myimg1')
cv2.imshow('myimg1', myimg1)
cv2.waitKey()
cv2.destroyAllWindows()
关于图像保存问题
- Python: cv2. imwrite (filename, img [, params ] ) → retval
- C: int cvSaveImage (const char* filename, const CvArr* image, const int* params=0 )
-
Parameters: - filename – Name of the file.
- image – Image to be saved.
- params –
Format-specific save parameters encoded as pairs paramId_1,paramValue_1, paramId_2, paramValue_2, ... . The following parameters are currently supported:
- For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.
- For WEBP, it can be a quality ( CV_IMWRITE_WEBP_QUALITY ) from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.
- For PNG, it can be the compression level (CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
- For PPM, PGM, or PBM, it can be a binary format flag (CV_IMWRITE_PXM_BINARY ), 0 or 1. Default value is 1.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import cv2
import numpy as np
fn="test2.jpg"
if __name__ == '__main__':
print 'http://blog.csdn.net/myhaspl'
print 'myhaspl@qq.com'
print
print 'loading %s ...' % fn
img = cv2.imread(fn)
#复制并转换为黑白图像
myimg1=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#保存图像
myimg2=cv2.imwrite("test3.png", myimg1,[int(cv2.IMWRITE_PNG_COMPRESSION), 7])
img = cv2.imread("test3.png")
cv2.namedWindow('img')
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
浙公网安备 33010602011771号