99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀利用Python自帶PIL庫擴展圖片大小給圖片加文字描述的方法示例
利用Python自帶PIL庫擴展圖片大小給圖片加文字描述的方法示例
2017-09-24
收藏

利用Python自帶PIL庫擴展圖片大小給圖片加文字描述的方法示例

最近的一個項目中需要在圖片上添加文字,使用了OpenCV,結(jié)果發(fā)現(xiàn)利用opencv給圖像添加文字有局限??衫玫淖煮w類型比較少,需要安裝Freetype擴展,比較復雜。而且不能用putText函數(shù)輸出中文,否則就會出現(xiàn)亂碼的情況。只好選擇使用Python PIL函數(shù)庫對照片進行處理,利用Python自帶的PIL庫擴展圖片大小給圖片加上文字描述,大多都是庫函數(shù)調(diào)用,只是給定圖片寬度后計算文字所需行數(shù)的代碼需要寫。 代碼比較丑,but it works.

代碼示例
#!/usr/bin/env python3
 
from PIL import Image, ImageDraw, ImageFont
import math
 
text="盡管曾作為皇家獵場而存在,意大利大帕拉迪索國家公園一直保留著其野性的一面。畫面里的赤狐靜靜地匍匐在秋草叢中等待時機,它的身軀與自然融為一體。所有狐貍都是機會主義者,生活在大帕拉迪索的赤狐也不例外;如果有可能,無論是魚類還是野兔,即便是人類野餐留下的殘羹冷炙,它們也不介意吃個一干二凈。"
 
 
def make_text_image(width, white, text, save_path, mode = "rgb"):
 """
 生成一個文字圖形, white=1,表示白底黑字,否則為黑底白字
 """
 
 # 字體可能要改
 # linux查看支持的漢字字體 # fc-list :lang=zh
 ft = ImageFont.truetype("DroidSansFallbackFull.ttf", 15)
 w, h = ft.getsize(text)
 
 # 計算要幾行
 lines = math.ceil(w / width) + 1
 height = h * lines
 
 # 一個漢字的寬度
 one_zh_width, h = ft.getsize("中")
 
 if len(mode) == 1: # L, 1
  background = (255)
  color = (0)
 if len(mode) == 3: # RGB
  background = (255, 255, 255)
  color = (0,0,0)
 if len(mode) == 4: # RGBA, CMYK
  background = (255, 255, 255, 255)
  color = (0,0,0,0)
 
 newImage = Image.new(mode, (width, height), background if white else color)
 draw = ImageDraw.Draw(newImage)
 
 # 分割行
 text = text + " " #處理最后少一個字問題
 text_list = []
 start = 0
 end = len(text) - 1
 while start < end:
  for n in range(end):
   try_text = text[start:start+n]
   w,h = ft.getsize(try_text)
   if w + 2*one_zh_width > width:
    break
  text_list.append(try_text[0:-1])
  start = start + n - 1;
 
 # print(text_list)
 
 i = 0
 for t in text_list:
  draw.text((one_zh_width, i * h), t, color if white else background, font=ft)
  i = i + 1
 
 newImage.save(save_path);
 
 
def resize_canvas(org_image="aa.jpg", add_image="222.jpg", new_image_path="save2.jpg"):
 
 org_im = Image.open(org_image)
 org_width, org_height = org_im.size
 
 mode = org_im.mode
 
 make_text_image(org_width, 0, text, "222.jpg", mode)
 
 add_im = Image.open(add_image)
 add_width, add_height = add_im.size
 
 mode = org_im.mode
 
 newImage = Image.new(mode, (org_width, org_height + add_height))
 
 newImage.paste(org_im, (0, 0, org_width, org_height))
 newImage.paste(add_im, (0, org_height, add_width, add_height + org_height))
 newImage.save(new_image_path)
 
resize_canvas()

原圖

改之后的圖 


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }