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

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

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

問題描述

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: 目標大小,單位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'  # 目標圖片路徑
    targetsize = 2 * 1024  # 目標圖片大小
    compress_under_size(imagefile, targetfile, targetsize)  # 將圖片壓縮到2KB

效果

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

2. 減小圖片尺寸

import os
from PIL import Image


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

    :param filename: 原圖路徑
    :param savename: 保存圖片路徑
    :param targetsize: 目標大小,單位為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'  # 目標圖片路徑
targetsize = 10 * 1024 * 1024  # 目標圖片大小

tempfile = str(int(time.time()))  # 臨時文件路徑
tempsize = str(targetsize - os.path.getsize(imagefile))  # 臨時文件大小
subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize])  # 創(chuàng)建臨時文件
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'  # 目標圖片路徑
targetsize = 10 * 1024 * 1024  # 目標圖片大小

tempfile = str(int(time.time()))  # 臨時文件路徑
tempsize = str(targetsize - os.path.getsize(imagefile))  # 臨時文件大小
subprocess.run(['fallocate', '-l', tempsize, tempfile])  # 創(chuàng)建臨時文件
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: 目標文件大小,單位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()))  # 臨時文件路徑
        tempsize = str(targetsize - os.path.getsize(imagefile))  # 臨時文件大小

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

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

相關(guān)文章

  • 輕松掌握python設(shè)計模式之訪問者模式

    輕松掌握python設(shè)計模式之訪問者模式

    這篇文章主要幫助大家輕松掌握python設(shè)計模式之訪問者模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • python 基于AioHttp 異步抓取火星圖片

    python 基于AioHttp 異步抓取火星圖片

    這篇文章主要介紹了python 基于AioHttp 異步抓取火星圖片的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • Python之PyQt6對話框的實現(xiàn)

    Python之PyQt6對話框的實現(xiàn)

    這篇文章主要介紹了Python之PyQt6對話框的實現(xiàn),文章內(nèi)容詳細,簡單易懂,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2023-01-01
  • Python完成毫秒級搶淘寶大單功能

    Python完成毫秒級搶淘寶大單功能

    在本篇文章里小編給大家分享了關(guān)于Python完成毫秒級搶淘寶大單功能以及實例代碼,需要的朋友們參考下。
    2019-06-06
  • 對python 各種刪除文件失敗的處理方式分享

    對python 各種刪除文件失敗的處理方式分享

    下面小編就為大家分享一篇對python 各種刪除文件失敗的處理方式。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python實時獲取cmd的輸出

    Python實時獲取cmd的輸出

    本文給大家分享python實時獲取cmd的輸出,對python實時獲取輸出相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Python模塊對Redis數(shù)據(jù)庫的連接與使用講解

    Python模塊對Redis數(shù)據(jù)庫的連接與使用講解

    這篇文章主要介紹了Python模塊對Redis數(shù)據(jù)庫的連接與使用,通過實例代碼給大家介紹了Python連接Redis數(shù)據(jù)庫方法,Python使用連接池連接Redis數(shù)據(jù)庫方法,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • 使用Python編寫一個簡單的tic-tac-toe游戲的教程

    使用Python編寫一個簡單的tic-tac-toe游戲的教程

    這篇文章主要介紹了使用Python編寫一個簡單的tic-tac-toe游戲的教程,有利于Python初學(xué)者進行上手實踐,需要的朋友可以參考下
    2015-04-04
  • Python獲取svn版本信息

    Python獲取svn版本信息

    本文主要介紹了Python獲取svn版本信息,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 關(guān)于Python 位運算防坑指南

    關(guān)于Python 位運算防坑指南

    這篇文章主要介紹了關(guān)于Python 位運算防坑指南,小編將劇烈向大家說明并且列舉python及C#兩種語言,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09

最新評論