您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用Python制作圖片”,在日常操作中,相信很多人在怎么用Python制作圖片問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用Python制作圖片”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
這個(gè)世界只有一個(gè)母親,包容我的一千萬(wàn)任性!
今天我用 Python 制作一個(gè)詞云圖給媽媽送祝福,制作代碼如下:
import numpy import multidict import matplotlib.pyplot as plt from scipy.misc import imread from wordcloud import WordCloud, ImageColorGenerator def transform_format(val): """ 用于去除雜色 Arguments: val {[array]} -- RGB顏色組 Returns: [array] -- 去除雜色后的值 """ if val[0] > 245 and val[1] > 245 and val[2] > 245: val[0] = val[1] = val[2] = 255 return val else: return val def gen_happy_birthday_cloud(file, name): words = multidict.MultiDict() # 必須先初始化兩個(gè)最大權(quán)重的 words.add('母親節(jié)快樂(lè)', 10) words.add(name, 12) # 隨意插入新的詞語(yǔ) for i in range(1000): words.add('媽媽', numpy.random.randint(1, 5)) words.add('您辛苦了', numpy.random.randint(1, 5)) words.add(name, numpy.random.randint(1, 5)) # 設(shè)定圖片 bimg = imread(file) for color in range(len(bimg)): bimg[color] = list(map(transform_format, bimg[color])) wordcloud = WordCloud( background_color='white', mask=bimg, font_path='simhei.ttf' ).generate_from_frequencies(words) # 生成詞云 bimgColors = ImageColorGenerator(bimg) # 渲染詞云 plt.axis("off") plt.imshow(wordcloud.recolor(color_func=bimgColors)) plt.savefig(name + '.png') plt.show() gen_happy_birthday_cloud("mother.jpg", "母親節(jié)快樂(lè)")
思路為: 導(dǎo)入一張圖片后再輸入節(jié)日祝福語(yǔ)后進(jìn)行圖片渲染,最后再根據(jù)圖片形狀生成相應(yīng)詞云圖。
運(yùn)行結(jié)果如下:
另一個(gè)形狀圖片生成的詞云圖:圖片
接下來(lái)還要制作一個(gè)唯美飄落桃心給媽媽們送祝福!
import time from random import randint for i in range(1, 35): # 打印抬頭 print('') heartStars = [2, 4, 8, 10, 14, 20, 26, 28, 40, 44, 52, 60, 64, 76] # *的位置 heartBreakLines = [13, 27, 41, 55, 69, 77] # 空格的位置 flowerBreakLines = [7, 15, 23, 31, 39, 46] # 玫瑰的空列位置 def addSpaces(a): # 添加空列 count = a while count > 0: print(' ', end='') count -= 1 def newLineWithSleep(): # 添加空行 time.sleep(0.3) print('\n', end='') play = 0 while play == 0: Left_Spaces = randint(8, 80) addSpaces(Left_Spaces) for i in range(0, 78): # 比心的形狀 if i in heartBreakLines: newLineWithSleep() addSpaces(Left_Spaces) elif i in heartStars: print('*', end='') elif i in (32, 36): print('M', end='') elif i == 34: print('O', end='') else: print(' ', end='') newLineWithSleep() addSpaces(randint(8, 80)) print("H a p p y M o t h e r ' s D a y !", end='') newLineWithSleep() newLineWithSleep() Left_Spaces = randint(8, 80) addSpaces(Left_Spaces) for i in range(0, 47): # 向母親獻(xiàn)花 if i in flowerBreakLines: newLineWithSleep() addSpaces(Left_Spaces) elif i in (2, 8, 12, 18): print('{', end='') elif i in (3, 9, 13, 19): print('_', end='') elif i in (4, 10, 14, 20): print('}', end='') elif i in (27, 35, 43): print('|', end='') elif i in (34, 44): print('~', end='') elif i == 11: print('o', end='') else: print(' ', end='') print('\n', end='')
實(shí)現(xiàn)效果如下:
到此,關(guān)于“怎么用Python制作圖片”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。