如何在python代码中指定输入文件位置和输出位置

如何在python代码中指定输入文件位置和输出位置,python,Python,我想将一个图像文件转换成另一个,我的代码会这样做,但为此,我必须在终端中提供源文件位置和目标文件位置,而不是在python代码中同时提供源文件名和目标文件名 import os import glob import time; from io import BytesIO import numpy as np from PIL import Image import tensorflow as tf import sys

我想将一个图像文件转换成另一个,我的代码会这样做,但为此,我必须在终端中提供源文件位置和目标文件位置,而不是在python代码中同时提供源文件名和目标文件名

    import os
    import glob
    import time;
    from io import BytesIO

    import numpy as np
    from PIL import Image

    import tensorflow as tf
    import sys
    import datetime

    date_string = time.strftime("%Y-%m-%d-%H:%M")


    class DeepLabModel(object):
      """Class to load deeplab model and run inference."""

      INPUT_TENSOR_NAME = 'ImageTensor:0'
      OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
      INPUT_SIZE = 513
      FROZEN_GRAPH_NAME = 'frozen_inference_graph'

      def __init__(self, tarball_path):
        """Creates and loads pretrained deeplab model."""
        self.graph = tf.Graph()

        graph_def = None
        graph_def = tf.GraphDef.FromString(open(tarball_path + "/frozen_inference_graph.pb", "rb").read()) 

        if graph_def is None:
          raise RuntimeError('Cannot find inference graph in tar archive.')

        with self.graph.as_default():
          tf.import_graph_def(graph_def, name='')

        self.sess = tf.Session(graph=self.graph)

      def run(self, image):
        """Runs inference on a single image.

        Args:
          image: A PIL.Image object, raw input image.

        Returns:
          resized_image: RGB image resized from original input image.
          seg_map: Segmentation map of `resized_image`.
        """
        start = datetime.datetime.now()

        width, height = image.size
        resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
        target_size = (int(resize_ratio * width), int(resize_ratio * height))
        resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
        batch_seg_map = self.sess.run(
            self.OUTPUT_TENSOR_NAME,
            feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
        seg_map = batch_seg_map[0]

        end = datetime.datetime.now()

        diff = end - start
        print("Time taken to evaluate segmentation is : " + str(diff))

        return resized_image, seg_map

    def drawSegment(baseImg, matImg):
      width, height = baseImg.size
      dummyImg = np.zeros([height, width, 4], dtype=np.uint8)
      for x in range(width):
                for y in range(height):
                    color = matImg[y,x]
                    (r,g,b) = baseImg.getpixel((x,y))
                    if color == 0:
                        dummyImg[y,x,3] = 0
                    else :
                        dummyImg[y,x] = [r,g,b,255]
      img = Image.fromarray(dummyImg)
      img.save(outputFilePath)


    inputFilePath = sys.argv[1]
    outputFilePath = sys.argv[2]

    if inputFilePath is None or outputFilePath is None:
      print("Bad parameters. Please specify input file path and output file path")
      exit()

    modelType = "mobile_net_model"
    if len(sys.argv) > 3 and sys.argv[3] == "1":
      modelType = "xception_model"

    MODEL = DeepLabModel(modelType)
    print('model loaded successfully : ' + modelType)

    def run_visualization(filepath):
      """Inferences DeepLab model and visualizes result."""
      try:
        print("Trying to open : " + sys.argv[1])
        # f = open(sys.argv[1])
        jpeg_str = open(filepath, "rb").read()
        orignal_im = Image.open(BytesIO(jpeg_str))
      except IOError:
        print('Cannot retrieve image. Please check file: ' + filepath)
        return

      print('running deeplab on image %s...' % filepath)
      resized_im, seg_map = MODEL.run(orignal_im)

      # vis_segmentation(resized_im, seg_map)
      drawSegment(resized_im, seg_map)

    run_visualization(inputFilePath)
这是我的密码

当我在终端中发出此命令时,此代码正在工作

   python abcd.py input_file/front1566192748.jpg output_file/testing1.png
与此相反,我想提及python代码中的输入文件和输出文件

所以要运行我喜欢使用的代码

 python abcd.py
只是


感谢您提前提供的帮助。

替换以下两行:

inputFilePath = sys.argv[1]
outputFilePath = sys.argv[2]

你完成了

编辑:您还必须删除代码其余部分中对sys.argv[1]和sys.argv[2]的所有引用,以避免出现错误

inputFilePath = 'input_file/front1566192748.jpg'
outputFilePath = 'output_file/testing1.png'