<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用 Python 為老師送上節(jié)日的祝福

          共 6497字,需瀏覽 13分鐘

           ·

          2020-09-12 22:30

          文 |?野客

          來源:Python 技術(shù)「ID: pythonall」


          提到老師,大家首先想到的可能就是在學校中教我們文化課的人,除此之外,在生活或工作中給予我們指導(dǎo)及幫助的人也可稱之為老師,今天是教師節(jié),本文我們就使用 Python 來為所有的老師送上節(jié)日的祝福。

          向日葵

          向日葵的花語包括:信念、光輝、忠誠等,很適合老師,我們先畫一個簡單的向日葵。

          繪制效果如下:

          實現(xiàn)代碼如下:

          turtle.setup(600,?600,?80,?80)
          turtle.pencolor("yellow")
          turtle.pensize(4)
          turtle.penup()
          turtle.fd(-150)
          turtle.pendown()
          for?i?in?range(18):
          ????turtle.fd(300)
          ????turtle.left(100)
          turtle.fd(150)
          turtle.right(90)
          turtle.pensize(8)
          turtle.pencolor("green")
          turtle.fd(400)
          turtle.penup()
          turtle.pensize(6)
          turtle.pendown()
          #?繪制葉子
          turtle.fd(-250)
          turtle.seth(45)
          turtle.circle(-130,?60)
          turtle.seth(-135)
          turtle.circle(-130,?60)
          turtle.seth(135)
          turtle.circle(130,?60)
          turtle.seth(-45)
          turtle.circle(130,?60)
          turtle.done()

          賀卡

          我們還可以利用 Python 在照片中添加一些文字來制作賀卡。

          原圖:

          效果圖:

          主要實現(xiàn)代碼:

          img?=?cv2.imread('test.png')
          mask?=?np.zeros(img.shape[:2],?np.uint8)
          size?=?(1,?65)
          bgd?=?np.zeros(size,?np.float64)
          fgd?=?np.zeros(size,?np.float64)
          rect?=?(1,?1,?img.shape[1],?img.shape[0])
          cv2.grabCut(img,?mask,?rect,?bgd,?fgd,?10,?cv2.GC_INIT_WITH_RECT)
          mask2?=?np.where((mask?==?2)?|?(mask?==?0),?1,?255)
          img?=?img.astype(np.int32)
          img?*=?mask2[:,?:,?np.newaxis]
          img[img>255]?=?255
          img?=img.astype(np.uint8)
          img?=?cv2.cvtColor(img,?cv2.COLOR_BGR2RGB)
          img?=?Image.fromarray(img,?'RGB')
          img.save('test1.jpg')
          fp?=?open(r"word.txt",?"r",?encoding="utf-8")
          text?=?fp.read()
          mask_pic=np.array(Image.open(r"test1.jpg"))
          wordcloud?=?WordCloud(font_path='hyr3gjm.ttf',mask=mask_pic,max_words=200).generate(text)
          image=wordcloud.to_image()
          image.save("wordcloud2.png")
          cloud_data?=?np.array(image)
          alpha?=?np.copy(cloud_data[:,:,0])
          alpha[alpha>0]?=?255
          new_image?=?Image.fromarray(np.dstack((cloud_data,?alpha)))
          card?=?Image.open("test.png")
          card?=?card.convert("RGBA")
          card.paste(new_image,?(0,0),?mask=new_image)
          card.save("card.png")

          照片墻

          我們也可以利用 Python 將一些照片制作成文字效果的照片墻。

          實現(xiàn)效果:

          主要實現(xiàn)代碼:

          #?將字轉(zhuǎn)化為漢字庫的點陣數(shù)據(jù)
          def?char2bit(textStr):
          ????KEYS?=?[0x80,?0x40,?0x20,?0x10,?0x08,?0x04,?0x02,?0x01]
          ????target?=?[]
          ????global?count
          ????count?=?0
          ????for?x?in?range(len(textStr)):
          ????????text?=?textStr[x]
          ????????rect_list?=?[]?*?16
          ????????for?i?in?range(16):
          ????????????rect_list.append([]?*?16)
          ????????gb2312?=?text.encode('gb2312')
          ????????hex_str?=?binascii.b2a_hex(gb2312)
          ????????result?=?str(hex_str,?encoding='utf-8')
          ????????area?=?eval('0x'?+?result[:2])?-?0xA0
          ????????index?=?eval('0x'?+?result[2:])?-?0xA0
          ????????offset?=?(94?*?(area-1)?+?(index-1))?*?32
          ????????font_rect?=?None
          ????????with?open("HZK16",?"rb")?as?f:
          ????????????f.seek(offset)
          ????????????font_rect?=?f.read(32)
          ????????for?k?in?range(len(font_rect)?//?2):
          ????????????row_list?=?rect_list[k]
          ????????????for?j?in?range(2):
          ????????????????for?i?in?range(8):
          ????????????????????asc?=?font_rect[k?*?2?+?j]
          ????????????????????flag?=?asc?&?KEYS[i]
          ????????????????????row_list.append(flag)
          ????????output?=?[]
          ????????for?row?in?rect_list:
          ????????????for?i?in?row:
          ????????????????if?i:
          ????????????????????output.append('1')
          ????????????????????count+=1
          ????????????????else:
          ????????????????????output.append('0')
          ????????target.append(''.join(output))
          ????return?target

          #?生成圖片文字
          def?head2char(workspace,folder,self,outlist):
          ????#?將工作路徑轉(zhuǎn)移至頭像文件夾
          ????os.chdir(folder)
          ????#?獲取文件夾內(nèi)頭像列表
          ????imgList?=?os.listdir(folder)
          ????#?獲取頭像圖片個數(shù)
          ????numImages?=?len(imgList)
          ????#?設(shè)置頭像裁剪后尺寸
          ????eachSize?=?100
          ????#?變量?n?用于循環(huán)遍歷頭像圖片
          ????n=0
          ????#?變量?count?用于為最終生成的單字圖片編號
          ????count?=?0
          ????#?初始化顏色列表,用于背景著色
          ????colorlist?=?['#FFFACD','#F0FFFF','#BFEFFF','#b7facd','#ffe7cc','#fbccff','#d1ffb8','#febec0','#E0EEE0']
          ????#?index?用來改變不同字的背景顏色
          ????index?=?0
          ????#?每個?item?對應(yīng)不同字的點陣信息
          ????for?item?in?outlist:
          ????????#?將工作路徑轉(zhuǎn)到頭像所在文件夾
          ????????os.chdir(folder)
          ????????#?新建一個帶有背景色的畫布,16?*?16點陣,每個點處填充?2?*?2?張頭像圖片,故長為?16?*?2?*?100
          ????????canvas?=?Image.new('RGB',?(3200,?3200),?colorlist[index])??#?新建一塊畫布
          ????????#?index?變換,用于變換背景顏色
          ????????index?=?(index+1)%9
          ????????count?+=?1
          ????????#?每個?16?*?16?點陣中的點,用四張?100?*?100?的頭像來填充
          ????????for?i?in?range(16*16):
          ????????????#?點陣信息為?1,即代表此處要顯示頭像來組字
          ????????????if?item[i]?==?"1":
          ????????????????#?循環(huán)讀取連續(xù)的四張頭像圖片
          ????????????????x1?=?n?%?len(imgList)
          ????????????????x2?=?(n+1)?%?len(imgList)
          ????????????????x3?=?(n+2)?%?len(imgList)
          ????????????????x4?=?(n+3)?%?len(imgList)
          ????????????????#?以下四組?try,將讀取到的四張頭像填充到畫板上對應(yīng)的一個點位置
          ????????????????#?點陣處左上角圖片?1/4
          ????????????????try:
          ????????????????????#?打開圖片
          ????????????????????img?=?Image.open(imgList[x1])
          ????????????????except?IOError:
          ????????????????????print("有1張圖片讀取失敗,已使用備用圖像替代")
          ????????????????????img?=?Image.open(self)
          ????????????????finally:
          ????????????????????#?縮小圖片
          ????????????????????img?=?img.resize((eachSize,?eachSize),?Image.ANTIALIAS)
          ????????????????????#?拼接圖片
          ????????????????????canvas.paste(img,?((i?%?16)?*?2?*?eachSize,?(i?//?16)?*?2?*?eachSize))
          ????????????????#?點陣處右上角圖片?2/4
          ????????????????try:
          ????????????????????img?=?Image.open(imgList[x2])
          ????????????????except?IOError:
          ????????????????????print("有1張圖片讀取失敗,已使用備用圖像替代")
          ????????????????????img?=?Image.open(self)
          ????????????????finally:
          ????????????????????img?=?img.resize((eachSize,?eachSize),?Image.ANTIALIAS)
          ????????????????????canvas.paste(img,?(((i?%?16)?*?2?+?1)?*?eachSize,?(i?//?16)?*?2?*?eachSize))
          ????????????????#?點陣處左下角圖片?3/4
          ????????????????try:
          ????????????????????img?=?Image.open(imgList[x3])
          ????????????????except?IOError:
          ????????????????????print("有1張圖片讀取失敗,已使用備用圖像替代")
          ????????????????????img?=?Image.open(self)
          ????????????????finally:
          ????????????????????img?=?img.resize((eachSize,?eachSize),?Image.ANTIALIAS)
          ????????????????????canvas.paste(img,?((i?%?16)?*?2?*?eachSize,?((i?//?16)?*?2?+?1?)?*?eachSize))
          ????????????????#?點陣處右下角圖片?4/4
          ????????????????try:
          ????????????????????img?=?Image.open(imgList[x4])
          ????????????????except?IOError:
          ????????????????????print("有1張圖片讀取失敗,已使用備用圖像替代")
          ????????????????????img?=?Image.open(self)
          ????????????????finally:
          ????????????????????img?=?img.resize((eachSize,?eachSize),?Image.ANTIALIAS)
          ????????????????????canvas.paste(img,?(((i?%?16)?*?2?+?1)?*?eachSize,?((i?//?16)?*?2?+?1)?*?eachSize))
          ????????????????#調(diào)整?n?以讀取后續(xù)圖片
          ????????????????n=?(n+4)?%?len(imgList)
          ????????os.chdir(workspace)
          ????????#?創(chuàng)建文件夾用于存儲輸出結(jié)果
          ????????if?not?os.path.exists('output'):
          ????????????os.mkdir('output')
          ????????os.chdir('output')
          ????????#?存儲將拼接后的圖片,quality?為圖片質(zhì)量,1?-?100,100?最高
          ????????canvas.save('result%d.jpg'%?count,?quality=100)

          總結(jié)

          本文,我們利用 Python 實現(xiàn)了 3 種送祝福的方式,你有哪些更好或有趣的實現(xiàn)方式嗎?

          PS公號內(nèi)回復(fù)「Python」即可進入Python 新手學習交流群,一起 100 天計劃!


          老規(guī)矩,兄弟們還記得么,右下角的 “在看” 點一下如果感覺文章內(nèi)容不錯的話,記得分享朋友圈讓更多的人知道!

          代碼獲取方式

          識別文末二維碼,回復(fù):200910

          瀏覽 168
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  狠狠操狠操 | 久久久精品影院 | 在线免费观看黄色电影 | 任你躁精品视频一区二区三区 | 欧美日韩国产在线观看 |