opencv和pil

在对图像进行resize操作的时候

img_pil = Image.open("1.jpg")
img_pil = img_pil.resize((512, 512))
print(type(img_pil))       # <class 'PIL.Image.Image'>

img_cv2 = cv2.imread("1.jpg")
img_cv2 = cv2.resize(img_cv2,(512, 512))     
print(type(img_cv2))              # <class 'numpy.ndarray'>
pil到cv2的转换

img_from_pil = np.asarray(img_pil)

cv2到pil的转换

img_from_cv2 = Image.fromarray(img_cv2)

用pil对不同尺寸大小的图片进行合并

from PIL import Image
import cv2

def blend_two_images():
  img1 = Image.open(r"C:\Users\yu\Downloads\meng_1.jpeg")
  #658  439
  img1 = img1.convert('RGBA')
  width = 492
  heigth = 488
  dim = (width,heigth)
  #img1 = cv2.resize(img1,dim,interpolation = cv2.INTER_AREA)
  img1 = img1.resize((492,488))

  img2 = Image.open("D:\c3d\zh.png")#492   488
  img2 = img2.convert('RGBA')
  img = Image.blend(img1, img2, 0.3)
  img.show()
  img.save("blend.png")

  return
blend_two_images()

上一篇:leetcode学习笔记 118杨辉三角形


下一篇:transforms常用函数简介