Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以优化此图像处理代码以使用更少的内存?_Python_Image Processing_Memory Management_S Expression_Python Imaging Library - Fatal编程技术网

Python 是否可以优化此图像处理代码以使用更少的内存?

Python 是否可以优化此图像处理代码以使用更少的内存?,python,image-processing,memory-management,s-expression,python-imaging-library,Python,Image Processing,Memory Management,S Expression,Python Imaging Library,我有一个python函数,它接受字符串s表达式,比如“(add(sub 10 5)5)”,其中“add”和“sub”实际上是图像处理函数,并计算和创建字符串中表示的图像。图像处理函数获取常量、变量或其他图像(表示为向量列表),并返回以相同方式表示的图像。PIL用于将表示为向量列表的图像转换为图像文件 为了计算前缀符号s-expressions,我将s-expr转换为一个列表,将其反转,并迭代标记,直到找到一个函数,此时执行图像处理函数,生成的图像替换列表中的函数及其参数。这样做直到列表中只剩下一

我有一个python函数,它接受字符串s表达式,比如“(add(sub 10 5)5)”,其中“add”和“sub”实际上是图像处理函数,并计算和创建字符串中表示的图像。图像处理函数获取常量、变量或其他图像(表示为向量列表),并返回以相同方式表示的图像。PIL用于将表示为向量列表的图像转换为图像文件

为了计算前缀符号s-expressions,我将s-expr转换为一个列表,将其反转,并迭代标记,直到找到一个函数,此时执行图像处理函数,生成的图像替换列表中的函数及其参数。这样做直到列表中只剩下一个元素,即最终图像

图像处理功能很简单-大多数对图像中的每个(r、g、b)值执行一些数学运算

问题是,如果我想为更复杂的表达式制作尺寸合适的图像,我的计算机就会停止工作。是否可以对此进行优化以使用更少的内存

def createImage(self, sexpr, filename, (picWidth, picHeight)):
    """Use the image processing functions in ImgProcessing
       to create an image from the procedural information
       contained in the s-expression."""

    img = Image.new("RGB",(picWidth,picHeight),(255,255,255))
    ip = ImgProcessing(picWidth,picHeight)

    # Split s-expression into list of tokens and reverse
    sList = sexpr.replace("(","").replace(")","").split()
    sList.reverse()

    while len(sList) > 1:

        for index,token in enumerate(sList):
            # If token is an image processing function
            if type(token) == str and self.FuncSet.has_key(token):
                # If this function takes one argument
                if self.FuncSet[token] == 1:
                    rawImage = eval("ip." + token + "(" + "\"" + str(sList[index-1]) +
                                    "\"" + ")")
                    sList = sList[:index-1] + [rawImage] + sList[index+1:]
                    break
                # If this function takes two arguments
                elif self.FuncSet[token] == 2:
                    rawImage = eval("ip." + token + "(" + "\"" + str(sList[index-1]) +
                                    "\"" + "," + "\"" + str(sList[index-2]) + "\"" +
                                    ")")
                    sList = sList[:index-2] + [rawImage] + sList[index+1:]
                    break

    img.putdata(sList[0])
    img.save(filename)
可以告诉你程序大部分时间都花在哪里

其次,
str(sList[index-1])
是否将图像转换为字符串?
ip.token(…)
是否返回图像?如果是这样,您将在字符串和图像之间进行多次转换。那可能会很慢

这可能有助于改变

rawImage = eval("ip." + token + "(" + "\"" + str(sList[index-1]) +
                                    "\"" + ")")
差不多

getattr(ip,token)(sList[index-1])
当然,这取决于
ip.token
期望的参数类型。我在谷歌上找不到任何关于
ImgProcessing
的信息。这是自定义类吗?如果是这样的话,也许有助于更多地解释它是如何工作的。
如果
ip.token
可以从拍摄字符串更改为拍摄图像,那可能是一个很大的改进。

根据我的经验,在纯Python或PIL中对大图像逐像素执行的任何操作都会像一月份的糖浆一样缓慢。考虑把低级的东西移动到用C编写的Python扩展中。我使用OpenCV,但是它需要一些学习。

我不认为跳进C是正确的答案,直到你已经尽可能地优化和优化了你的Python代码。在我看来,过早跳转到较低级别的语言只是过早优化的另一种形式。有时你不得不使用
eval
,但99%的实际
eval
用法(包括这一用法)是极不合适的。如果你认为你必须使用
eval
,一个很好的经验法则是你错了。在使用
eval
之前,由于您必须使用
eval
,请学习课程CS362,确定您确实必须使用
eval
的少数情况,并将您完成的考试学分和申请费邮寄给协会,以便在您真正需要的极少数情况下管理访问
eval
,在您真正需要的极少数情况下获得使用
eval
的书面许可。