用PIL處理圖像比用OPENCV更好用。opencv通常是用來采集視頻圖像和圖片的。
為思茅等地區用戶提供了全套網頁設計制作服務,及思茅網站建設行業解決方案。主營業務為成都網站設計、成都網站建設、外貿網站建設、思茅網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
首先需要安裝 PIL 庫
然后 from PIL import Image
im = Image.open(pash)
im.thumbnail((new_width, new_hight))
im.save(path)
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?#正確獲取小數的方式
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)
#這里的參數可以這么認為:從某圖的(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
#保存的圖片質量
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)
import zipfile
# 傳入壓縮文件zfile.zip獲取相關信息
zip_file = zipfile.ZipFile('zfile.zip')
# 獲取壓縮文件中的內容
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()方法,返回一個關 于特定文件的 ZipInfo 對象。ZipInfo 對象有自己的屬性,諸如表示字節數的 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 的內容將被解壓縮到 C:\。 或者, 你可以向 extractall()傳遞的一個文件夾名稱,它將文件解壓縮到那個文件夾,而不是當前工作 目錄。如果傳遞給 extractall()方法的文件夾不存在,它會被創建。例如,如果你用 exampleZip.extractall('C:\ delicious')取代?處的調用,代碼就會從 example.zip 中解壓 縮文件,放到新創建的 C:\delicious 文件夾中。
ZipFile 對象的 extract()方法從 ZIP 文件中解壓縮單個文件。
創建和添加到 ZIP 文件
要創建你自己的壓縮 ZIP 文件,必須以“寫模式”打開 ZipFile 對象,即傳入'w' 作為第二個參數(這類似于向 open()函數傳入'w',以寫模式打開一個文本文件)。
如果向 ZipFile 對象的 write()方法傳入一個路徑,Python 就會壓縮該路徑所指 的文件,將它加到 ZIP 文件中。write()方法的第一個參數是一個字符串,代表要添 加的文件名。第二個參數是“壓縮類型”參數,它告訴計算機使用怎樣的算法來壓 縮文件。可以總是將這個值設置為 zipfile.ZIP_DEFLATED(這指定了 deflate 壓縮 算法,它對各種類型的數據都很有效)。
import zipfile
zip_file = zipfile.ZipFile('new.zip','w')
# 把zfile整個目錄下所有內容,壓縮為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()
這段代碼將創建一個新的 ZIP 文件,名為 new.zip,它包含 spam.txt 壓縮后的內容。
要記住,就像寫入文件一樣,寫模式將擦除 ZIP 文件中所有原有的內容。如果 只是希望將文件添加到原有的 ZIP 文件中,就要向 zipfile.ZipFile()傳入'a'作為第二 個參數,以追加模式打開 ZIP 文件。
當前標題:python圖像壓縮函數 圖像壓縮 python
文章轉載:http://m.kartarina.com/article2/dodesoc.html
成都網站建設公司_創新互聯,為您提供網站維護、網站收錄、網站改版、品牌網站設計、電子商務、移動網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯