python圖片剪裁代碼(圖片按四個(gè)點(diǎn)坐標(biāo)剪裁)
用了兩種方法保存圖片,opencv和Image,實(shí)踐證明opencv非???/p>
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項(xiàng)目\\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')
補(bǔ)充知識(shí):python實(shí)現(xiàn)固定區(qū)域截圖,實(shí)現(xiàn)錄屏截圖
1、實(shí)現(xiàn)固定區(qū)域截圖及保存
如果不設(shè)置區(qū)域默認(rèn)是全屏保存
from PIL import ImageGrab
# 參數(shù)說(shuō)明
# 第一個(gè)參數(shù) 開(kāi)始截圖的x坐標(biāo)
# 第二個(gè)參數(shù) 開(kāi)始截圖的y坐標(biāo)
# 第三個(gè)參數(shù) 結(jié)束截圖的x坐標(biāo)
# 第四個(gè)參數(shù) 結(jié)束截圖的y坐標(biāo)
bbox = (760, 0, 1160, 1080)
im = ImageGrab.grab(bbox)
# 參數(shù) 保存截圖文件的路徑
im.save('zy.png')
2、實(shí)現(xiàn)屏幕的視頻錄制
可以用上面程序的bbox設(shè)置一下錄屏區(qū)域
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圖片剪裁代碼(圖片按四個(gè)點(diǎn)坐標(biāo)剪裁)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python函數(shù)中不定長(zhǎng)參數(shù)的寫法
今天小編就為大家分享一篇關(guān)于Python函數(shù)中不定長(zhǎng)參數(shù)的寫法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
Python使用OpenCV對(duì)圖像進(jìn)行縮放功能
這篇文章主要介紹了Python使用OpenCV對(duì)圖像進(jìn)行縮放功能,文中給大家提到了兩種調(diào)用方式及OpenCV中pryUp和pryDown的用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
pycharm通過(guò)ssh遠(yuǎn)程連接服務(wù)器并運(yùn)行代碼詳細(xì)圖文
在運(yùn)行項(xiàng)目的過(guò)程中,由于自己電腦GPU不夠,通常需要將項(xiàng)目放到服務(wù)器上運(yùn)行,這時(shí)就會(huì)遇到如何將pycharm和服務(wù)器進(jìn)行連接,下面這篇文章主要給大家介紹了關(guān)于pycharm通過(guò)ssh遠(yuǎn)程連接服務(wù)器并運(yùn)行代碼的相關(guān)資料,需要的朋友可以參考下2024-03-03
Pytorch自動(dòng)求導(dǎo)函數(shù)詳解流程以及與TensorFlow搭建網(wǎng)絡(luò)的對(duì)比
PyTorch是一個(gè)開(kāi)源的Python機(jī)器學(xué)習(xí)庫(kù),基于Torch,用于自然語(yǔ)言處理等應(yīng)用程序。2017年1月,由Facebook人工智能研究院(FAIR)基于Torch推出了PyTorch,這篇文章主要介紹了Pytorch自定義自動(dòng)求導(dǎo)函數(shù),以及PyTorch與TensorFlow搭建網(wǎng)絡(luò)的對(duì)比2021-11-11

