图像处理写入excel

#!/usr/bin/env Python
# -*- coding:utf-8 -*-

import configparser
import os
import re
import logging
import log
import openpyxl

from PIL import ImageColor
from PIL import Image

log = log.de8ug_log()


class ImageUtils():
    '图像处理'
    def __init__(self):
        self.im = Image.open('et.jpg')
        self.image_rotate()
        self.mirror_flip()
        self.image_tailor()
        self.Excel()
        self.config()

    def image_rotate(self):
        '图片旋转'
        self.im.rotate(90).save('et_90.jpg')
        self.im.rotate(180).save('et_180.jpg')
        self.im.rotate(7).save('et_7.jpg')

    def mirror_flip(self):
        '镜像翻转'
        self.im.transpose(Image.FLIP_LEFT_RIGHT).save('et_水平.jpg')
        self.im.transpose(Image.FLIP_TOP_BOTTOM).save('et_上下.jpg')

    def image_tailor(self):
        '图片裁剪'
        print(self.im.format, self.im.size, self.im.mode)
        w, h = self.im.size
        log.info('尺寸:%sx%s' %(w, h))
        self.im.thumbnail((w//2, h//2))
        log.info('裁剪后尺寸为:%sx%s' %(w//2, h//2))
        self.im.save('et1.jpg')

    def Excel(self):
        '选取目录中所有图片类型文件(jpg,png,bmp),并用excel存档'
        excel_list1 = []
        excel_list2 = []
        # BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        BASE_DIR = r'E:\test\test3'
        for root, dirs, files in os.walk(BASE_DIR):
            for name in files:
                file = os.path.join(root, name)
                re_file = re.compile('(.*jpg$)|(.*png$)|(.*bmp$)')
                re_name = re_file.match(file)
                if re_name:
                    re_size = os.path.getsize(file)
                    excel_list1.append(name)
                    excel_list2.append(re_size)
        wb = openpyxl.load_workbook('et.xlsx')
        sh = wb.active
        line1 = 2
        line2 = 2
        for filename in excel_list1:
            # print(row1)
            sh['A1'] = "文件名"
            sh['A' + str(line1)] = filename
            line1 += 1
        for filesize in excel_list2:
            # print(row2)
            sh['B1'] = "文件大小"
            sh['B' + str(line2)] = filesize
            line2 += 1
        wb.save('et.xlsx')

    def config(self):
        '添加配置文件'
        self.base_dir = r'E:\test\test3\et2'
        self.config = configparser.ConfigParser()
        self.config['DEFAULT'] = {'base_dir': r'E:\test\test3'}
        self.config['et'] = {}
        self.config['et']['base'] = r'E:\test\test3\et2'
        self.config['et']['log_path'] = "E:\新建文件夹\test3\et1\et\memo"
        self.config['et']['et_type'] = 'ini'
        self.config['et']['file_name'] = 'Et_image.py'
        self.save()
        
    def save(self):
        '保存配置文件内容'
        config_path = os.path.join(self.base_dir, 'test.ini')
        with open(config_path, 'w') as f:
            self.config.write(f)


def main():
    I = ImageUtils()

if __name__ == "__main__":
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值