python图片剪裁代码(图片按四个点坐标剪裁)
用了两种方法保存图片,opencv和Image,实践证明opencv非常快
from PIL import Image import os import cv2 import time import matplotlib.pyplot as plt def label2picture(cropImg,framenum,tracker): pathnew ="E:\\img2\\" # cv2.imshow("image", cropImg) # cv2.waitKey(1) if (os.path.exists(pathnew + tracker)): cv2.imwrite(pathnew + tracker+'\\'+framenum + '.jpg', cropImg,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) else: os.makedirs(pathnew + tracker) cv2.imwrite(pathnew + tracker+'\\'+framenum + '.jpg', cropImg,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) f = open("E:\\hypotheses.txt","r") lines = f.readlines() for line in lines: li = line.split(',') print(li[0],li[1],li[2],li[3],li[4],li[5]) filename = li[0]+'.jpg' img = cv2.imread("E:\\DeeCamp\\img1\\" + filename) crop_img = img[int(li[3][:-3]):(int(li[3][:-3]) + int(li[5][:-3])), int(li[2][:-3]):(int(li[2][:-3]) + int(li[4][:-3]))] # print(int(li[2][:-3]),int(li[3][:-3]),int(li[4][:-3]),int(li[5][:-3])) label2picture(crop_img, li[0], li[1]) # # # x,y,w,h = 87,158,109,222 # img = cv2.imread("E:\\DeeCamp\\img1\\1606.jpg") # # print(img.shape) # crop = img[y:(h+y),x:(w+x)] # cv2.imshow("image", crop) # cv2.waitKey(0) # img = Image.open("E:\\DeeCamp\\img1\\3217.jpg") # # cropImg = img.crop((x,y,x+w,y+h)) # cropImg.show() # img = Image.open("E:\\deep_sort-master\\MOT16\\train\\try1\\img1\\"+filename) # print(int(li[2][:-3]),(int(li[2][:-3])+int(li[4][:-3])), int(li[3][:-3]),(int(li[3][:-3])+int(li[5][:-3]))) # #裁切图片 # # cropImg = img.crop(region) # # cropImg.show() # framenum ,tracker= li[0],li[1] # pathnew = 'E:\\DeeCamp\\deecamp项目\\deep_sort-master\\crop_picture\\' # if (os.path.exists(pathnew + tracker)): # # 保存裁切后的图片 # plt.imshow(cropImg) # plt.savefig(pathnew + tracker+'\\'+framenum + '.jpg') # else: # os.makedirs(pathnew + tracker) # plt.imshow(cropImg) # plt.savefig(pathnew + tracker+'\\'+framenum + '.jpg')
补充知识:python实现固定区域截图,实现录屏截图
1、实现固定区域截图及保存
如果不设置区域默认是全屏保存
from PIL import ImageGrab # 参数说明 # 第一个参数 开始截图的x坐标 # 第二个参数 开始截图的y坐标 # 第三个参数 结束截图的x坐标 # 第四个参数 结束截图的y坐标 bbox = (760, 0, 1160, 1080) im = ImageGrab.grab(bbox) # 参数 保存截图文件的路径 im.save('zy.png')
2、实现屏幕的视频录制
可以用上面程序的bbox设置一下录屏区域
from time import sleep from PIL import ImageGrab m=int(input("输入录屏几分钟:")) m=m*60 n=1 while n<m: sleep(0.02) im=ImageGrab.grab() local=(r"%s.jpg"%(n)) im.save(local,'jpeg') n=n+1
以上这篇python图片剪裁代码(图片按四个点坐标剪裁)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)