欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中圖片文件路徑格式如何使用

 更新時(shí)間:2023年08月22日 15:45:58   作者:凱二七  
這篇文章主要介紹了python中圖片文件路徑格式如何使用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python中圖片文件路徑格式使用

在Python中,使用文件路徑的格式可以用來訪問圖片文件。

常見的文件路徑格式有兩種:

  • 相對(duì)路徑
  • 絕對(duì)路徑

相對(duì)路徑是相對(duì)于當(dāng)前工作目錄的路徑,例如:

'./images/picture.jpg'

絕對(duì)路徑是從根目錄開始的完整路徑,例如:

'/User/<username>/images/picture.jpg'

注意:

如果文件路徑中包含空格,請(qǐng)確保使用引號(hào)(單引號(hào)或雙引號(hào))將路徑括起來。

通過使用正確的文件路徑格式,可以使用 Python 的圖像處理庫(如 PIL,OpenCV 等)讀取和處理圖片文件。

利用python轉(zhuǎn)換圖片格式

常見的圖像任務(wù)通常需要把照片統(tǒng)一成相同的格式,所以此文章正是為了統(tǒng)一格式而生,常見的主要有cv2和PIL.Image的相關(guān)操作,照片格式是一串?dāng)?shù)字加上后綴名

工具一:cv2

pip install opencv-python

之后就可以

import cv2

1.1 導(dǎo)包

import os
import cv2
import sys
import numpy as np

1.2 路徑設(shè)置

path = r"C:\\Users\\86775\\test_change\\"
path2 = r"C:\\Users\\86775\\test_change2\\"

1.3 改格式重新傳

1.3.1 用os.path.splitext()來判斷

images = os.listdir(path)
for i in os.listdir(path):
    print(os.path.splitext(i))  # ('34474006827920603', '.png')
    if os.path.splitext(i)[1] == ".jpeg":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".jpeg", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif os.path.splitext(i)[1] == ".png":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".png", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif os.path.splitext(i)[1] == ".JPG":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".JPG", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif os.path.splitext(i)[1] == ".PNG":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".PNG", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif os.path.splitext(i)[1] == ".jpg":
        img = cv2.imread(path + i)
        # print(img)
        cv2.imwrite(path2 + i, img)

1.3.2 用split()來判斷

images = os.listdir(path)
for i in os.listdir(path):
    print(i.split("."))
    if i.split(".")[1] == "jpeg":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".jpeg", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif i.split(".")[1] == "png":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".png", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif i.split(".")[1] == "JPG":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".JPG", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif i.split(".")[1] == "PNG":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".PNG", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)
    elif i.split(".")[1] == "jpg":
        img = cv2.imread(path + i)
        # print(img)
        cv2.imwrite(path2 + i, img)

1.4 查看結(jié)果

工具二:pillow

pip install pillow

之后就可以

from PIL import Image

2.1 導(dǎo)包

import os
from PIL import Image

2.2 路徑設(shè)置

path = r"C:\\Users\\86775\\test_change\\"
path2 = r"C:\\Users\\86775\\test_change2\\"

2.3 寫格式修改函數(shù)

RGBA意思是紅色,綠色,藍(lán)色,Alpha的色彩空間,Alpha指透明度,而JPG不支持透明度。

所以其他格式不轉(zhuǎn)jpg可以直接保存為.png格式,轉(zhuǎn)jpg就需要丟棄Alpha

img = img.convert(“RGB”),轉(zhuǎn)換成RGB,保證和jpg格式的通道數(shù)相同,也算一種圖片類型變換的防報(bào)錯(cuò)機(jī)制

def jpeg2jpg(path_in, path_out):
    img = Image.open(path_in)
    img = img.convert("RGB")
    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)
def png2jpg(path_in, path_out):
    img = Image.open(path_in)
    img = img.convert("RGB")
    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)
def JPG2jpg(path_in, path_out):
    img = Image.open(path_in)
    img = img.convert("RGB")
    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)
def PNG2jpg(path_in, path_out):
    img = Image.open(path_in)
    img = img.convert("RGB")
    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)
def jpg2jpg(path_in, path_out):
    img = Image.open(path_in)
    img = img.convert("RGB")
    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)

2.4 開始轉(zhuǎn)換

images = os.listdir(path1)
for i in images:
    print(i)
    if os.path.splitext(i)[1] == ".jpeg":
        source = path1 + "\\" + str(i.split(".")[0]) + ".jpeg"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        jpeg2jpg(source, target)
    elif os.path.splitext(i)[1] == ".png":
        source = path1 + "\\" + str(i.split(".")[0]) + ".png"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        png2jpg(source, target)
    elif os.path.splitext(i)[1] == ".JPG":
        source = path1 + "\\" + str(i.split(".")[0]) + ".JPG"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        JPG2jpg(source, target)
    elif os.path.splitext(i)[1] == ".PNG":
        source = path1 + "\\" + str(i.split(".")[0]) + ".PNG"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        PNG2jpg(source, target)
    elif os.path.splitext(i)[1] == ".jpg":
        source = path1 + "\\" + str(i.split(".")[0]) + ".jpg"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        jpg2jpg(source, target)

2.5 查看結(jié)果

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django REST framework內(nèi)置路由用法

    Django REST framework內(nèi)置路由用法

    這篇文章主要介紹了Django REST framework內(nèi)置路由用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python判斷字符串是否包含子字符串的方法

    python判斷字符串是否包含子字符串的方法

    這篇文章主要介紹了python判斷字符串是否包含子字符串的方法,實(shí)例分析了Python中的in與find方法來實(shí)現(xiàn)這一功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • python 讀文件,然后轉(zhuǎn)化為矩陣的實(shí)例

    python 讀文件,然后轉(zhuǎn)化為矩陣的實(shí)例

    下面小編就為大家分享一篇python 讀文件,然后轉(zhuǎn)化為矩陣的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python實(shí)現(xiàn)輸入的數(shù)據(jù)在地圖上生成熱力圖效果

    python實(shí)現(xiàn)輸入的數(shù)據(jù)在地圖上生成熱力圖效果

    今天小編就為大家分享一篇python實(shí)現(xiàn)輸入的數(shù)據(jù)在地圖上生成熱力圖效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python使用PIL庫實(shí)現(xiàn)驗(yàn)證碼圖片的方法

    Python使用PIL庫實(shí)現(xiàn)驗(yàn)證碼圖片的方法

    這篇文章主要介紹了Python使用PIL庫實(shí)現(xiàn)驗(yàn)證碼圖片的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python基于PIL庫生成驗(yàn)證碼圖片的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2016-03-03
  • python 設(shè)置輸出圖像的像素大小方法

    python 設(shè)置輸出圖像的像素大小方法

    今天小編就為大家分享一篇python 設(shè)置輸出圖像的像素大小方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python3中bytes數(shù)據(jù)類型的具體使用

    python3中bytes數(shù)據(jù)類型的具體使用

    bytes類型是python3引入的,本文就來介紹一下python3中bytes數(shù)據(jù)類型的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Python: 傳遞列表副本方式

    Python: 傳遞列表副本方式

    今天小編就為大家分享一篇Python: 傳遞列表副本方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python使用Nocalhost并開啟debug調(diào)試的方法

    Python使用Nocalhost并開啟debug調(diào)試的方法

    Nocalhost是一種開發(fā)者工具,支持針對(duì)Kubernetes應(yīng)用程序進(jìn)行調(diào)試和部署,這篇文章主要介紹了Python怎么使用Nocalhost并開啟debug,需要的朋友可以參考下
    2023-04-04
  • pandas 獲取季度,月度,年度首尾日期的方法

    pandas 獲取季度,月度,年度首尾日期的方法

    下面小編就為大家分享一篇pandas 獲取季度,月度,年度首尾日期的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評(píng)論