Tomorrow is Valentine's day. This Valentine's Day is doomed to be a Valentine's day that can't be dated, but not dating doesn't mean that can't be romantic. The ancients were born earlier than us, and those romantic poems were mined out by them. We certainly have no chance. Fortunately, we also have Python, otherwise we don't know how to express romance. Next, wave guide teaches you to make a romantic Valentine's day exclusive greeting card.
First of all, prepare a picture of you and a passionate expression. Lang Dao has passed the age of love, so he has to borrow https://baijiahao.baidu.com/s? Id = 1658389297213946646 & Wfr = Spider & for = PC for photos and love words. Invasion and deletion.
The original picture and the postcard are put together. The effect is as follows:
I chose this picture:
Treat the humanoid part to pure white:
>>> import cv2 >>> import numpy as np >>> from PIL import Image >>> img = cv2.imread('d:\\photo.jpg') >>> 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, img.shape) >>> 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('d:\\mask.jpg')
It's a little flawed. It doesn't matter. It doesn't affect the later effect.
With wordcloud library, word cloud can be easily generated. First, install wordcloud:
pip install wordcloud
After installation, you can generate a word cloud:
>>> from wordcloud import WordCloud >>> fp = open(r"d:\Love words.txt", "r") >>> text = fp.read() >>> text 'Spring wind ten li, can't meet you; clear sky ten thousand li, can't have you in heart.\n There are thousands of different kinds of people in the world. Don't ask for the floating clouds. If a man is a rainbow, he will know it when he comes across it.\n Without the romance of film and TV series, I dare not allow you to live three lives, but to give you two halls and three rooms.\n You can give the rest of your life to me for safekeeping, and take over every word of my love words.\n Green mountain is not as long as your eyebrow, clear water is not as clear as your eyes, across the mountains and rivers several rains, I only want one you in my life.\n I don't like any kind of people. If I like you, I just like you.\n Spring flowers, autumn moon, summer breeze, winter and evening snow are all beautiful, but only your heart is the four seasons I want to go.\n The strongest feeling in the world is not "I love you", but "I'm used to having you". Mutual dependence is the deepest love.\n When you are around, you are the whole world; when you are not around, the whole world is you.\n You may not be the best person in the world, but when I fall in love with you, you are my whole world!\n It's not my intention to meet you, it's my will to know you, it's my will to think you're my love. When I don't see you, I'll have two minds. When I see you, I'll have one mind.\n I think the sunset is red because it has a sun in its arms. I think my face should be red too, because you live in my heart.' >>> wordcloud=WordCloud(font_path="C:/Windows/Fonts/simfang.ttf", background_color="black",width=600,height=300,max_words=50).generate(text) >>> image=wordcloud.to_image() >>> image.save("d:\\wordcloud1.png")
The results are as follows:
wordcloud supports mask, which can generate word cloud of specified shape by using the previously processed image:
>>> import numpy as np >>> from PIL import Image >>> mask_pic=numpy.array(Image.open(r"d:\mask.jpg")) >>> wordcloud = WordCloud(font_path=r"C:\Windows\Fonts\simfang.ttf",mask=mask_pic).generate(text) >>> image=wordcloud.to_image() >>> image.save("d:\\wordcloud2.png")
The result is:
wordcloud does not support the generation of transparent images. We need to process the images as transparent by ourselves:
>>> cloud_data = np.array(image) >>> alpha = np.copy(could_data[:,:,0]) # Generate transparent channel >>> alpha[alpha>0] = 255 # Not black set to 255 >>> new_image = Image.fromarray(np.dstack((cloud_data, alpha)))
Finally, use PIL to merge the two layers:
>>> card = Image.open("d:\\photo.jpg") >>> card = card.convert("RGBA") >>> card.paste(new_cloud, (0,0), mask=new_cloud) >>> card.save("d:\\card.png")
Send it to your girlfriend for romance: