Python?jpg快速轉(zhuǎn)png并調(diào)整大小方式
Python jpg轉(zhuǎn)png并調(diào)整大小
很簡(jiǎn)單的小程序,可以直接將本文件夾下所有 .jpg 轉(zhuǎn)為 .png 格式,并自定義大小。
依賴
需要安裝 pillow 庫(kù):
pip install pillow
代碼
# -*- coding:utf-8 -*-
# file name: jpg2png.py
import os
from PIL import Image
list = os.listdir()
for i in list:
? ? if i != 'jpg2png.py':
? ? ? ? print(i)
? ? ? ? Image.open(i).convert('RGBA').resize((512, 512)).save(i[:i.find('.')] + '.png')注意本文件名,還有調(diào)整大小需要括號(hào)起來(lái)。
缺點(diǎn)
- 不能去白底
python把jpg圖片批量轉(zhuǎn)化為png圖片
# python圖片格式j(luò)pg轉(zhuǎn)換為png(批量處理,尺寸不變)
import os
import PIL.Image as Image
def changeJpgToPng(srcPath, dstPath):
# 修改圖像大小
image = Image.open(srcPath)
# 將jpg轉(zhuǎn)換為png
png_name = str(dstPath)[0:-len('.jpg')] + '.png'
# image.save(png_name)
# print(png_name)
# image = image.convert('RGBA')
image = image.convert('RGB')
image.save(png_name)
pass
if __name__ == '__main__':
listPath = 'D:/BYLW666/deeplabv3-plus-pytorch-main/VOCdevkit/VOC2007/jpg/'#jpg圖片
srcPath = 'D:/BYLW666/deeplabv3-plus-pytorch-main/VOCdevkit/VOC2007/src/'#jpg圖片
dstPath = 'D:/BYLW666/deeplabv3-plus-pytorch-main/VOCdevkit/VOC2007/png/'#png圖片
print("開始轉(zhuǎn)換...")
filename_list = os.listdir(listPath)
for d in filename_list:
if d.count('.jpg') > 0:
changeJpgToPng(srcPath + d, dstPath + d)
pass
print("完成了...")
二、視頻轉(zhuǎn)圖像或圖像轉(zhuǎn)視頻
import cv2
import os
def video2image():
cap = cv2.VideoCapture("video/01.mp4")
count = 1
while True:
success, frame = cap.read()
if success == False:
break
cv2.imwrite("images/%d.jpg" % count, frame)
count += 1
def image2video():
# 得到圖像路徑
files = os.listdir("images/")
# 對(duì)圖像排序
files.sort(key=lambda x: int(x.split(".")[0]))
# 獲取圖像寬高
h, w, _ = cv2.imread("images/" + files[0]).shape
# 設(shè)置幀數(shù)
fps = 30
vid = []
'''
設(shè)置要保存的格式
mp4:
mp4v
avi:
xvid
i420
'''
# 保存視頻路徑和名稱
# save_path = "video/video.mp4" # 保存視頻路徑和名稱 MP4格式
save_path = "video/video.avi" # 保存視頻路徑和名稱 av格式
# 準(zhǔn)備寫入視頻
vid = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
# vid = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'xvid'), fps, (w, h))
# vid = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'i420'), fps, (w, h))
# 寫入
for file in files:
img = cv2.imread("images/" + file)
vid.write(img)
if __name__ == '__main__':
print("start...")
video2image()
#image2video()
print("OK!")
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python Flask異步發(fā)送郵件實(shí)現(xiàn)方法解析
這篇文章主要介紹了Python Flask異步發(fā)送郵件實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Python處理時(shí)間日期坐標(biāo)軸過(guò)程詳解
這篇文章主要介紹了Python處理時(shí)間日期坐標(biāo)軸過(guò)程詳解,當(dāng)日期數(shù)據(jù)作為圖表的坐標(biāo)軸時(shí)通常需要特殊處理,應(yīng)為日期字符串比較長(zhǎng),容易產(chǎn)生重疊現(xiàn)象,需要的朋友可以參考下2019-06-06
OpenCV+Python幾何變換的實(shí)現(xiàn)示例
這篇文章主要介紹了OpenCV+Python幾何變換的實(shí)現(xiàn)示例,圖像的幾何變換是指將一幅圖像映射到另一幅圖像內(nèi)。有縮放、翻轉(zhuǎn)、仿射變換、透視、重映射等操作。感興趣的可以了解一下2021-03-03
opencv導(dǎo)入頭文件時(shí)報(bào)錯(cuò)#include的解決方法
這篇文章主要介紹了opencv導(dǎo)入頭文件時(shí)報(bào)錯(cuò)#include的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
用 Python 元類的特性實(shí)現(xiàn) ORM 框架
利用 Python 元類的特性實(shí)現(xiàn) ORM 框架的 insert 功能,通過(guò)操作類對(duì)象,對(duì)數(shù)據(jù)表進(jìn)行數(shù)據(jù)增加操作。由于 ORM 比較復(fù)雜,也不要重復(fù)造輪子,就完成一個(gè) insert 相類似的ORM,理解其中的道理即可。2021-05-05
剖析Django中模版標(biāo)簽的解析與參數(shù)傳遞
這篇文章主要介紹了剖析Django中模版標(biāo)簽的解析與參數(shù)傳遞,Django是重多高人氣Python框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07

