import cv2
#Image loading
im = cv2.imread('data/src/lena.jpg')
#Black and white image reading
im_gray = cv2.imread('data/src/lena.jpg', cv2.IMREAD_GRAYSCALE)
im_gray = cv2.imread('data/src/lena.jpg', 0)
#Color space conversion
imrgb = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2RGB)
#Other conversions
#https://docs.opencv.org/3.4.0/d7/d1b/group__imgproc__misc.html#ga4e0972be5de079fed4e3a10e24ef5ef0
#Image display
cv2.imshow('image', im)
#Save to image file
cv2.imwrite('data/dst/lena_opencv_red.jpg', im)
#Saved image quality specification
cv2.imwrite('data/dst/lena_opencv_red_low.jpg', im, [cv2.IMWRITE_JPEG_QUALITY, 50])
#Image size acquisition
h,w,c = img.shape
#Draw a shape on the image
cv2.rectangle(img, (50, 150), (125, 200), (0, 0, 255), thickness=-1)
cv2.line(img, (50, 60), (125, 10), (0, 255, 255), thickness=4, lineType=cv2.LINE_AA)
cv2.arrowedLine(img, pt1, pt2, color, thickness=1, lineType=cv2.LINE_8, shift=0, tipLength=0.1)
cv2.circle(img, center, radius, color, thickness=1, lineType=cv2.LINE_8, shift=0)
cv2.circle(img, (240, 35), 20, (0, 0, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.ellipse(img, box, color, thickness=1, lineType=cv2.LINE_8)
cv2.ellipse(img, ((240, 105), (20, 50), 30), (0, 0, 0), thickness=-1)
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness=1, lineType=cv2.LINE_8, shift=0)
cv2.ellipse(img, (240, 175), (10, 25), 30, 0, 270, (0, 0, 0), thickness=-1)
cv2.drawMarker(img, position, color, markerType=cv2.MARKER_CROSS, markerSize=20, thickness=1, line_type=cv2.LINE_8)
cv2.drawMarker(img, (337, 20), (0, 255, 0), markerType=cv2.MARKER_TILTED_CROSS, markerSize=15)
cv2.polylines(img, pts, isClosed, color, thickness=1, lineType=cv2.LINE_8, shift=0)
pts = np.array(((300, 80), (300, 130), (335, 130)))
cv2.polylines(img, [pts], True, (255, 255, 255), thickness=2)
cv2.fillPoly(img, pts, color, lineType=cv2.LINE_8, shift=0)
pts = np.array(((335, 80), (375, 80), (375, 130)))
cv2.fillPoly(img, [pts], (0, 0, 0))
cv2.fillConvexPoly(img, pts, (0, 0, 0))
cv.putText(img, text, org, fontFace, fontScale, color, thickness=1, lineType=cv2.LINE_8)
cv2.putText(img, 'test', (300, 170), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), thickness=2)
#Japanese input
import numpy as np
from PIL import ImageFont, ImageDraw, Image
#Set all elements to 0 and 200 so that the background is black*500 can store 3 RGB values,
#The data type of the element is 8bit(1byte)An array of unsigned integers(background image)make.
img = np.zeros((200,500,3),np.uint8)
#Color to display
b,g,r,a = 0,255,0,0 #B(Blue)・ G(Green)・ R(Red)·Ah(Transparency)
##MS Gothic Standard
fontpath ='C:\Windows\Fonts\msgothic.ttc' #C on Windows 10:\Windows\Fonts\Below are the fonts.
font = ImageFont.truetype(fontpath, 32) #Font size is 32
img_pil = Image.fromarray(img) #Each value of the array is 8bit(1byte)Integer type(0~255)To PIL Image.
draw = ImageDraw.Draw(img_pil) #Create draw instance
position = (50, 100) #Text display position
draw.text(position, message, font = font , fill = (b, g, r, a) ) #Write text in draw fill:Color BGRA(RGB)
img = np.array(img_pil) #Convert PIL to array
IT memos for non-IT industries
Recommended Posts