PythonⅡ

A: batch processing file

1. batch rename

Import OS
 DEF the rename (): 
    I = 0 
    path = R & lt ' / the Users / lorristar / Desktop / Zio '  # access route 
    Filelist = the os.listdir (path) 
     for Files in Filelist: 
        I = I +. 1 
        olddir = the os.path. the Join (path, files)   # old file path 
        IF os.path.isdir (olddir):   # skip folder 
                the Continue 
        filename = ' Zi02 '      # new file name 
        filetype = ' .jpg '       #扩展名
        Newdir = os.path.join(path, filename + str(i) + filetype)
        os.rename(Olddir, Newdir)
    return True
if __name__ == '__main__':
    rename()

 

2. Batch delete

import os
    def del_files(path):
        for root,dirs,files in os.walk(path):
                for name in files:
                    if '.jpg' in name:
                     os.remove(os.path.join(root,name))
                    print('Delete files:',os.path.join(root,name))   
if __name__=='__main__':
    path=r'/Users/lorristar/Desktop/zio'
    del_files(path)

Two: the basic operation of self-realization of several pictures

1. Simple display

Import cv2 
IMG = cv2.imread ( " /Users/lorristar/Desktop/zio/zio.jpg " ) # read image 
# cv2.namedWindow ( 'Zio') 
cv2.imshow ( ' Zio ' , IMG) # display image 
cv2 .waitKey (0) # hold window 
cv2.destroyWindow ( " Zio " )

2. The color conversion

Import CV2 
img = cv2.imread ( " /Users/lorristar/Desktop/zio/zio.jpg " ) # the Read 
img1 = cv2.cvtColor (img, cv2.COLOR_RGB2GRAY) # change colors and create a picture 
cv2.imwrite ( " /Users/lorristar/Desktop/zio/zio-gray.jpg " , img1) # save the new image

3. Turn the picture

Import CV2 
IMG = cv2.imread ( " /Users/lorristar/Desktop/zio/zio.jpg " ) 
IMG1 = cv2.flip (IMG, flipCode = 0) # flipCode = 0 flipped vertically flipped horizontally flipCode = 1 flipCode = -1 rotating around the center point 
cv2.imwrite ( " /Users/lorristar/Desktop/zio/zio-filp.jpg " , img1)

4. Simple stitching

import cv2
img = cv2.imread("/Users/lorristar/Desktop/zio/zio.jpg") 
img2 = cv2.imread("/Users/lorristar/Desktop/zio/zio.jpg") 
img3 =cv2.hconcat([img,img2])#hconcat 水平拼接 vconcat 垂直拼接
cv2.imwrite("/Users/lorristar/Desktop/zio/zio-concat.jpg",img3)

The partial shearing

Import CV2 
IMG = cv2.imread ( " /Users/lorristar/Desktop/zio/zio.jpg " ) 
img3 = IMG [300: 400,50: 300] # Y cut start: end y, start x: the end of the X- 
cv2.imwrite ( " /Users/lorristar/Desktop/zio/zio-frame.jpg " , img3) 
cv2.imshow ( " Image " , img3)   # reads cut picture 
cv2.waitKey (0)

6. Picture zoom

import cv2
img = cv2.imread("/Users/lorristar/Desktop/zio/zio.jpg") 
img2 = cv2.resize(img,(165,213), interpolation = cv2.INTER_CUBIC) # 缩放
cv2.imwrite("/Users/lorristar/Desktop/zio/zio-resize.jpg",img2)

CV_INTER_NN - nearest neighbor interpolation,  

CV_INTER_LINEAR - bilinear interpolation (used by default)  

CV_INTER_AREA - using the relationship between the pixel resampling. When the image is reduced when this method avoids the occurrence of ripples.

CV_INTER_CUBIC - cubic interpolation. 

7. drawing code

Import CV2
 Import numpy AS NP 

IMG   = 255 * np.ones ((350,512,3), np.uint8) # of unit8: 0 ~ 255 
font = cv2.FONT_HERSHEY_DUPLEX
 # font set font = cv2.FONT_HERSHEY_COMPLEX # 

# text # picture object, text, position, font, font size, color, font weight 
cv2.putText (IMG, " Happy Day " , (50,300), font, 0.8, (25, 25, 25), 2,) # color can adjust themselves, range 0-255 
# line start and end points color # thickness 
cv2.line (IMG, (50, 310), (185,310), (0, 0,0),. 4 )
 # rectangle lower right vertex of the upper left vertex # 
cv2.rectangle (img, ( 80,8), (200, 100), (0, 255, 0), 2 )
 #Color control center radius circular # -1 indicates whether the filling is filled 
cv2.circle (IMG, (60, 60), 30, (0,0,213),. 1 )
 # elliptical center of the longest minor axis # deflection angle, start angle , end angle 
cv2.ellipse (IMG, (100,300), (100,50), 180,0,360, (20,213,79),. 1 ) 
cv2.imshow ( " the Draw " , IMG) 
cv2.waitKey (0)

 


 

Guess you like

Origin www.cnblogs.com/bob3000/p/11785997.html