国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python使用Image處理圖片常用技巧分析

2019-11-25 17:20:36
字體:
供稿:網(wǎng)友

本文實例講述了python使用Image處理圖片常用技巧。分享給大家供大家參考。具體分析如下:

使用python來處理圖片是非常方便的,下面提供一小段python處理圖片的代碼,需要安裝圖像處理工具包PIL(Python Image Library)。

#coding=utf-8import Imageimport urllib2import StringIOimport os#改變圖片大小def resize_img(img_path):  try:    img = Image.open(img_path)    (width,height) = img.size    new_width = 200    new_height = height * new_width / width    out = img.resize((new_width,new_height),Image.ANTIALIAS)    ext = os.path.splitext(img_path)[1]    new_file_name = '%s%s' %('small',ext)    out.save(new_file_name,quality=95)  except Exception,e:    print e#改變圖片類型def change_img_type(img_path):  try:    img = Image.open(img_path)    img.save('new_type.png')  except Exception,e:    print e#處理遠(yuǎn)程圖片def handle_remote_img(img_url):  try:    request = urllib2.Request(img_url)    img_data = urllib2.urlopen(request).read()    img_buffer = StringIO.StringIO(img_data)    img = Image.open(img_buffer)    img.save('remote.jpg')    (width,height) = img.size    out = img.resize((200,height * 200 / width),Image.ANTIALIAS)    out.save('remote_small.jpg')  except Exception,e:    print eif __name__ == '__main__':  img_path = 'test.jpg'  resize_img(img_path)  change_img_type(img_path)  img_url = 'http://img.hb.aicdn.com/042f8a4a70239f724ff7b9fa0fc8edf18658f41022ada-WcItWE_fw554'  handle_remote_img(img_url)

可能會遇到的問題

ImportError: No module named Image

解決辦法:安裝Python Imaging Library(PIL)

復(fù)制代碼 代碼如下:
sudo easy_install PIL

安裝PIL出現(xiàn):
― JPEG support not available

― ZLIB (PNG/ZIP) support not available

― FREETYPE2 support not available

操作jpeg圖片和png圖片出現(xiàn):

IOError: decoder jpeg not available 和 IOError: encoder zip not available

解決辦法:
(1) 刪除已經(jīng)安裝的PIL

復(fù)制代碼 代碼如下:
sudo rm -rf /usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-x86_64.egg/

(2) 安裝相關(guān)庫
復(fù)制代碼 代碼如下:
sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/

(3) 重新安裝PIL
復(fù)制代碼 代碼如下:
sudo easy_install PIL

終端出現(xiàn):

― JPEG support available
― ZLIB (PNG/ZIP) support available
― FREETYPE2 support available

現(xiàn)在試試,已經(jīng)ok了

希望本文所述對大家的Python程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 象州县| 绩溪县| 佛学| 石河子市| 如东县| 绩溪县| 阜南县| 阿瓦提县| 错那县| 镇康县| 绥化市| 曲水县| 方山县| 醴陵市| 靖西县| 奎屯市| 盈江县| 河北省| 尚义县| 五寨县| 达日县| 彰化市| 天长市| 宜春市| 监利县| 普兰店市| 葫芦岛市| 潞西市| 平山县| 平原县| 马关县| 通海县| 绥滨县| 嘉峪关市| 巨野县| 修水县| 稷山县| 临海市| 阆中市| 张家口市| 长治县|