#!/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()
图像处理写入excel
最新推荐文章于 2023-05-04 22:07:48 发布