网络编程
位置:首页>> 网络编程>> Python编程>> python批量提取图片信息并保存的实现

python批量提取图片信息并保存的实现

作者:Whao_5  发布时间:2021-10-08 05:21:14 

标签:python,批量,提取,图片信息

程序运行环境

python批量提取图片信息并保存的实现

code


# -*- coding:utf-8 -*-
# -----------------------------------
# @Time  : 2021/2/3 9:23
# @Author : HaoWu
# @File  : OutPixel.py
# ------------------------------------

import sys
import os
from glob import glob
from PIL import Image

sys.path.append('环境路径') # 本程序使用在本机创建的'wh'虚拟环境
source_dir = "目标文件夹路径" # 原始文件
filenames = glob('{}/*'.format(source_dir))

# 遍历文件夹下所有文件并保存在OutPixel.txt文件中
for filename in filenames:
 with Image.open(filename)as im:
   width,height = im.size
   f = open("输出文件夹路径.\OutPixel.txt", "a+")
   print('图片名:',filename, '图片宽:',width, '图片高:',height,
      '图片大小:',os.path.getsize(filename), file=f)
   f.close

运行结果展示

python批量提取图片信息并保存的实现

python批量提取图片信息并保存的实现

注意

  • 第21行语句如果用“w”,则只会保存一条信息

  • f = open("F:\code\输出目标文件夹下图片信息\OutPixel.txt", "a+")

  • “w” 只能操作写入,“r” 只能读取,“a” 向文件追加

  • “w+” 可读可写,"r+"可读可写,"a+"可读可追加

  • "wb+"写入进制数据

  • "w"模式打开文件,如果文件中有数据,再次写入内容,会把原来的覆盖掉

  • 打开txt文件时,如有中文需要使用gbk

来源:https://blog.csdn.net/Whao_5/article/details/113596215

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com