您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python 图像缩放批量处理程序

51自学网 2021-10-30 14:15:45
  python
这篇教程Python 图像缩放批量处理程序写得很实用,希望能帮到您。
#!/usr/bin/python
# -*- coding:utf8 -*-
 
#author@skillart www.
 
import os
import shutil
from PIL import Image
#import Image  
to_scale = 0.5
processIndex = 0
def resizeImg(imgPath):
  global processIndex
  fileList = []
  files = os.listdir(imgPath)
  for f in files:
    filePath = imgPath + os.sep + f
    if(os.path.isfile(filePath)):
      fileList.append(f)
    elif(os.path.isdir(filePath)):
      resizeImg(filePath)
  for fileName in fileList:
    processIndex+=1
    fileFullName = imgPath+os.sep+fileName
    suffix = fileName[fileName.rfind('.'):]
    if(suffix == '.bmp' or suffix == '.jpg'):
      #print 'processing the '+str(processIndex)+'th file:'+fileFullName
      img = Image.open(fileFullName)
      w,h = img.size
      tw = int(w * to_scale)
      th = int(h * to_scale)
      reImg = img.resize((tw,th),Image.ANTIALIAS)
      reImg.save(fileFullName)
      del reImg
if __name__ == '__main__':
  scaleStr = input('input to_scale: ')
  to_scale = float(scaleStr)
  scaledPath = './2'; #缩放后的文件目录
  if os.path.isdir(scaledPath):
    flag = input('the output dir is exist, sure to del it(y/n)')
    if flag == 'y' or flag == 'yes':
      try:   
        shutil.rmtree(scaledPath)
      finally:
        input('remove dir failed , please removed the dir manually.')
    else:
      exit
  shutil.copytree('./1',scaledPath)   # ./1为原图像目录
  resizeImg(scaledPath)
  input("resize success")
scalesize.py
2a74939d33b324733f5e68feb897e7a6.py (1.44 KB)

超详细注释之OpenCV dlib实现人脸采集
“Name 'raw_input' is not defined”解决方案
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。