python生成指定尺寸縮略圖的示例
python生成指定尺寸的縮略圖
def MakeThumb(path, sizes=(75, 32, 16)):
"""
縮略圖生成程序 by Neil Chen
sizes 參數(shù)傳遞要生成的尺寸,可以生成多種尺寸
"""
base, ext = os.path.splitext(path)
try:
im = Image.open(path)
except IOError:
return
mode = im.mode
if mode not in ('L', 'RGB'):
if mode == 'RGBA':
# 透明圖片需要加白色底
alpha = im.split()[3]
bgmask = alpha.point(lambda x: 255-x)
im = im.convert('RGB')
# paste(color, box, mask)
im.paste((255,255,255), None, bgmask)
else:
im = im.convert('RGB')
width, height = im.size
if width == height:
region = im
else:
if width > height:
delta = (width - height)/2
box = (delta, 0, delta+height, height)
else:
delta = (height - width)/2
box = (0, delta, width, delta+width)
region = im.crop(box)
for size in sizes:
filename = base + "_" + "%sx%s" % (str(size), str(size)) + ".jpg"
thumb = region.resize((size,size), Image.ANTIALIAS)
thumb.save(filename, quality=100) # 默認(rèn) JPEG 保存質(zhì)量是 75, 不太清楚??蛇x值(0~100)
相關(guān)文章
PyQt通過動畫實(shí)現(xiàn)平滑滾動的QScrollArea
這篇文章主要為大家詳細(xì)介紹了PyQt如何使用Qt的動畫框架 QPropertyAnimation來實(shí)現(xiàn)平滑滾動的QScrollArea,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以學(xué)習(xí)一下2023-01-01python實(shí)現(xiàn)簡單的購物程序代碼實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)簡單的購物程序代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03使用Python的開發(fā)框架Brownie部署以太坊智能合約
在本文中,我們將使用Python部署智能合約。這篇文章可能是您走向智能合約和區(qū)塊鏈開發(fā)的橋梁!2021-05-05python導(dǎo)出hive數(shù)據(jù)表的schema實(shí)例代碼
這篇文章主要介紹了python導(dǎo)出hive數(shù)據(jù)表的schema實(shí)例代碼,小編覺得還是挺不錯的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python collections中的雙向隊(duì)列deque簡單介紹詳解
這篇文章主要介紹了Python collections中的雙向隊(duì)列deque簡單介紹詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11windows下python連接oracle數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了windows下python連接oracle數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06