• python图片压缩


      #!/usr/bin/env python
      # -*- coding: utf-8 -*-
      import Image
      import os

      def imageYasuo(srcPath,dstPath):
      for filename in os.listdir(srcPath):
      # 如果不存在目的目录则创建一个,保持层级结构
      if not os.path.exists(dstPath):
      os.makedirs(dstPath)

      # 拼接完整的文件或文件夹路径
      srcFile = os.path.join(srcPath, filename)
      dstFile = os.path.join(dstPath, filename)
      # print srcFile
      # print dstFile

      # 如果是文件就处理
      if os.path.isfile(srcFile):
      # 打开原图片缩小后保存,可以用if srcFile.endswith(".jpg")或者split,splitext等函数等针对特定文件压缩
      sImg = Image.open(srcFile)
      w, h = sImg.size
      print w, h
      if w != h :
      print "宽度不等于高度"
      print w,h
      print srcFile

      dImg = sImg.resize((1024, 1024), Image.ANTIALIAS) # 设置压缩尺寸和选项,注意尺寸要用括号
      dImg.save(dstFile) # 也可以用srcFile原路径保存,或者更改后缀保存,save这个函数后面可以加压缩编码选项JPEG之类的
      # print dstFile + " compressed succeeded"

      # 如果是文件夹就递归
      if os.path.isdir(srcFile):
      imageYasuo(srcFile, dstFile)


    if __name__ == "__main__":
    srcPath = "/home/siyin/素材库各20例(全部是png格式)"
    dstPath = "/home/siyin/素材库各20例(1024x1024)"
    imageYasuo(srcPath,dstPath)

  • 相关阅读:
    8.图形软件开发
    7.GDI绘图技术
    15.MFC网络通信
    JavaWeb:基于MVC设计模式的一个小案例(一)
    在虚拟机里连接PLC S7-200
    mark-又重新回到博客园
    早起的奇迹
    STM32-cJSON库的打包和解析
    Copley-STM32串口+CANopen实现双电机力矩同步
    DataStructure-链表实现指数非递减一元多项式的求和
  • 原文地址:https://www.cnblogs.com/sy-liu/p/7058216.html
Copyright © 2020-2023  润新知