Use python to convert webp format pictures to png or jpg with one click

table of Contents

1. Blog introduction

2. Content

.Main content

.Batch one-click call

3. Push

4. Conclusion


1. Blog introduction

Recently, there are a lot of webp format pictures in the resources picked from the webpage, which need to be converted to png or jpg. I haven’t seen python before. I read a little tool and wrote a small tool that can convert all webp format files in the same level directory. The pictures are all converted to png or jpg. The blogger is developing in the python2.7 environment. The effect is as follows:


2. Content

 

.Main content

Go directly to the code, there is nothing to circumvent, it is to retrieve all the webp in the directory and then convert it to png, and then delete the original image after saving, the code is very full of comments


# 功能 : 将当前工作目录下所有webp格式转为png or jpg
# -*- coding: UTF-8 -*-
import os
from PIL import Image

# 返回当前工作目录
CURRENT_PATH = os.getcwd()

# 转换格式
IMG_EXP = ".png"

# 获取最高所有文件
cur_all_files   = os.listdir(CURRENT_PATH)
# 转换列表
imgList         = []

# 遍历文件夹,储存webp格式的路径到列表内
def findFileForImage(filePath):
    child_all_files = os.listdir(filePath)
    for child_file_name in child_all_files:
    	sPath = os.path.join(filePath, child_file_name)
    	if os.path.isdir(sPath):
    		findFileForImage(sPath)
    	n,e = os.path.splitext(child_file_name)
    	if e.lower() == ".webp":
    		imgList.append(os.path.join(filePath, n))


# 检索目录下所有的webp文件,如果是文件夹则继续向下检索
for file_name in cur_all_files:
    nPath = os.path.join(CURRENT_PATH, file_name)
    # 文件夹
    if os.path.isdir(nPath):
    	findFileForImage(nPath)
        continue
    # 储存
    name, ext = os.path.splitext(file_name)
    if ext.lower() == ".webp":
        imgList.append(os.path.join(CURRENT_PATH, name))


# 转换图片
def convertImage():
	for webpPath in imgList:
		print(webpPath)

		# 打开图片并赋值一份新的图片
		img = Image.open(webpPath+".webp")
		img.load()
		# 将赋值的图片修改后缀保存在原路径
		img.save(webpPath+IMG_EXP)
		# 删除原webp图
		os.remove(webpPath+".webp")

# 执行
convertImage()


.Batch one-click call

Make a batch file here and call it directly with one key

@echo off


rem %0         代指批处理文件自身
rem %~d0       是指批处理所在的盘符
rem %~dp0      是盘符加路径

rem cd %~dp0   就是进入批处理所在目录了
 
echo local_cap  
C:  
cd %~dp0
start python ConvertImage.py 
rem 使用ping命令暂停3s,这样可以看到调用python后的结果
::ping -n 10 127.0.0.1 > nul 

3. Push

Source code: https://github.com/KingSun5/WebpToPngByPython


4. Conclusion

If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !
 

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/106303440