不同的語言,同一個七夕
4 人贊了文章
~這個時候,有對象的都出去浪了...首先要祝有情人終成眷屬~
作為單身狗...也不胡思亂想啥了,那就自娛自樂一下,
整理整理不同語言,而又比較符合七夕主題的有意思的代碼~
論騷操作...MATLAB絕對夠硬,比如畫個小心心?
n=100;x=linspace(-3,3,n); %nirvy=linspace(-3,3,n);z=linspace(-3,3,n);[X,Y,Z]=ndgrid(x,y,z);F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3); %心型函數,《高數(同濟大學版)-附錄》,是不是回憶起來了什麼...isosurface(F,0)lighting phongcaxis axis equalcolormap(flag); %顏色view([60 30]); %視角
人生苦短,我用python~表白,也少不了他的身影~
比如一行代碼就能寫出對你的愛
print(
.join([.join([(I LOVE U[(x-y) % 8] if ( (x*0.05)**2 + (y*0.1)**2 -1)**3 - (x*0.05)**2 * (y*0.1)**3 <=0 else ) for x in range(-30,30)]) for y in range(15, -15, -1)]))
#!/usr/bin/env python# -*- coding: utf-8 -*-import jiebaimport numpyimport pandasimport matplotlib.pyplot as pltfrom wordcloud import WordCloudfrom scipy.misc import imreadfrom PIL import Image, ImageDrawfrom wordcloud import ImageColorGeneratorfrom clize import Parameter, rundef cut_file_text(text_file): # 讀取文件內容並將其分詞 with open(text_file, encoding=utf8) as f: content = f.read() segs = jieba.cut(content) # 分詞 return [seg for seg in segs if len(seg) > 1 and seg !=
] # 拚棄標點符號和單字# 讀取內容# segment = cut_file_text("./data/love_letter.txt")# print(segment)def word_statistics(seg, stop_words="./data/stopwords.txt"): # 1. 去除文本中不適合的辭彙, 為了方便統計詞頻,我們把結果保存在pandas的 DataFrame 格式方便統計 words_df = pandas.DataFrame({segment:seg}) # words_df.head() # 查看大致內容 stopwords = pandas.read_csv(stop_words, index_col=False, quoting=3, sep=" ", names=[stopword], encoding="utf8") words_df = words_df[~words_df.segment.isin(stopwords.stopword)] # 2. 詞頻統計 words_stat = words_df.groupby(by=[segment])[segment].agg({"count":numpy.size}) words_stat = words_stat.reset_index().sort_values("count", ascending=False) # words_stat.to_csv(result.txt,sep= , encoding=utf-8) return words_stat# word_stat = word_statistics(segment)# print(word_stat.head(20000))def _show_and_save_img(img, file_name = None): if file_name is not None: img.to_file(file_name) plt.axis("off") plt.imshow(img) plt.show()# ref: https://github.com/amueller/word_clouddef gen_word_cloud_rectangle(words_stat, font_path="./demo.ttf", background_color="white"): # 使用matplotlib和wordcloud工具來圖形化顯示上述的詞頻統計結果 wordcloud = WordCloud(font_path=font_path, background_color=background_color) word_frequence = {x[0]: x[1] for x in words_stat.head(20000).values} # 最多顯示20000個 wordcloud = wordcloud.fit_words(word_frequence) return wordcloud# img = gen_word_cloud_rectangle(word_stat)# _show_and_save_img(img)def gen_word_cloud_picture(words_stat, font_path="./demo.ttf", mask_file="./data/heart.jpg", word_color_img="./data/pink.jpg", background_color="white"): # 自定義圖像背景並將詞雲圖形化輸出 mask_img = imread(mask_file) wordcloud = WordCloud(background_color=background_color, mask=mask_img, font_path=font_path) word_frequence = {x[0]: x[1] for x in words_stat.head(20000).values} wordcloud = wordcloud.fit_words(word_frequence) color_img = imread(word_color_img) mask_color = ImageColorGenerator(color_img) return wordcloud.recolor(color_func=mask_color)# img = gen_word_cloud_picture(word_stat)# _show_and_save_img(img, "./out/word_cloud.png")def add_background(img, background="./data/background.jpg"): # 為詞雲添加背景圖像 new_img = img.to_image() # convert to Image background = Image.open(background) final_img = Image.blend(background, new_img, 1) # 這樣疊加是覆蓋式的,需要專為numpy後再行判斷疊加較好 final_img.show() final_img.save("./out/word_cloud.png")def main(*par, text_file:t="./data/love_letter.txt", stop_file:s="./data/stopwords.txt", color_img:c="./data/pink.jpg", mask_file:m="./data/heart.jpg", out_file:o="./out/word_cloud.png", font_path: p=./demo.ttf,): 生成文字雲 :param text_file: text file that contain all you word :param stop_file: the stop word which cant be considered :param color_img: the color map img :param mask_file: the mask img for the word :param out_file: output file path which should with sufix of png/jpg... :param font_path: font path segment = cut_file_text("./data/love_letter.txt") word_stat = word_statistics(segment) if mask_file is None: img = gen_word_cloud_rectangle(word_stat) _show_and_save_img(img, out_file) else: img = gen_word_cloud_picture(word_stat, font_path, mask_file, color_img) _show_and_save_img(img, out_file)if __name__ == __main__: run(main)
那咱就搞個詞雲~
#!/usr/bin/env python# -*- coding: utf-8 -*-from PIL import Image, ImageDraw, ImageFontimport os, sysfrom clize import Parameter, runfrom effect_func import effects_funcdef list_file(filedir, sufix=None): # 根據輸入的後綴來列舉文件夾中的文件,支持多個後綴 # sufix: str 類型的後綴,或者 list 類型的後綴列表 file_list = os.listdir(filedir) file_list = [filedir + os.path.sep + f for f in file_list] # add path for files if sufix is not None: if isinstance(sufix, str): ret_list = [f for f in file_list if f.endswith(sufix)] elif isinstance(sufix, list): ret_list = [f for f in file_list if any([f.endswith(t) for t in sufix])] else: print(f"undefined input of sufix:{type(sufix)}") ret_list = None return ret_list else: return file_listdef picture_wall_rectangle(wall_width, wall_length, edge_len, pic_dir="./img"): # 生成長方形的照片牆,輸入:長和寬對應的照片數量,各個照片的邊長,照片所在文件夾 new_img = Image.new(RGBA, (edge_len * wall_width, edge_len * wall_length)) file_list = list_file(pic_dir, [.jpeg, .jpg, .gif, .png, .bmp]) for x in range(0, wall_width): for y in range(0, wall_length): file_name = file_list[(x * wall_width + y) % len(file_list)] try: img = Image.open(file_name) img = img.resize((edge_len, edge_len), Image.ANTIALIAS) new_img.paste(img, (x * edge_len, y * edge_len)) except Exception as e: print(f"open file {file_name} failed!") return new_imgdef gen_text_img(text, font_size=20, font_path=None): # 從文字生成圖像,輸入:文字內容,文字字體大小,字體路徑 font = ImageFont.truetype(font_path, font_size) if font_path is not None else None (width, length) = font.getsize(text) # 獲取文字大小 text_img = Image.new(RGBA, (width, length)) draw = ImageDraw.Draw(text_img) # 第一個tuple表示未知(left,up),之後是文字,然後顏色,最後設置字體 draw.text((0, 0), text, fill=(0, 0, 0), font=font) return text_imgdef picture_wall_mask(text_img, edge_len, pic_dir="./img", method=alpha): # 根據文字圖像生成對應的照片牆,輸入:文字圖像,各個照片邊長,照片所在路徑 new_img = Image.new(RGBA, (text_img.size[0] * edge_len, text_img.size[1] * edge_len)) file_list = list_file(pic_dir, [.jpeg, .jpg, .gif, .png, .bmp]) img_index = 0 for x in range(0, text_img.size[0]): for y in range(0, text_img.size[1]): pixel = text_img.getpixel((x, y)) file_name = file_list[img_index % len(file_list)] try: img = Image.open(file_name).convert(RGBA) img = img.resize((edge_len, edge_len), Image.ANTIALIAS) # img = trans_alpha(img, pixel) # img = trans_size(img, pixel) img = effects_func[method](img, pixel) new_img.paste(img, (x * edge_len, y * edge_len)) img_index += 1 except Exception as e: print(f"open file {file_name} failed! {e}") return new_img# img_ascii = picture_wall_mask(text_img, 50, "./img/")# img_ascii.show()# img_ascii.save(./out/img_ascii.png)# ref: http://clize.readthedocs.io/en/stable/basics.html#collecting-all-positional-argumentsdef main(*text, font_size:s=20, edge_len:e=50, wall_width:w=20, wall_length:l=10, pic_dir:d="./img", out_dir:o="./out/", font_path: p=./demo.ttf, method: m=alpha): 生成照片牆 :param text: Text of picture wall, if not defined this will generage a rectangle picture wall :param font_size: font size of a clear value :param edge_len: sub pictures egde length :param wall_width: picture number of rectangle width :param wall_length: picture number of rectangle length :param pic_dir: pictures path :param out_dir: output dir :param font_path: font path :param method: decrator method, now accept alpha, size if len(text) >= 1: text_ = .join(text) print(f"generate text wall for {text_} with picture path:{pic_dir}") if method not in effects_func.keys(): raise Exception(fparam method[-m {method}] not defined! accept method is: size, alpha) text_img = gen_text_img(text_, font_size, font_path) # text_img.show() img_ascii = picture_wall_mask(text_img, edge_len, pic_dir, method) img_ascii.show() img_ascii.save(out_dir + os.path.sep + _.join(text) + .png) else: print(f"generate rectangle wall with picture path:{pic_dir}") img_rec = picture_wall_rectangle(wall_width, wall_length, edge_len, pic_dir) img_rec.show() img_rec.save(out_dir + os.path.sep + img_rec.png)if __name__ == __main__: run(main) pass
嫌是動態?那就來個HTML
HTML5浪漫愛心表白動畫DEMO演示自己打開看動畫啦~~~
再附另一個小demo,裡面的注釋按提示可以改成你自己的~~~
HTML5浪漫愛心表白動畫在線演示具體做法可以參照~
打造浪漫的Android表白程序 - CSDN博客以上程序全是東拼西湊,不知源頭,如有冒犯,請私信聯繫我~~~
編輯到這裡的時候...突然想到
唉,這個時候有對象的怎麼會看這破文...洗洗睡吧
推薦閱讀:
TAG:七夕 | 情人節ValentinesDay | 表白 |