最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python之如何調(diào)整圖片的文件大小

 更新時(shí)間:2023年03月25日 09:06:48   作者:XerCis  
這篇文章主要介紹了Python之如何調(diào)整圖片的文件大小問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。

問題描述

Python調(diào)整圖片文件的占用空間大小,而不是分辨率

1.jpg

1.jpg

圖片大小為 8KB

 

減小文件大小

使用 PIL 模塊

pip install Pillow

1. 減小圖片質(zhì)量

代碼

import os
from PIL import Image


def compress_under_size(imagefile, targetfile, targetsize):
    """壓縮圖片尺寸直到某一尺寸

    :param imagefile: 原圖路徑
    :param targetfile: 保存圖片路徑
    :param targetsize: 目標(biāo)大小,單位byte
    """
    currentsize = os.path.getsize(imagefile)
    for quality in range(99, 0, -1):  # 壓縮質(zhì)量遞減
        if currentsize > targetsize:
            image = Image.open(imagefile)
            image.save(targetfile, optimize=True, quality=quality)
            currentsize = os.path.getsize(targetfile)


if __name__ == '__main__':
    imagefile = '1.jpg'  # 圖片路徑
    targetfile = 'result.jpg'  # 目標(biāo)圖片路徑
    targetsize = 2 * 1024  # 目標(biāo)圖片大小
    compress_under_size(imagefile, targetfile, targetsize)  # 將圖片壓縮到2KB

效果

注意!無法實(shí)現(xiàn)圖片無限壓縮,若文件太小,辨識(shí)度也會(huì)大大降低

2. 減小圖片尺寸

import os
from PIL import Image


def image_compress(filename, savename, targetsize):
    """圖像壓縮

    :param filename: 原圖路徑
    :param savename: 保存圖片路徑
    :param targetsize: 目標(biāo)大小,單位為byte
    """
    image = Image.open(filename)
    size = os.path.getsize(filename)
    if size <= targetsize:
        return
    width, height = image.size
    num = (targetsize / size) ** 0.5
    width, height = round(width * num), round(height * num)
    image.resize((width, height)).save(savename)


if __name__ == '__main__':
    filename = '1.jpg'
    savename = 'result.jpg'
    targetsize = 2 * 1024
    image_compress(filename, savename, targetsize)

效果

增加文件大小

Windows

通過 subprocess 模塊調(diào)用系統(tǒng)命令 fsutil file createnew filename filesize 創(chuàng)建指定大小的文件

再用 copy/b 命令合并數(shù)據(jù)到圖片上

import os
import time
import subprocess

imagefile = '1.jpg'  # 圖片路徑
targetfile = 'result.jpg'  # 目標(biāo)圖片路徑
targetsize = 10 * 1024 * 1024  # 目標(biāo)圖片大小

tempfile = str(int(time.time()))  # 臨時(shí)文件路徑
tempsize = str(targetsize - os.path.getsize(imagefile))  # 臨時(shí)文件大小
subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize])  # 創(chuàng)建臨時(shí)文件
subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True)  # 合并生成新圖片
os.remove(tempfile)

Linux

通過 subprocess 模塊調(diào)用系統(tǒng)命令 fallocate -l filesize filename 創(chuàng)建指定大小的文件

再用 cat > 命令合并數(shù)據(jù)到圖片上

import os
import time
import subprocess

imagefile = '1.jpg'  # 圖片路徑
targetfile = 'result.jpg'  # 目標(biāo)圖片路徑
targetsize = 10 * 1024 * 1024  # 目標(biāo)圖片大小

tempfile = str(int(time.time()))  # 臨時(shí)文件路徑
tempsize = str(targetsize - os.path.getsize(imagefile))  # 臨時(shí)文件大小
subprocess.run(['fallocate', '-l', tempsize, tempfile])  # 創(chuàng)建臨時(shí)文件
subprocess.run('cat {} {} > {}'.format(imagefile, tempfile, targetfile), shell=True)  # 合并生成新圖片
os.remove(tempfile)

效果

圖片的分辨率沒變

封裝

import os
import time
import platform
import subprocess
from PIL import Image


def resize_picture_filesize(imagefile, targetfile, targetsize):
    """調(diào)整圖片文件大小

    :param imagefile: 原圖路徑
    :param targetfile: 保存圖片路徑
    :param targetsize: 目標(biāo)文件大小,單位byte
    """
    currentsize = os.path.getsize(imagefile)  # 原圖文件大小

    if currentsize > targetsize:  # 需要縮小
        for quality in range(99, 0, -1):  # 壓縮質(zhì)量遞減
            if currentsize > targetsize:
                image = Image.open(imagefile)
                image.save(targetfile, optimize=True, quality=quality)
                currentsize = os.path.getsize(targetfile)
    else:  # 需要放大
        system = platform.system()
        tempfile = str(int(time.time()))  # 臨時(shí)文件路徑
        tempsize = str(targetsize - os.path.getsize(imagefile))  # 臨時(shí)文件大小

        if system == 'Windows':
            subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize])  # 創(chuàng)建臨時(shí)文件
            subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True)  # 合并生成新圖片
        elif system == 'Linux':
            subprocess.run(['fallocate', '-l', tempsize, tempfile])  # 創(chuàng)建臨時(shí)文件
            subprocess.run('cat {} {} > {}'.format(imagefile, tempfile, targetfile), shell=True)  # 合并生成新圖片
        os.remove(tempfile)


if __name__ == '__main__':
    imagefile = '1.jpg'  # 8KB的圖片
    resize_picture_filesize(imagefile, 'reduce.jpg', 2 * 1024)  # 縮小到2KB
    resize_picture_filesize(imagefile, 'increase.jpg', 800 * 1024)  # 放大到800KB

總結(jié)

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

相關(guān)文章

  • python基礎(chǔ)教程項(xiàng)目四之新聞聚合

    python基礎(chǔ)教程項(xiàng)目四之新聞聚合

    這篇文章主要為大家詳細(xì)介紹了python基礎(chǔ)教程項(xiàng)目四之新聞聚合,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python中uuid模塊的應(yīng)用實(shí)例詳解

    Python中uuid模塊的應(yīng)用實(shí)例詳解

    這篇文章主要介紹了Python中uuid模塊應(yīng)用的相關(guān)資料,該模塊提供了多種方法生成UUID,包括uuid1()、uuid3()、uuid4()和uuid5(),并解釋了UUID的格式,UUID在數(shù)據(jù)庫(kù)、分布式系統(tǒng)和網(wǎng)絡(luò)協(xié)議中廣泛應(yīng)用,是處理唯一標(biāo)識(shí)符的有力工具,需要的朋友可以參考下
    2024-11-11
  • Selenium鼠標(biāo)與鍵盤事件常用操作方法示例

    Selenium鼠標(biāo)與鍵盤事件常用操作方法示例

    這篇文章主要介紹了Selenium鼠標(biāo)與鍵盤事件常用操作方法,結(jié)合實(shí)例形式分析了Selenium鼠標(biāo)事件與鍵盤事件常見方法與相關(guān)使用技巧,需要的朋友可以參考下
    2018-08-08
  • python?opencv的imread方法無法讀取圖片問題

    python?opencv的imread方法無法讀取圖片問題

    這篇文章主要介紹了python?opencv的imread方法無法讀取圖片問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python實(shí)現(xiàn)五子棋聯(lián)機(jī)對(duì)戰(zhàn)小游戲

    Python實(shí)現(xiàn)五子棋聯(lián)機(jī)對(duì)戰(zhàn)小游戲

    本文主要介紹了通過Python實(shí)現(xiàn)簡(jiǎn)單的支持聯(lián)機(jī)對(duì)戰(zhàn)的游戲——支持局域網(wǎng)聯(lián)機(jī)對(duì)戰(zhàn)的五子棋小游戲。廢話不多說,快來跟隨小編一起學(xué)習(xí)吧
    2021-12-12
  • Python實(shí)現(xiàn)遺傳算法(虛擬機(jī)中運(yùn)行)

    Python實(shí)現(xiàn)遺傳算法(虛擬機(jī)中運(yùn)行)

    遺傳算法(GA)是最早由美國(guó)Holland教授提出的一種基于自然界的“適者生存,優(yōu)勝劣汰”基本法則的智能搜索算法。本文主要介紹了如何通過Python實(shí)現(xiàn)遺傳算法,感興趣的同學(xué)可以看一看
    2021-11-11
  • TensorFlow-gpu和opencv安裝詳細(xì)教程

    TensorFlow-gpu和opencv安裝詳細(xì)教程

    這篇文章主要介紹了TensorFlow-gpu和opencv安裝過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Django實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    Django實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Django實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • python中如何使用xml.dom.minidom模塊讀取解析xml文件

    python中如何使用xml.dom.minidom模塊讀取解析xml文件

    xml.dom.minidom模塊應(yīng)該是內(nèi)置模塊不用下載安裝,本文給大家介紹python中如何使用xml.dom.minidom模塊讀取解析xml文件,感興趣的朋友一起看看吧
    2023-10-10
  • Python實(shí)現(xiàn)的rsa加密算法詳解

    Python實(shí)現(xiàn)的rsa加密算法詳解

    這篇文章主要介紹了Python實(shí)現(xiàn)的rsa加密算法,結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)rsa加密算法的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01

最新評(píng)論

潜山县| 来凤县| 武陟县| 太康县| 盐津县| 安化县| 泽州县| 土默特左旗| 陕西省| 华蓥市| 阜新| 浮梁县| 大新县| 南投县| 泊头市| 海伦市| 正定县| 隆尧县| 聂拉木县| 庄河市| 阿尔山市| 正镶白旗| 静海县| 乐都县| 庆阳市| 仙桃市| 伊宁县| 和平县| 漳州市| 房产| 微博| 青铜峡市| 黔江区| 景洪市| 普安县| 湖南省| 延川县| 银川市| 乳源| 和平县| 徐汇区|