python图片转excel,用Python玩转图片处理,并导出文件列表到Excel文件
1、用Python玩转图片处理
class ImageUtils:
""" 图片处理工具 """
def __init__(self, source_dir, target_dir):
self.source_dir = source_dir
self.target_dir = target_dir
def thumbnail(self, filename, percent=0.5):
'缩略图'
im = Image.open(os.path.join(self.source_dir, filename))
w, h = im.size
print('Original image size: %sx%s' % (w, h))
im.thumbnail((int(w*percent), int(h*percent)))
print('Thumbnail image to: %sx%s' % (int(w*percent), int(h*percent)))
output = os.path.join(self.target_dir, filename.split('.')[0]+'-thumbnail.jpg')
im.save(output, 'jpeg')
def resize(self, filename, horizontal_ratio=0.5, vertical_ratio=0.5):
'调整大小'
im = Image.open(os.path.join(self.source_dir, filename))
w, h = im.size
print('Original image size: %sx%s' % (w, h))
im_size = im.resize((int(w*horizontal_ratio), int(h*vertical_ratio)))
print('Resize image to: %sx%s' % (int(w*horizontal_ratio), int(h*vertical_ratio)))
output = os.path.join(self.target_dir, filename.split('.')[0]+'-resize.jpg')
im_size.save(output, 'jpeg')
def enhance(self, filename, enhance_ratio=1.3):
'图片对比度增强'
im = Image.open(os.path.join(self.source_dir, filename))
enh = ImageEnhance.Contrast(im)
print(f'图像对比度增强: {enhance_ratio}倍')
output = os.path.join(self.target_dir, filename.split('.')[0]+'-enhance.jpg')
enh.enhance(enhance_ratio).save(output, 'jpeg')
def region(self, filename, snap=(0.1, 0.1, 0.9, 0.9)):
'截取一块区域'
im = Image.open(os.path.join(self.source_dir, filename))
w, h = im.size
box = (int(w*snap[0]), int(h*snap[1]), int(w*snap[2]), int(h*snap[3]))
print(f'图像截取区域: {box}')
region = im.crop(box)
output = os.path.join(self.target_dir, filename.split('.')[0]+'-region.jpg')
region.save(output, 'jpeg')
def rotate(self, filename, angle=0):
'旋转图片,翻转'
im = Image.open(os.path.join(self.source_dir, filename))
print(f'图像旋转: {angle}°')
output = os.path.join(self.target_dir, filename.split('.')[0]+'-rotate.jpg')
im.rotate(angle).save(output, 'jpeg')
def flip(self, filename, horizontal=False, vertical=False):
'翻转'
im = Image.open(os.path.join(self.source_dir, filename))
if horizontal:
print('图像水平翻转')
im = im.transpose(Image.FLIP_LEFT_RIGHT)
if vertical:
print('图像上下翻转')
im = im.transpose(Image.FLIP_TOP_BOTTOM)
output = os.path.join(self.target_dir, filename.split('.')[0]+'-flip.jpg')
im.save(output, 'jpeg')
def add_logo(self, filename, logo_file):
'添加水印'
im_logo = Image.open(os.path.join(self.source_dir, logo_file))
logo_width, logo_height = im_logo.size
im_target = Image.open(os.path.join(self.source_dir, filename))
target_width, target_height = im_target.size
im_copy = im_target.copy()
print('图像添加水印')
im_copy.paste(im_logo, (target_width-logo_width, target_height-logo_height), im_logo)
output = os.path.join(self.target_dir, filename.split('.')[0]+'-add_logo.jpg')
im_copy.save(output, 'jpeg')
def new_image(self, text='Text'):
'创建新图片'
im_new = Image.new('RGBA', (400, 400), 'white')
print('创建新图片')
pic = ImageDraw.Draw(im_new)
print('图片添加文字')
pic.text((50, 50), text, fill='blue')
output = os.path.join(self.target_dir, 'new_image.png')
im_new.save(output)
def msyh_font(self, text='文字'):
'设置字体'
im_new = Image.new('RGBA', (400, 400), 'white')
pic = ImageDraw.Draw(im_new)
fonts_path = r'D:\VSCode\xuan_demo_v0302\fonts\msyh.ttf'
msyh = ImageFont.truetype(fonts_path, 40)
print('文字格式为微软雅黑')
pic.text((50, 50), text, fill='blue', font=msyh)
output = os.path.join(self.target_dir, 'new_image_msyh.png')
im_new.save(output)
2、导出文件列表到Excel文件
class ImageSystem:
""" 图片列表生成excel文件系统"""
def __init__(self, dirname):
self.dirpath = dirname
def listfile(self, img_type='.jpg'):
'获取指定路径下所有的图片文件, 返回列表'
img_File_List = os.listdir(self.dirpath) # 图片列表
image_list = []
for filename in img_File_List:
filepath = os.path.join(self.dirpath, filename) # 图片的绝对路径
if os.path.isdir(filepath):
self.listfile(filepath)
print(filepath)
else:
if os.path.isfile(filepath) and filename.lower().endswith(img_type):
print(os.path.join(self.dirpath, filename))
image_list.append(os.path.join(self.dirpath, filename))
return image_list
def xlsx_create(self, image_list, filename='Workbook.xlsx'):
'创建excel表格'
wb = Workbook()
ws = wb.active
ws['A1'] = '文件名:'
for s in image_list:
ws.append([s, self.get_FileSize(s)])
ws.append([datetime.datetime.now()])
output = os.path.join(r'D:\VSCode\xuan_demo_v0302\test', filename)
print('创建excel表格')
wb.save(output)
def get_FileSize(self, filename):
'获取文件的大小,结果保留两位小数,单位为kb'
fsize = os.path.getsize(filename)
fsize = fsize/float(1024)
return f'{round(fsize, 2)} kb'
3、运行
def main():
source_dir = r'D:\VSCode\xuan_demo_v0302\image'
target_dir = r'D:\VSCode\xuan_demo_v0302\test'
test_image = ImageUtils(source_dir, target_dir)
test_image.thumbnail('scenery.jpg', 0.9)
test_image.resize('scenery.jpg', 0.9, 0.5)
test_image.enhance('scenery.jpg', 1.5)
test_image.region('scenery.jpg', snap=(0.2, 0.2, 0.55, 0.5555))
test_image.rotate('scenery.jpg', angle=10)
test_image.flip('scenery.jpg', horizontal=True, vertical=True)
test_image.add_logo('scenery.jpg', 'logo.png')
test_image.new_image()
test_image.msyh_font()
image_file = ImageSystem(source_dir)
image_list = image_file.listfile()
image_file.xlsx_create(image_list, 'image_list.xlsx')
if __name__ == "__main__":
main()
python图片转excel,用Python玩转图片处理,并导出文件列表到Excel文件相关推荐
- python 读取excel图片_如何用Python读取Excel中图片?
公众号: 早起Python 作者:刘早起 大家好,在使用Python进行办公自动化操作时,一定少不了与Excel表格的交互,我们通常是用pandas处理表格数据,但大多数情况下,都是读取表格中的数值进 ...
- python小程序嵌入excel_用原生的方式操作Excel,Python玩转Excel神器xlsxwriter详解!...
大家好,在之前的Python办公自动化系列文章中,我们已经介绍了两个Python操作Excel的库openpyxl与xlwings,并且相信大家已经了解这两者之间的异同. 但是在Python中操作Ex ...
- python 获取excel信息,下载对应图片
python 获取excel信息,下载对应图片 import pandas as pd from openpyxl import load_workbook import requests impor ...
- 用原生的方式操作Excel,Python玩转Excel神器xlsxwriter详解!
来源:早起Python 本文就将介绍一个强大的库xlsxwriter.来学习如何用原生的方式操作Excel! 首先还是来简单了解下这三个库之间的区别 " openpyxl:只允许读取和写入. ...
- python将图片保存到excel_使用Python导出Excel图表以及导出为图片的方法
本篇讲下如何使用纯python代码将excel 中的图表导出为图片.这里需要使用的模块有win32com.pythoncom模块. 网上经查询有人已经写好的模块pyxlchart,具体代码如下: fr ...
- python爬取王者_Python爬取王者荣耀英雄图片及装备!你玩吗?
在玩王者荣耀的时候一直想把装备合英雄的图片保存下来,但是官网的单个图片保存太慢一气之下写了个爬虫. 1.爬取装备 import requests from bs4 import BeautifulSo ...
- python暴力破解excel_使用 Python 读写 Excel 文件(一)
项目要求 如果说是 Office 办公软件使得 Windows 成为主流的操作系统,那么 Excel 就是使得微软硬起来的法宝! Word 和 PowerPoint 都有不相上下的对手,但 Excel ...
- excel python 形状_何使用Python操作Excel绘制柱形图
开发工具,环境 PyCharm Python3 Office Excel 我们已经创建好了一张Excel表. 现在我们要根据已有的数据,往里面添加柱形图. 柱形图 BarChart 我们主要使用Bar ...
- python菜鸟excel教程-Python操作Excel的Xlwings教程(一)
在日常的工作中,我们或多或少的都要和Excel打交道.甚至在一些领域,某一些业务人员的主要工作就是处理Excel表格,处理大量的Excel数据并生成一系列的报表.对于程序员朋友们来说,更喜欢以代码的形 ...
- excel调用python编程-使用python集合进行EXCEL数据分析
使用python集合进行EXCEL数据分析 标准库 Python真正精彩的方面之一是它具有非常丰富的模块标准库 ,无需安装第三方模块即可进行一些复杂的编程. 如果您在系统上有效安装了python,则可 ...
最新文章
- .NET业务实体类验证组件Fluent Validation
- Be a new gentleman
- altium designer PCB板厚度设置
- iOS开发中标签控制器的使用——UITabBarController
- Topcoder SRM 630div 2
- 一篇需要膜拜的文篇--Javascript异步编程模型进化(转)
- linux接口 头文件,第一种:1、添加关键头文件:#include linux/of_gpio.h#include linux/gpio.h...
- [EmguCV|C#]使用CvInvoke自己繪製色彩直方圖-直方圖(Hitsogram)系列(4)
- 三角形外接球万能公式_秒杀三角形问题!!三角形分角线的几个重要结论及其应用...
- osi7层协议源代码_捍卫开放源代码的17年:立即加入OSI
- Linux文件系统性能测试工具fdtree和iozone
- matlab 元胞调用方法你真的理解了么?
- 看完上汽制动的数字化,才发现以前的数据可视化大屏都白做了
- BayaiM__MySQL错误对照表
- PHP switch问题
- jdk1.8 64位与32位免费下载
- rapidminer进行关联分析、分类预测、聚类分析
- 蛋白胶条质谱鉴定实验
- 怎么做科研你懂吗?道翰天琼认知智能机器人平台API接口大脑为您揭秘。
- android 指南针图片,如何在android中制作自定义指南针视图
热门文章
- 三个视频教你如何找到另一半
- 计算机导师问读研计划和后续计划,考研面试,问“研究生时期的规划”怎么回答急...
- 商城APP开发关键板块
- 2016年1月.CN域名总量净增44.7万 增速环比下降
- UPnP 端口映射服务威胁分析
- android ca,Android CA证书安装流程
- 3DMAX 切换视图+切换到摄像机视角
- matlab 差分 平稳时间序列,利用时间序列模型预测贵阳市烟草生长期内的降水量...
- 从游戏商业思维中分析游戏用户行为数据(主要是参考网上的一些感想)
- 概率论与数理统计(5):大数定律与中心极限定律