欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python圖像壓縮函數(shù) 圖像壓縮 python

python環(huán)境下用opencv壓縮圖片尺寸,用python語言引入poencv的方法,實現(xiàn)圖片的

用PIL處理圖像比用OPENCV更好用。opencv通常是用來采集視頻圖像和圖片的。

為思茅等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及思茅網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、思茅網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

python中image怎樣壓縮圖像

首先需要安裝 PIL 庫

然后 from PIL import Image

im = Image.open(pash)

im.thumbnail((new_width, new_hight))

im.save(path)

python中PLE調(diào)整圖片大小,等比例壓縮文件,怎么寫代碼

How do I read image data from a URL in Python?

importosimportImagefileName?='c:/py/jb51.jpg'fp?=open(fileName,'rb')im?=Image.open(fp)fp.close()x,y?=im.sizeifx 300or ?y ?300:???os.remove(fileName)

from PIL import Imageimport requestsimport numpy as npfrom StringIO import StringIOresponse = requests.get(url)img = np.array(Image.open(StringIO(response.content)))

from PIL import Imageimport urllib2

im = Image.open(urllib2.urlopen(url))

or if you use?requests:

from PIL import Imageimport requests

im = Image.open(requests.get(url, stream=True).raw)

[python] view plain copy

[html] view plain copy

#coding:utf-8

'''

python圖片處理

'''

import?Image?as?image

#等比例壓縮圖片

def?resizeImg(**args):

args_key?=?{'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}

arg?=?{}

for?key?in?args_key:

if?key?in?args:

arg[key]?=?args[key]

im?=?image.open(arg['ori_img'])

ori_w,ori_h?=?im.size

widthRatio?=?heightRatio?=?None

ratio?=?1

if?(ori_w?and?ori_w??arg['dst_w'])?or?(ori_h?and?ori_h??arg['dst_h']):

if?arg['dst_w']?and?ori_w??arg['dst_w']:

widthRatio?=?float(arg['dst_w'])?/?ori_w?#正確獲取小數(shù)的方式

if?arg['dst_h']?and?ori_h??arg['dst_h']:

heightRatio?=?float(arg['dst_h'])?/?ori_h

if?widthRatio?and?heightRatio:

if?widthRatio??heightRatio:

ratio?=?widthRatio

else:

ratio?=?heightRatio

if?widthRatio?and?not?heightRatio:

ratio?=?widthRatio

if?heightRatio?and?not?widthRatio:

ratio?=?heightRatio

newWidth?=?int(ori_w?*?ratio)

newHeight?=?int(ori_h?*?ratio)

else:

newWidth?=?ori_w

newHeight?=?ori_h

im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])

'''

image.ANTIALIAS還有如下值:

NEAREST:?use?nearest?neighbour

BILINEAR:?linear?interpolation?in?a?2x2?environment

BICUBIC:cubic?spline?interpolation?in?a?4x4?environment

ANTIALIAS:best?down-sizing?filter

'''

#裁剪壓縮圖片

def?clipResizeImg(**args):

args_key?=?{'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}

arg?=?{}

for?key?in?args_key:

if?key?in?args:

arg[key]?=?args[key]

im?=?image.open(arg['ori_img'])

ori_w,ori_h?=?im.size

dst_scale?=?float(arg['dst_h'])?/?arg['dst_w']?#目標高寬比

ori_scale?=?float(ori_h)?/?ori_w?#原高寬比

if?ori_scale?=?dst_scale:

#過高

width?=?ori_w

height?=?int(width*dst_scale)

x?=?0

y?=?(ori_h?-?height)?/?3

else:

#過寬

height?=?ori_h

width?=?int(height*dst_scale)

x?=?(ori_w?-?width)?/?2

y?=?0

#裁剪

box?=?(x,y,width+x,height+y)

#這里的參數(shù)可以這么認為:從某圖的(x,y)坐標開始截,截到(width+x,height+y)坐標

#所包圍的圖像,crop方法與php中的imagecopy方法大為不一樣

newIm?=?im.crop(box)

im?=?None

#壓縮

ratio?=?float(arg['dst_w'])?/?width

newWidth?=?int(width?*?ratio)

newHeight?=?int(height?*?ratio)

newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])

#水印(這里僅為圖片水印)

def?waterMark(**args):

args_key?=?{'ori_img':'','dst_img':'','mark_img':'','water_opt':''}

arg?=?{}

for?key?in?args_key:

if?key?in?args:

arg[key]?=?args[key]

im?=?image.open(arg['ori_img'])

ori_w,ori_h?=?im.size

mark_im?=?image.open(arg['mark_img'])

mark_w,mark_h?=?mark_im.size

option?={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h),

'rightlow':(ori_w-mark_w,ori_h-mark_h)

}

im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA'))

im.save(arg['dst_img'])

#Demon

#源圖片

ori_img?=?'D:/tt.jpg'

#水印標

mark_img?=?'D:/mark.png'

#水印位置(右下)

water_opt?=?'rightlow'

#目標圖片

dst_img?=?'D:/python_2.jpg'

#目標圖片大小

dst_w?=?94

dst_h?=?94

#保存的圖片質(zhì)量

save_q?=?35

#裁剪壓縮

clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q?=?save_q)

#等比例壓縮

#resizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)

#水印

#waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)

用python解壓圖片并打印代碼

import zipfile

# 傳入壓縮文件zfile.zip獲取相關(guān)信息

zip_file = zipfile.ZipFile('zfile.zip')

# 獲取壓縮文件中的內(nèi)容

f_content = zip_file.namelist()

# 壓縮前的大小

f_size = zip_file.getinfo('zfile/a.txt').file_size

# 壓縮后的大小

c_size = zip_file.getinfo('zfile/a.txt').compress_size

ZipFile 對象有一個 namelist()方法,返回 ZIP 文件中包含的所有文件和文件夾 的字符串的列表。這些字符串可以傳遞給 ZipFile 對象的 getinfo()方法,返回一個關(guān) 于特定文件的 ZipInfo 對象。ZipInfo 對象有自己的屬性,諸如表示字節(jié)數(shù)的 file_size 和 compress_size,它們分別表示原來文件大小和壓縮后文件大小。ZipFile 對象表示 整個歸檔文件,而 ZipInfo 對象則保存該歸檔文件中每個文件的有用信息。

從 ZIP 文件中解壓縮

ZipFile 對象的 extractall()方法從 ZIP 文件中解壓縮所有文件和文件夾,放到當 前工作目錄中。

import zipfile

zip_file = zipfile.ZipFile('zfile.zip')

# 解壓

zip_extract = zip_file.extractall()

zip_extract.close()

運行這段代碼后, example.zip 的內(nèi)容將被解壓縮到 C:\。 或者, 你可以向 extractall()傳遞的一個文件夾名稱,它將文件解壓縮到那個文件夾,而不是當前工作 目錄。如果傳遞給 extractall()方法的文件夾不存在,它會被創(chuàng)建。例如,如果你用 exampleZip.extractall('C:\ delicious')取代?處的調(diào)用,代碼就會從 example.zip 中解壓 縮文件,放到新創(chuàng)建的 C:\delicious 文件夾中。

ZipFile 對象的 extract()方法從 ZIP 文件中解壓縮單個文件。

創(chuàng)建和添加到 ZIP 文件

要創(chuàng)建你自己的壓縮 ZIP 文件,必須以“寫模式”打開 ZipFile 對象,即傳入'w' 作為第二個參數(shù)(這類似于向 open()函數(shù)傳入'w',以寫模式打開一個文本文件)。

如果向 ZipFile 對象的 write()方法傳入一個路徑,Python 就會壓縮該路徑所指 的文件,將它加到 ZIP 文件中。write()方法的第一個參數(shù)是一個字符串,代表要添 加的文件名。第二個參數(shù)是“壓縮類型”參數(shù),它告訴計算機使用怎樣的算法來壓 縮文件。可以總是將這個值設(shè)置為 zipfile.ZIP_DEFLATED(這指定了 deflate 壓縮 算法,它對各種類型的數(shù)據(jù)都很有效)。

import zipfile

zip_file = zipfile.ZipFile('new.zip','w')

# 把zfile整個目錄下所有內(nèi)容,壓縮為new.zip文件

zip_file.write('zfile',compress_type=zipfile.ZIP_DEFLATED)

# 把c.txt文件壓縮成一個壓縮文件

# zip_file.write('c.txt',compress_type=zipfile.ZIP_DEFLATED)

zip_file.close()

這段代碼將創(chuàng)建一個新的 ZIP 文件,名為 new.zip,它包含 spam.txt 壓縮后的內(nèi)容。

要記住,就像寫入文件一樣,寫模式將擦除 ZIP 文件中所有原有的內(nèi)容。如果 只是希望將文件添加到原有的 ZIP 文件中,就要向 zipfile.ZipFile()傳入'a'作為第二 個參數(shù),以追加模式打開 ZIP 文件。

當前標題:python圖像壓縮函數(shù) 圖像壓縮 python
文章轉(zhuǎn)載:http://chinadenli.net/article2/dodesoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護網(wǎng)站收錄、網(wǎng)站改版、品牌網(wǎng)站設(shè)計、電子商務(wù)移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司