怎么用Python制作图片

发布时间:2021-06-26 14:41:08 作者:chen
来源:亿速云 阅读:1393

这篇文章主要介绍“怎么用Python制作图片”,在日常操作中,相信很多人在怎么用Python制作图片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Python制作图片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

制作母亲节词云图-祝福方式1

这个世界只有一个母亲,包容我的一千万任性!

今天我用 Python 制作一个词云图给妈妈送祝福,制作代码如下:

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()     # 必须先初始化两个最大权重的     words.add('母亲节快乐', 10)     words.add(name, 12)      # 随意插入新的词语     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))      # 设定图片     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", "母亲节快乐")

思路为: 导入一张图片后再输入节日祝福语后进行图片渲染,最后再根据图片形状生成相应词云图。

怎么用Python制作图片

运行结果如下:

怎么用Python制作图片

另一个形状图片生成的词云图:图片

飘落爱心玫瑰-祝福方式2

接下来还要制作一个唯美飘落桃心给妈妈们送祝福!

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):  # 向母亲献花         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='')

实现效果如下:

怎么用Python制作图片

到此,关于“怎么用Python制作图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. python怎么制作图片缩略图
  2. 怎么用Python制作表白图片

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Android中怎么实现事件分发机制

下一篇:Android中怎么实现代理模式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》