图形缩放
在PIL库中可以实现图形的缩放,但是 如果使用下面的写法的话,会造成部分的信息丢失
img = img.resize((width, height))
但是,在PIL中 带ANTIALIAS滤镜缩放结果,程序如下:
img = img.resize((width, height), Image.ANTIALIAS) 这样的话就不会有信息丢失啦
创建绘画对象 ImageDraw module creates drawing surface for image
import Image, ImageDraw
im = Image.open(“vacation.jpeg")
drawSurface = ImageDraw.Draw(im)
基本绘画操作 Basic methods of drawing surface
- 弧/弦/扇形 chord arc pieslice (bbox, strtAng, endAng)
- 椭圆 ellipse (bbox)
- 线段/多段线 line (L) draw.line(((60,60),(90,60), (90,90), (60,90), (60,60))) #draw a square
- 点 point (xy) #单像素点很小看不清,实际中可用实心小圆代替
- 多边形 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), font=None) - 文字大小 textsize(message,font=None) 给定文字message,返回所占像素(width,height)
可选参数 Common optional args for these methods
- fill=fillColor
- outline=outlineColor
矢量字体支持 TrueType Font support
import ImageFont
ttFont = ImageFont.truetype (“arial.ttf”, 16)
drawable.text ((10, 10), “Hello”, fill=(255,0,0), font=ttFont)
Rectangle
定义:draw.rectangle(box,options)
含义:绘制一个长边形。变量box是包含2元组[(x,y),…]或者数字[x,y,…]的任何序列对象。它应该包括2个坐标值。
注意:当长方形没有没有被填充时,第二个坐标对定义了一个长方形外面的点。变量options的fill给定长边形内部的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>>draw.rectangle((0,0,100,200), fill = (255,0,0))
>>> draw.rectangle([100,200,300,500],fill = (0,255,0))
>>>draw.rectangle([(300,500),(600,700)], fill = (0,0,255))
>>>im01.show()
例:
from PIL import Image
img = Image.open(‘D:\image_for_test\Spee.jpg’)
print(“初始尺寸”,img.size)
print(“默认缩放NEARESET”,img.resize((128,128)).size)
print(“BILINEAR”,img.resize((127,127),Image.BILINEAR).size)
print(“BICUBIC”,img.resize((126,126),Image.BICUBIC).size)
print(“ANTIALIAS”,img.resize((125,125),Image.ANTIALIAS).size)
双线性差值,只考虑了旁边4个的点,并且是用的加权平均
双三次插值,考虑了16个点,做加权平均
抗锯齿插值,会考虑所有影响到的像素点,反正很大
1. 函数
用 OpenCV 标注 bounding box 主要用到下面两个工具——cv2.rectangle() 和 cv2.putText()。用法如下:
# cv2.rectangle()
# 输入参数分别为图像、左上角坐标、右下角坐标、颜色数组、粗细
cv2.rectangle(img, (x,y), (x+w,y+h), (B,G,R), Thickness)
# cv2.putText()
# 输入参数为图像、文本、位置、字体、大小、颜色数组、粗细
cv2.putText(img, text, (x,y), Font, Size, (B,G,R), Thickness)
2. Example
import cv2
fname = '001.jpg'
img = cv2.imread(fname)
# 画矩形框
cv2.rectangle(img, (212,317), (290,436), (0,255,0), 4)
# 标注文本
font = cv2.FONT_HERSHEY_SUPLEX
text = '001'
cv2.putText(img, text, (212, 310), font, 2, (0,0,255), 1)
cv2.imwrite('001_new.jpg', img)