img1.rectangle(shape, fill ="# ffff33", outline ="red") img.show() 输出: 另一个示例:在这里,我们使用不同的颜色进行填充。 # importing image object from PILimportmathfromPILimportImage, ImageDraw w, h =220,190shape = [(40,40), (w -10, h -10)]# creating new Image objectimg = ...
ImageDraw.Draw:用于创建绘图对象。 Draw.rectangle:用于绘制矩形。 Draw.text:用于在图像上绘制文本。 应用场景 Pillow广泛应用于各种需要图像处理的场景,例如: 数据可视化 图像编辑 图标制作 文档处理 示例代码 以下是一个使用Pillow绘制矩形和文本的示例代码: 代码语言:txt 复制 from PIL import Image, ImageDraw, ...
1 1.安装pil库2.打开一张图片3.用PIL.ImageDraw.ImageDraw.rectangle绘制一个矩形4.保存图片 注意事项 注意3.7的模块安装是pip install pillow
image=Image.open("example.jpg") 1. 创建画布 在PIL 中,我们可以使用ImageDraw类的Draw方法在图片上绘制图形。首先,我们需要创建一个可绘制的对象。 可以使用以下代码来创建一个可绘制的对象: draw=ImageDraw.Draw(image) 1. 画框 接下来,我们可以使用draw.rectangle方法在图片上画一个矩形框。 该方法接受一个...
draw.rectangle([(x1, y1), (x2, y2)], fill=color+(alpha,)) 保存修改后的图像: 代码语言:txt 复制 image.save("modified_image.jpg") 这样就可以在PIL中改变矩形的不透明度了。 PIL是一个功能强大的图像处理库,广泛应用于图像处理、计算机视觉等领域。它提供了丰富的图像处理功能和易于使用的API,适用于...
的笔刷foriinrange(193):#(i = 0,1,2,...,192)forjinrange(193):#结构:遍历每一个像素(这种设计通常用for)draw.rectangle([16*i,16*j,16*(i+1),16*(j+1)],fill=original_image.getpixel((i,j)))#构造:每一个像素在新画布的指定位置放大16倍scaled_image.save('scaled_square.png')#流出:...
多边形 polygon (L) draw.polygon([(60,60), (90,60), (90,90), (60,90)]) #draw a square 矩形rectangle (bbox) # first coord属于矩形, second coord不属于 文字text(xy,message,font=None) 绘制文字message,文本区域左上角坐标为xy drawable.text((10, 10), "Hello", fill=(255,0,0), fon...
多边形 polygon (L) draw.polygon([(60,60), (90,60), (90,90), (60,90)]) #draw a square 矩形rectangle (bbox) # first coord属于矩形, second coord不属于 文字text(xy,message,font=None) 绘制文字message,文本区域左上角坐标为xy drawable.text((10, 10), "Hello", fill=(255,0,0), fon...
draw = ImageDraw.Draw(image) font = ImageFont.truetype(font='PingFang.ttc', size=40) # 参数:位置、文本、填充、字体 draw.text(xy=(100, 100), text='demo', fill=(255, 0, 0), font=font) # 画个边框为1的红色矩形框 draw.rectangle(xy=(90, 100, 210, 160), fill=None, outline="re...
from PIL import Image, ImageDraw # Open an image file image = Image.open('example.jpg') # Create an ImageDraw object draw = ImageDraw.Draw(image) # Draw a filled rectangle draw.rectangle(xy=(50, 50, 100, 100), fill=(255, 0, 0)) # Draw an unfilled rectangle with a red outline...