python resize

import sys
import os
sys.path.append('/usr/local/lib/python2.7/site-packages')
sys.path.append('/usr/lib/python2.7/dist-packages')
import matplotlib.pyplot as plt
import cv2

def resizeimg(filename):
    img=cv2.imread(filename)
    shape=img.shape
    
    w=shape[1]
    h=shape[0]
    ww=w
    hh=h
    bresize=False
    if w > h and w > 800:
        ww=800
        hh=h/(w/800)
        bresize=True
    if h > w and h > 800:
        hh=800
        ww=w/(h/800)
        bresize=True
    if bresize:    
        print filename
        img=cv2.resize(img,(ww,hh))
        cv2.imwrite(filename,img)
    
for r, d, files in os.walk( '/var/Public/img/' ):
    for ffile in files:
        if ffile.endswith( 'jpg' ):
            resizeimg(r+'/'+ffile)

  

posted @ 2017-03-23 16:39  ahuo  阅读(2956)  评论(0编辑  收藏  举报