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

Python如何將jpg圖像修改大小并轉(zhuǎn)換為png

 更新時(shí)間:2023年09月11日 15:21:54   作者:回爐重造P  
這篇文章主要介紹了Python如何將jpg圖像修改大小并轉(zhuǎn)換為png問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Python將jpg圖像修改大小并轉(zhuǎn)換為png

簡(jiǎn)單利用pillow庫(kù)實(shí)現(xiàn)的大小與格式轉(zhuǎn)換的腳本。

實(shí)現(xiàn)

import os
import PIL.Image as Image
def changeJpgToPng(w, h, path):
    # 修改圖像大小
    image = Image.open(path)
    image = image.resize((w, h), Image.ANTIALIAS)
    # 將jpg轉(zhuǎn)換為png
    png_name = str(path)[0:-len('.jpg')] + '_min.png'
    image.save(png_name)
    print(png_name)
    # 去白底
    image = image.convert('RGBA')
    img_w, img_h = image.size
    color_white = (255, 255, 255, 255)
    for j in range(img_h):
        for i in range(img_w):
            pos = (i, j)
            color_now = image.getpixel(pos)
            if color_now == color_white:
                # 透明度置為0
                color_now = color_now[:-1] + (0,)
                image.putpixel(pos, color_now)
    image.save(png_name)
if __name__ == '__main__':
    t_w = 300
    t_h = 150
    dic = os.listdir('image')
    for d in dic:
        if d.count('.jpg') > 0:
            changeJpgToPng(t_w, t_h, 'image/' + d)

參考實(shí)現(xiàn)了去白底的功能,簡(jiǎn)單循環(huán)對(duì)比。

結(jié)構(gòu)

Python批量實(shí)現(xiàn)jpg圖片修改大小并轉(zhuǎn)換為png(圖像通道:RGB,適配KITTI數(shù)據(jù)集)

# python:批量實(shí)現(xiàn)jpg圖片修改大小并轉(zhuǎn)換為png
# author:zhou jinxing
import os
import PIL.Image as Image
def changeJpgToPng(w, h,srcPath,dstPath):
    # 修改圖像大小
    image = Image.open(srcPath)
    image = image.resize((w, h), Image.ANTIALIAS)
    # 將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')
    img_w, img_h = image.size
    #color_white = (255, 255, 255, 255)
    color_white = (255, 255, 255)
    for j in range(img_h):
        for i in range(img_w):
            pos = (i, j)
            color_now = image.getpixel(pos)
            if color_now == color_white:
                # 透明度置為0
                color_now = color_now[:-1] + (0,)
                image.putpixel(pos, color_now)
    image.save(png_name)
if __name__ == '__main__':
    t_w = 1242
    t_h = 375
    #srcPath = '/workspace/tlt-experiments/data_fire/image_2_jpg/'
    #dstPath = '/workspace/tlt-experiments/data_fire/image_2_png/'
    srcPath = '/workspace/tlt-experiments/data_fire/012/training/image_2/'
    dstPath = '/workspace/tlt-experiments/data_fire/012/training/image_2_png/'    
#    filename_list = os.listdir('/workspace/tlt-experiments/data_fire/image_2_jpg')
#    for d in dic:
#        if d.count('.jpg') > 0:
#            changeJpgToPng(t_w, t_h, 'image/' + d)
    print("開(kāi)始轉(zhuǎn)換...")
    filename_list = os.listdir('/workspace/tlt-experiments/data_fire/012/training/image_2')
    for d in filename_list:
        if d.count('.png') > 0:
            changeJpgToPng(t_w, t_h,srcPath + d,dstPath + d)
        pass
    print("完成了...")

總結(jié)

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

相關(guān)文章

  • Python Requests庫(kù)基本用法示例

    Python Requests庫(kù)基本用法示例

    這篇文章主要介紹了Python Requests庫(kù)基本用法,結(jié)合實(shí)例形式總結(jié)分析了Python Requests庫(kù)安裝、請(qǐng)求發(fā)送與響應(yīng)、文件下載、重定向等相關(guān)操作技巧及注意事項(xiàng),需要的朋友可以參考下
    2018-08-08
  • 使用Python計(jì)算TRC20地址

    使用Python計(jì)算TRC20地址

    這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)計(jì)算TRC20地址,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-02-02
  • python字典DICT類型合并詳解

    python字典DICT類型合并詳解

    這篇文章主要為大家詳細(xì)介紹了python字典DICT類型合并,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • python中pygame安裝過(guò)程(超級(jí)詳細(xì))

    python中pygame安裝過(guò)程(超級(jí)詳細(xì))

    這篇文章主要介紹了python中pygame安裝過(guò)程(超級(jí)詳細(xì)),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 如何測(cè)試Python網(wǎng)站的訪問(wèn)速度,并且優(yōu)化Python網(wǎng)站的性能

    如何測(cè)試Python網(wǎng)站的訪問(wèn)速度,并且優(yōu)化Python網(wǎng)站的性能

    本文使用網(wǎng)絡(luò)工具和Python測(cè)速庫(kù)進(jìn)行測(cè)試Python網(wǎng)站的訪問(wèn)速度,通過(guò)優(yōu)化代碼性能和優(yōu)化服務(wù)器性能以及優(yōu)化數(shù)據(jù)庫(kù)性能等有針對(duì)性地優(yōu)化Python網(wǎng)站的性能
    2024-01-01
  • 解決python圖像處理圖像賦值后變?yōu)榘咨膯?wèn)題

    解決python圖像處理圖像賦值后變?yōu)榘咨膯?wèn)題

    這篇文章主要介紹了解決python圖像處理圖像賦值后變?yōu)榘咨膯?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python中實(shí)現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法(示例詳解)

    Python中實(shí)現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法(示例詳解)

    在 Python 中,可以采用 pandas、pyexcel、win32com 和 xls2xlsx 這四個(gè)模塊,實(shí)現(xiàn) xls 轉(zhuǎn) xlsx 格式,本文以 Excel 示例文件test_Excel.xls 為例結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • 詳解Python 切片語(yǔ)法

    詳解Python 切片語(yǔ)法

    Python的切片是特別常用的功能,主要用于對(duì)列表的元素取值。這篇文章主要介紹了詳解Python 切片語(yǔ)法,需要的朋友可以參考下
    2019-06-06
  • django頁(yè)面跳轉(zhuǎn)問(wèn)題及注意事項(xiàng)

    django頁(yè)面跳轉(zhuǎn)問(wèn)題及注意事項(xiàng)

    這篇文章主要介紹了django頁(yè)面跳轉(zhuǎn)問(wèn)題及注意事項(xiàng),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • pytorch中獲取模型input/output shape實(shí)例

    pytorch中獲取模型input/output shape實(shí)例

    今天小編就為大家分享一篇pytorch中獲取模型input/output shape實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12

最新評(píng)論