99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀教你用 Python 快速批量轉(zhuǎn)換 HEIC 文件(cda干貨分享)
教你用 Python 快速批量轉(zhuǎn)換 HEIC 文件(cda干貨分享)
2021-12-27
收藏

作者:星安果

來源:AirPython

教你用 Python 快速批量轉(zhuǎn)換 HEIC 文件

1. 前言

大家好,我是安果!

最近打算做一批日歷給親朋好友,但是從 iPhone 上導(dǎo)出的照片格式是 HEIC 格式,而商家的在線制作網(wǎng)站不支持這種圖片格式

PS:HEIC 是蘋果采用的新的默認(rèn)圖片格式,它能在不損失圖片畫質(zhì)的情況下,減少圖片大小

有很多在線網(wǎng)站支持圖片批量轉(zhuǎn)換,但是安全隱私又沒法得到保證;如果使用 PS 等軟件去一張張轉(zhuǎn)換,浪費(fèi)時(shí)間的同時(shí)效率太低

本篇文章將使用 Python 批量實(shí)現(xiàn) HEIC 圖片文件的格式轉(zhuǎn)換

2. 準(zhǔn)備

首先,我們安裝 pyheif 依賴包

Linux 和 Mac OS 可以通過下面鏈接選擇合適的方式進(jìn)行安裝

https://pypi.org/project/pyheif/

如果是 Windows,我們只能下載 whl 依賴文件,使用 pip 命令進(jìn)行安裝

注意:我們需要根據(jù)系統(tǒng)及 Python 版本選擇對(duì)應(yīng)的文件進(jìn)行安裝

# 比如,本機(jī)是win10+64位 + Python3.7
# 通過下面鏈接下載文件:pyheif?0.6.1?cp37?cp37m?win_amd64.whl
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyheif
# 然后進(jìn)行虛擬環(huán)境
# 使用pip3命令安裝whl文件
pip3 install pyheif?0.6.1?cp37?cp37m?win_amd64.whl

然后,安裝 PIL 依賴,用于圖片處理

# 安裝依賴
pip3 install Pillow

3. 實(shí)戰(zhàn)

首先,遍歷源文件夾及子文件夾,獲取所有 HEIC 格式(不區(qū)分大小寫)的圖片

import pathlib
import os
def get_all_heic_imgs():
"""
獲取所有heic格式的圖片
:return:
"""

# heif_image_paths = glob.glob(r"{}/*.heic".format(source_path))
# 滿足條件的文件列表
filelist = []
for root, dirnames, filenames in os.walk(source_path):
for filename in filenames:
# filename:文件名、root:文件對(duì)應(yīng)的目錄
# 獲取文件后綴名
file_end = pathlib.Path(filename).suffix
# 文件名(不帶后綴)
file_name = pathlib.Path(filename).name.split(".")[-2]
if file_end in ['.heic''.HEIC']:
# 文件的完整目錄
# file_path = os.path.join(root, filename)
filelist.append({
"filename": file_name,
"filepath": os.path.join(root, filename)
})
return filelist

然后,遍歷文件列表,使用 pyheif 讀取文件,使用PIL 轉(zhuǎn)為二進(jìn)制圖片,以 JPG 格式保存到目標(biāo)文件夾下

import pyheif
from PIL import Image
# 讀取文件
img = pyheif.read(filepath)
img_bytes = Image.frombytes(mode=img.mode, size=img.size, data=img.data)
# 文件保存完整目錄
target_file_path = '{}/{}_{}.jpg'.format(target_path, filename, generate_random_num(6))
# 保存
img_bytes.save(target_file_path, format="jpeg")

由于圖片數(shù)目很多,圖片讀取、圖片保存都是耗時(shí)的 IO 操作,最后對(duì)程序進(jìn)行改造,利用多線程加快圖片轉(zhuǎn)換

另外,圖片可能會(huì)存在文件名重名,最后保存的文件名追加了一個(gè)隨機(jī)的字符串

import threading
def generate_random_num(count):
"""
產(chǎn)生一段隨機(jī)的字符串
:param count:
:return:
"""

return ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', count))
def convert_heic_to_jpg(file, semaphore):
"""
heic格式轉(zhuǎn)jpg
:param files:
:return:
"""

semaphore.acquire()
...
#文件操作
# 釋放
semaphore.release()
if __name__ == '__main__':
...
# 定義信號(hào)量,并發(fā)處理文件IO
semaphore = threading.BoundedSemaphore(20)
for file in files:
t = threading.Thread(target=convert_heic_to_jpg, args=(file, semaphore))
t.start()

4. 最后

通過上面的操作就可以快速將 HEIC 文件批量轉(zhuǎn)換為 JPG 文件,當(dāng)然如果想轉(zhuǎn)為其他圖片,比如:PNG,只需要更改 PIL 保存圖片的格式即可

如果你覺得文章還不錯(cuò),請(qǐng)大家 點(diǎn)贊、分享、留言 下,因?yàn)檫@將是我持續(xù)輸出更多優(yōu)質(zhì)文章的最強(qiáng)動(dòng)力!

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }