Python OpenCV - 在同一图像文件上执行多个命令行脚本

时间:2017-08-23 20:02:36

标签: python opencv3.0

尝试将已保存的已映像的路径从stdout从一个命令行进程传递到另一个命令行进程。图像不会关闭,直到整个脚本完成。 "旋转"保存到path1.rotated.png " myCrop"使用path1.rotated.png作为输入" myCrop"空图像的错误。如果我通过命令行手动执行

,一切都有效
import glob
import os
import ntpath
import random
import sys
import random
import math
import json
import cv2
import subprocess
import re
import datetime as dt
from collections import defaultdict


def rotate(fn):    
#rotate    
    try:
        p = subprocess.run(['python','rotate.py','-i',fn], 
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0)
        fn = p.stdout.decode()
        return p.stdout.decode()      
    except:
        print('could not rotate ',fn, p.stderr.decode())


def crop(fn):
#crop   
    fn = fn.replace('\\','\\\\') 
    try:
        print(fn)        
        print('cropping',fn)
        p = subprocess.run(['python','myCrop.py','-i',fn], 
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) 
        fn = p.stdout.decode()
        return p.stdout.decode()
    except:
        print('could not crop ',fn)

def process_image(o, f):
    fn = o + "\\" + f
    rotated = rotate(fn)
    print("rotated as :",rotated)
    cropped = crop(rotated)
    print("cropped as :", cropped)

if __name__ == '__main__':
    fn = open(str(dt.datetime.now()).replace(":","-") + '.results.txt','w')
    if len(sys.argv) == 2 and '*' in sys.argv[1]:
        files = glob.glob(sys.argv[1])
        random.shuffle(files)
    else:
    files = sys.argv[1:]

for path in files:
    #out_path = path.replace('.jpg', '.crop.png')
    dirName  = os.path.dirname(path) # + '\\modified\\' 
    fileName = os.path.basename(path)
    #fileName = fileName.replace('.png', '.modified.png') 
    #out_path = dirName + fileName # .png as input
    #newImagePath = path.replace(".png", ".modified.png")
    #if os.path.exists(out_path): continue
    try:
        process_image(dirName,fileName)
    except Exception as e:
        print('%s %s' % (path, e))
    fn.close()

0 个答案:

没有答案