首页 > 解决方案 > 有没有办法在 jupyter notebook 的循环中使用魔法运行 python 程序?

问题描述

我在 Mac 上使用 Jupyter Notebook。我有一个从 github 下载的 python 程序,它获取图像并对其进行转换并返回原始图像和他转换的图像。我正在尝试将此程序用于文件夹内的一批图像。

这是我尝试过的:

directory = r'/some/directory'
for filename in os.listdir(directory):
    if filename.endswith(".jpg"): 
        !python /location/folder/imagemodifier.py -i ('/some/directory'+filename) -iter 50 -coeff 2 -n_comp 100

我收到此错误:/bin/sh: -c: line 0: syntax error near unexpected token `('

我也试过:

directory = r'/some/directory'
for filename in os.listdir(directory):
    #only use images
    if filename.endswith(".jpg"): 
        imagetotransform = ('/location/folder/'+filename)
        !python /location/folder/imagemodifier.py -i imagetotransform -iter 10 -coeff 2 -n_comp 100

我收到以下错误: AttributeError: 'NoneType' object has no attribute 'shape'

我也尝试用它做一个功能。

def imagefunction(image):
    !python /location/folder/imagemodifier.py -i image -iter 10 -coeff 2 -n_comp 100    

imagefunction("image.jpg")

我得到:'NoneType'对象没有属性'shape'

到目前为止,它似乎工作的唯一方法是,如果我只为一个图像运行 python 程序并将其路径放在那里。像这样:

%matplotlib inline
!python /location/folder/imagemodifier.py -i 'image.jpg' -iter 500 -coeff 2 -n_comp 100


标签: python

解决方案


推荐阅读