资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么用Python制作图片

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

创新互联坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。10余年网站建设经验创新互联是成都老牌网站营销服务商,为您提供网站设计、成都网站建设、网站设计、H5页面制作、网站制作、品牌网站设计、小程序定制开发服务,给众多知名企业提供过好品质的建站服务。

制作母亲节词云图-祝福方式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制作图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


分享标题:怎么用Python制作图片
链接URL:http://cdkjz.cn/article/pcdpdp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220