python怎么导出excel里面的图片_用Python玩转图片处理,并导出文件列表到Excel文件...

用Python玩转图片处理,并导出文件列表到Excel文件

发布时间:2020-07-01 03:37:45

来源:51CTO

阅读:716

作者:mb5c9f0bd46ed07

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值