乔力奎
2021-07-02 21:22
采纳率: 100%
浏览 123

Pycharm配置运行参数设置,这参数怎么设置呢?

img

img

img

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • CSDN专家-HGJ 2021-07-03 00:51
    已采纳

    命令行传参,运行时你需要将设定的相应参数传递进命令行中才可以。在代码中有二两处要改一下,一是在84行,for c in xrange(len(scheme)),xrange函数是python2中的函数,如果你运行python3的话,将其改为range()函数。二是在第72行和第85行两处构造字符串列表时写法不正确,用python3中的f-string进行改写。在运行时,输入图片放在imgs文件夹根目录,参数file_pattern,用已有文件的名,不带后缀名,test为输出文件子目录,假如有图片wcb1.jpg,wcb.jpg2,那么用命令test_color.py --in_dir imgs --file_pattern wcb --file_indices 1 2 --out_subdir test,即可运行。修改的代码如下,供你作参考并加以修改完善。

    import argparse
    import os
    from colorsys import hsv_to_rgb
    import numpy as np
    import torch
    from PIL import Image
    parser = argparse.ArgumentParser()
    parser.add_argument("--in_dir", type=str, required=True)
    parser.add_argument("--file_pattern", type=str, required=True)
    parser.add_argument("--file_indices", type=int, nargs="+", required=True)
    parser.add_argument("--out_subdir", type=str, default="colour_change")
    args = parser.parse_args()
    
    
    def change_colours(img, input_colours, colours):
      h, w, c = img.shape
      assert (c == 3)
      assert (img.dtype == np.uint8)
      new_img = np.copy(img)
      for i, in_c in enumerate(input_colours):
        out_c = colours[i]
        in_c_np = np.array(in_c).reshape((1, 1, 3))
        new_img[(img == in_c_np).sum(axis=2) == 3] = out_c
      return new_img
    
    
    N = 10
    hues = torch.linspace(0.0, 1.0, N + 1)[0:-1]  # ignore last one
    input_colours = [list((np.array(hsv_to_rgb(hue, 0.5, 0.8)) * 255.).astype(np.uint8)) for hue in hues]
    # Colour schemes
    scheme = []
    # basic colours
    colours = [
        [0, 0, 0],
        [177, 177, 177],
        [250, 0, 0],
        [0, 250, 0],
        [0, 0, 250],
        [250, 250, 0],
        [250, 0, 250],
        [0, 250, 250],
        [250, 100, 0],
        [0, 100, 250]
    ]
    scheme.append(colours)
    # Add functional colours
    saturations = [0.5, 0.6, 0.7, 0.8]
    values = [0.5, 0.6, 0.7, 0.8]
    for s in saturations:
      for v in values:
        hues = torch.linspace(0.0, 1.0, N + 1)[0:-1]  # ignore last one
        colours = [list((np.array(hsv_to_rgb(hue, s, v)) * 255.).astype(np.uint8)) for hue in hues]
        scheme.append(colours)
    # Changes the colour scheme of certain images
    out_dir = os.path.join(args.in_dir, args.out_subdir)
    if not os.path.exists(out_dir):
      os.makedirs(out_dir)
    for i in args.file_indices:
      fname = f'{args.file_pattern}{i}.jpg'
      fpath = os.path.join(args.in_dir, fname)
      print("fpath: %s..." % fpath)
      img = np.array(Image.open(fpath))
      for c, curr_colours in enumerate(scheme):
        new_img = change_colours(img, input_colours, curr_colours)
        new_img = Image.fromarray(new_img)
        new_img.save(os.path.join(out_dir, "c_%d_%s" % (c, fname)))
    # make composites
    for c in range(len(scheme)):
      fnames = [f'{args.file_pattern}{i}.jpg' for i in args.file_indices]
      fnames = ["c_%d_%s" % (c, fname) for fname in fnames]
      fnames = [os.path.join(out_dir, fname) for fname in fnames]
      images = map(Image.open, fnames)
      widths, heights = zip(*(i.size for i in images))
      total_width = sum(widths)
      max_height = max(heights)
      new_im = Image.new('RGB', (total_width, max_height))
      x_offset = 0
      for im in images:
        new_im.paste(im, (x_offset, 0))
        x_offset += im.size[0]
      new_im.save(os.path.join(out_dir, "composite_%d.png" % c))
    
    已采纳该答案
    打赏 评论
  • CSDN专家-HGJ 2021-07-02 22:52

    命令行传参,运行时你需要将设定的相应参数传递进命令行中才可以。参考一下argparser的使用讲解,https://www.jb51.net/article/190589.htm

    1 打赏 评论
  • 乔力奎 2021-07-02 23:23

    按照你们的方法没有解决呢,这是我的完整代码,大佬有时间帮忙看一下,谢谢啦

    import argparse
    import os
    from colorsys import hsv_to_rgb
    
    import numpy as np
    import torch
    from PIL import Image
    
    parser = argparse.ArgumentParser()
    parser.add_argument("--in_dir", type=str, required=True)
    parser.add_argument("--file_pattern", type=str, required=True)
    parser.add_argument("--file_indices", type=int, nargs="+", required=True)
    parser.add_argument("--out_subdir", type=str, default="colour_change")
    
    args = parser.parse_args()
    
    
    def change_colours(img, input_colours, colours):
      h, w, c = img.shape
      assert (c == 3)
      assert (img.dtype == np.uint8)
      new_img = np.copy(img)
    
      for i, in_c in enumerate(input_colours):
        out_c = colours[i]
        in_c_np = np.array(in_c).reshape((1, 1, 3))
        new_img[(img == in_c_np).sum(axis=2) == 3] = out_c
    
      return new_img
    
    
    N = 10
    
    hues = torch.linspace(0.0, 1.0, N + 1)[0:-1]  # ignore last one
    input_colours = [list((np.array(hsv_to_rgb(hue, 0.5, 0.8)) * 255.).astype(
      np.uint8)) for hue in hues]
    
    # Colour schemes
    scheme = []
    
    # basic colours
    colours = [
      [0, 0, 0],
      [177, 177, 177],
      [250, 0, 0],
      [0, 250, 0],
      [0, 0, 250],
      [250, 250, 0],
      [250, 0, 250],
      [0, 250, 250],
      [250, 100, 0],
      [0, 100, 250]
    ]
    scheme.append(colours)
    
    # Add functional colours
    saturations = [0.5, 0.6, 0.7, 0.8]
    values = [0.5, 0.6, 0.7, 0.8]
    for s in saturations:
      for v in values:
        hues = torch.linspace(0.0, 1.0, N + 1)[0:-1]  # ignore last one
        colours = [list((np.array(hsv_to_rgb(hue, s, v)) * 255.).astype(
          np.uint8)) for hue in hues]
        scheme.append(colours)
    
    # Changes the colour scheme of certain images
    out_dir = os.path.join(args.in_dir, args.out_subdir)
    if not os.path.exists(out_dir):
      os.makedirs(out_dir)
    
    for i in args.file_indices:
      fname = args.file_pattern % i
      fpath = os.path.join(args.in_dir, fname)
      print("fpath: %s..." % fpath)
      img = np.array(Image.open(fpath))
    
      for c, curr_colours in enumerate(scheme):
        new_img = change_colours(img, input_colours, curr_colours)
    
        new_img = Image.fromarray(new_img)
        new_img.save(os.path.join(out_dir, "c_%d_%s" % (c, fname)))
    
    # make composites
    for c in xrange(len(scheme)):
      fnames = [args.file_pattern % i for i in args.file_indices]
      fnames = ["c_%d_%s" % (c, fname) for fname in fnames]
      fnames = [os.path.join(out_dir, fname) for fname in fnames]
    
      images = map(Image.open, fnames)
      widths, heights = zip(*(i.size for i in images))
    
      total_width = sum(widths)
      max_height = max(heights)
    
      new_im = Image.new('RGB', (total_width, max_height))
    
      x_offset = 0
      for im in images:
        new_im.paste(im, (x_offset, 0))
        x_offset += im.size[0]
    
      new_im.save(os.path.join(out_dir, "composite_%d.png" % c))
    
    
    1 打赏 评论
  • CSDN专家-黄老师 2021-07-02 21:43

    你用sys.argv获取命令行参数列表,看看能不能成功获取,如果能获取,说明你代码的获取方式有问题

    打赏 评论

相关推荐 更多相似问题