Python?jpg快速轉(zhuǎn)png并調(diào)整大小方式
更新時間:2023年03月25日 08:40:04 作者:咲奈
這篇文章主要介紹了Python實現(xiàn)jpg快速轉(zhuǎn)png并調(diào)整大小方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Python jpg轉(zhuǎn)png并調(diào)整大小
很簡單的小程序,可以直接將本文件夾下所有 .jpg 轉(zhuǎn)為 .png 格式,并自定義大小。
依賴
需要安裝 pillow 庫:
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)整大小需要括號起來。
缺點
- 不能去白底
python把jpg圖片批量轉(zhuǎn)化為png圖片
# python圖片格式jpg轉(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/")
# 對圖像排序
files.sort(key=lambda x: int(x.split(".")[0]))
# 獲取圖像寬高
h, w, _ = cv2.imread("images/" + files[0]).shape
# 設置幀數(shù)
fps = 30
vid = []
'''
設置要保存的格式
mp4:
mp4v
avi:
xvid
i420
'''
# 保存視頻路徑和名稱
# save_path = "video/video.mp4" # 保存視頻路徑和名稱 MP4格式
save_path = "video/video.avi" # 保存視頻路徑和名稱 av格式
# 準備寫入視頻
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!")
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python數(shù)據(jù)類型--字典dictionary
這篇文章主要介紹了Python數(shù)據(jù)類型字典dictionary,字典是另一種可變?nèi)萜髂P停铱纱鎯θ我忸愋蛯ο?。下面詳細?nèi)容需要的小伙伴可以參考一下,希望對你有所幫助2022-02-02
Python PyQt5實戰(zhàn)項目之網(wǎng)速監(jiān)控器的實現(xiàn)
PyQt5以一套Python模塊的形式來實現(xiàn)功能。它包含了超過620個類,600個方法和函數(shù)。它是一個多平臺的工具套件,它可以運行在所有的主流操作系統(tǒng)中,包含Unix,Windows和Mac OS。PyQt5采用雙重許可模式。開發(fā)者可以在GPL和社區(qū)授權之間選擇2021-11-11
Python openpyxl 遍歷所有sheet 查找特定字符串的方法
今天小編就為大家分享一篇Python openpyxl 遍歷所有sheet 查找特定字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Numpy中np.vstack()?和?np.hstack()?的實現(xiàn)
本文主要介紹了Numpy中np.vstack()和np.hstack()的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-04-04

