-
2021-12-30 17:59:38
在一些应用场景中,我们需要将一些自定义的文字内容附加到某张图片中。本篇文章将对这一功能进行实现。
import PIL from PIL import ImageFont,Image,ImageDraw #设置字体,如果没有,也可以不设置 font = ImageFont.truetype(r"C:\Windows\Fonts\simsun.ttc",70) #font2 = ImageFont.truetype(r"C:\Windows\Fonts\simsun.ttc",60) #打开背景图片 imageFile = "base.png" im1=Image.open(imageFile) # 在图片上添加文字 draw = ImageDraw.Draw(im1) draw.text((100, 100),"文字在这里出现:",(0,0,0),font=font) #draw.text((100, 220),"昨日共完成",(0,0,0),font=font2) #draw = ImageDraw.Draw(im1) # 保存 im1.save("custom.png")
更多相关内容 -
Python在图片中添加文字的两种方法
2020-09-21 08:32:56主要给大家介绍了在Python在图片中添加文字的两种方法,分别是使用OpenCV和PIL这两个方法实现,在实际应用中要在这两种方法中择优使用。两种方法都给出了详细示例代码,需要的朋友可以参考下。 -
Python在图片上添加文字
2022-03-02 16:07:49图片加文字对图片增加文字。
导入所需要的库from PIL import ImageFont from PIL import Image from PIL import ImageDraw
添加字体(需要设置,可以不设置),加文字
全部代码from PIL import ImageFont from PIL import Image from PIL import ImageDraw #设置字体,如果没有,也可以不设置 font = ImageFont.truetype("C:\WINDOWS\FONTS\MSYHL.TTC",103) #打开底版图片 imageFile = "11.jpg" tp=Image.open(imageFile) # 在图片上添加文字 1 draw = ImageDraw.Draw(tp) draw.text((100, 100),"测试文字",(255,255,0),font=font) draw = ImageDraw.Draw(tp) # 保存 tp.save("1223.png")
效果:
添加文字
若有不合理的地方请指出探讨! -
用Python在图片上添加文字
2021-12-07 10:07:35图片上添加文字,这个实现起来其实很简单 from PIL import Image,ImageDraw,ImageFont # pip install pillow fp = '图片.png' img = Image.open(fp) draw = ImageDraw.Draw(img) ttfront = ImageFont.truetype('...需求
需求就是一批图片,在固定位置加上相应文字。为查看折线图的人在一张图片上提供足够的信息。
代码
其实只需要寥寥数行代码就能实现,
而且支持调整字体、大小、颜色from PIL import Image,ImageDraw,ImageFont # pip install pillow fp = '图片.png' img = Image.open(fp) draw = ImageDraw.Draw(img) ttfront = ImageFont.truetype('msyh.ttc', 14) #字体,大小 content = '''正文''' draw.text((976, 260),content,fill=(0,25,25), font=ttfront)#文字位置,正文内容,文字RGB颜色,字体 img.save(fp) print('执行完毕')
-
python批量在图片指定位置添加文字
2020-12-21 10:08:03批量在图片指定位置添加文字python # 导入PIL模块 from PIL import Image, ImageDraw, ImageFont, ImageFilter zt = r"C:/Windows/Fonts/simhei.ttf" # 指定字体 def tianzi(pic, site, txt, path): # 打开初始... -
python实现在函数图像上添加文字和标注的方法
2020-09-19 03:29:16今天小编就为大家分享一篇python实现在函数图像上添加文字和标注的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
python实现图片上添加图片
2021-01-02 23:18:05在介绍完给图上添加文字后,我们再介绍给图片上添加图片,也就是图片的叠加。 需要使用的Python的图像库:PIL.更加详细的知识点如下: Imaga模块:用来创建,打开,保存图片文件 new(path):用来创建一个新的图片... -
python使用PIL给图片添加文字生成海报示例
2020-12-24 17:20:52我又开始想写作了,一个写了几年代码的老男人,在被生活粗暴地摁在地上摩擦几回后,突然触发了内心的柔软,想写些东西。 要写些什么?如何写?在我看来,写作比写代码更难,详见 编程与写作 那就先从最简单的每天... -
python向图片里添加文字
2020-09-18 09:34:09主要为大家详细介绍了python向图片里添加文字,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
python在图片上添加文字 - cv2模块的安装与使用
2021-08-11 14:29:48一、cv2的安装 一般来说,在pycharm中安装包的方式如下: ... 但是这个包在安装过程中一直报错,安装...如上图,"opencv-python"已经在packages列表中了,在程序中import cv2,没有报错,说明这个包现在可以开始使用了。一、cv2的安装
- 一般来说,在pycharm中安装包的方式如下:
- 点击“+”之后,在搜索框中输入“cv2”,点击下方的“Install package”
- 但是这个包在安装过程中一直报错,安装不成功,错误信息如下:
- 于是在看了很多大佬的解决方法之后,最终用如下方法成功解决:
安装上述操作步骤,将"cv2"换成"opencv-python",即可安装成功。
- 如上图,"opencv-python"已经在packages列表中了,在程序中
import cv2
,没有报错,说明这个包现在可以开始使用了。
二、cv2的使用-在图片上添加文字
#!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2021/8/6 17:24 # @Author : sxgjmn import cv2 input_img = 'C:/Users/DELL/Desktop/pathway-gene-ppi_legend.png' output_img = 'C:/Users/DELL/Desktop/pathway-gene-ppi_legend_2.png' edge_node_dir = 'C:/Users/DELL/Desktop/生信项目/Cytoscape网络开发/test_data_2' def get_node_count_pvalue(indir): # Get max value and min value of nodes Count and pvalue node_file = indir + '/node_edge/pathway-pathway_node.txt' count = [] pvalue = [] infile = open(node_file, 'r') for line in infile.readlines(): row = line.rstrip().split('\t') if row[0] == 'id': continue else: count.append(row[3]) pvalue.append(row[2]) count = list(map(int, count)) pvalue = list(map(float, pvalue)) max_count = max(count) min_count = min(count) max_pvalue = '{:.3f}'.format(max(pvalue)) min_pvalue = '{:.3f}'.format(min(pvalue)) infile.close() return max_count, min_count, max_pvalue, min_pvalue def get_edge_value(indir): # Get max and min value of edges value edge_file = indir + '/node_edge/pathway-pathway_edge.txt' value = [] infile = open(edge_file, 'r') for line in infile.readlines(): row = line.rstrip().split('\t') if row[0] == 'id': continue else: value.append(row[3]) value = list(map(int, value)) max_value = max(value) min_value = min(value) infile.close() return max_value, min_value def add_text_png(input_img, output_img, edge_node_dir): # Add text in image in_img = cv2.imread(input_img) max_count, min_count, max_pvalue, min_pvalue = get_node_count_pvalue(edge_node_dir) max_value, min_value = get_edge_value(edge_node_dir) cv2.putText(in_img, max_pvalue, (105, 69), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(in_img, min_pvalue, (105, 225), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(in_img, str(min_count), (120, 345), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(in_img, str(max_count), (120, 410), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(in_img, str(min_value), (120, 520), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(in_img, str(max_value), (120, 560), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 1, cv2.LINE_AA) cv2.imshow("pathway-gene-ppi_legend_2", in_img) cv2.waitKey() cv2.imwrite(output_img, in_img) add_text_png(input_img, output_img, edge_node_dir)
上面的程序里,前两个函数是处理数据的,第三个才是添加文字的,"(105, 69)"这里的数字是文字在图片上的位置,"0.55"是字体的大小,其他参数基本不需要调。
- 一般来说,在pycharm中安装包的方式如下:
-
Python-OpenCV图片上添加文字
2021-06-16 18:27:15import cv2 font = cv2.FONT_HERSHEY_SIMPLEX img = cv2.imread('putText_1.jpg') cv2.putText(img, 'judy', (110, 1590), font, 8, (128, 0, 128), 8) cv2.namedWindow('putText_1', cv2.WINDOW_KEEPRATIO) ... -
Python实现图片添加文字
2020-09-18 09:40:07主要为大家详细介绍了Python实现图片添加文字,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
python实现给gif添加文字
2018-06-16 19:34:24用python实现了给gif图片添加文字可改变位置颜色字体等 -
Python 使用 Pillow 模块给图片添加文字水印的方法
2020-09-18 17:08:39主要介绍了Python 使用 Pillow 模块给图片添加文字水印的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下 -
在Python 中实现图片加框和加字的方法
2021-01-01 03:50:45第一步:安装opencv-python rpm -ivh opencn-python-2.4.5-3.el7.ppc64le.rpm 第二步:引用cv2 import cv2 第三步:读入图片,必须是全路径 im = cv2.imread(filename) 第四步:设置需要画框的左上角与右下角的... -
python如何在图片上添加文字(中文和英文)
2020-12-09 02:57:52Python在图片上添加文字的两种方法:OpenCV和PIL一、OpenCV方法1.安装cv2pip install opencv-python2.利用putText方法来实现在图片的指定位置添加文字putText(img,text,org,fontFace,fontScale,color,... -
Python在图片中插入大量文字并且自动换行
2020-12-08 01:31:36问题如何在图片中插入大量文字并且自动换行效果原始图效果图注明若需要写入中文请使用中文字体实现方式from PIL import Image, ImageDraw, ImageFontclass ImgText:font = ImageFont.truetype("micross.ttf", 24)def... -
Python如何创建在图片中加入文字
2020-05-17 12:44:53我们在微信或者QQ聊天时会用到许多图片表情包,我们可以利用python的pillow库来实现表情包的添加 1.导入程序所需要的库 from PIL import Image,ImageDraw,ImageFont import matplotlib.pyplot as plt 如果没有...