python 图像格式转换文件夹下 jpg 转 bmp | 目录遍历
【摘要】
python 图像格式转换文件夹下 jpg 转 bmp
import os
from PIL import Image
def jpgToBmp(imgFile):
dst_dir =...
python 图像格式转换文件夹下 jpg 转 bmp
import os
from PIL import Image
def jpgToBmp(imgFile):
dst_dir = "/home/moli/project/data/xiaoming2"
for fileName in os.listdir(imgFile):
if os.path.splitext(fileName)[1] == '.jpg':
name = os.path.splitext(fileName)[0]
newFileName = name + ".bmp"
img = Image.open(imgFile + "/" + fileName)
img.save(dst_dir+"/"+newFileName)
def main():
imgFile = "/home/moli/project/data/xiaoming"
jpgToBmp(imgFile)
if __name__ == '__main__':
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
目录遍历
import os
# 遍历文件夹
def walkFile(file):
for root, dirs, files in os.walk(file):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历文件
for f in files:
print(os.path.join(root, f))
# 遍历所有的文件夹
for d in dirs:
print(os.path.join(root, d))
# 获取所有.jpg 文件名称,不带后缀
def GetImgName(dir):
listName = []
for fileName in os.listdir(dir):
if os.path.splitext(fileName)[1] == '.jpg':
fileName = os.path.splitext(fileName)[0]
listName.append(fileName)
print(listName)
return listName
def main():
dirPath = "/home/moli/project/data/"
walkFile(dirPath)
print("dirPath = %s" % (dirPath))
print(os.path.dirname(dirPath))
print(os.path.basename(dirPath))
filePath = "/home/moli/project/data/fileLoop.py"
print("filePath = %s" % (filePath))
print(os.path.dirname(filePath))
print(os.path.basename(filePath))
print("\n")
print("\n")
imgFile = "/home/moli/project/data/clearImg"
listName = GetImgName(imgFile)
if __name__ == '__main__':
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
文章来源: positive.blog.csdn.net,作者:墨理学AI,版权归原作者所有,如需转载,请联系作者。
原文链接:positive.blog.csdn.net/article/details/116709265
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)