【问题标题】:how to properly read a PPM file using Python如何使用 Python 正确读取 PPM 文件
【发布时间】:2014-05-17 20:48:16
【问题描述】:

这是我的总体说明

使用 0 到 255 范围内的整数值编写一个表示 RGB 颜色的 Color 类。您的类必须: 放在 image.py 中 提供一个构造函数,从客户端接受红色、绿色和蓝色通道的值并存储这些值 提供返回红、绿、蓝通道值的公共方法

编写一个表示 PPM 图像的 PortablePixmap 类。你的班级必须: 放在 image.py 中 提供一个构造函数,该构造函数接受来自客户端的幻数、宽度、高度、最大颜色值和像素数据并存储这些值 将像素数据存储为 Color 对象的列表(或列表的列表) 提供一个返回 PPM 图像的字符串表示形式的公共方法

编写一个 read_ppm 函数,该函数打开一个 PPM 图像文件,读取其内容,并返回一个包含其内容的 PortablePixmap 对象。您的功能必须: 放在 image.py 中 读取 PPM 映像文件的内容 对 PPM 图像文件的格式不敏感 如果预期和提供的像素数不同,则退出并出错

编写一个测试您的 read_ppm 函数的主函数。你的函数必须放在 main.py 中

这就是我目前所拥有的

class Color:
# constructor takes in values from client and stores them
def __init__(self, red, green, blue): 


    # checks that type of arg == int: raises exception otherwise 
    if (isinstance(red, int) and isinstance(green, int) and isinstance(blue, int)):     
        print("good stuff, indeed integers")
    else:   
        raise TypeError("Argument must be an integer.")

    # checks if values are between 0 and 225 
    if red < 0 or red > 225: 
        print("0 < rgb values < 225")
    elif green < 0 or green > 225:
        print("0 < rgb values < 225") 
    elif blue < 0 or blue > 225:
        print("0 < rgb values < 225")

    # instance variables (RGB values)
    self._red = red 
    self._green = green
    self._blue = blue 


# methods that reuturn RGB values
def returnRed(self): 
    return self._red 

def returnGreen(self):
    return self._green

def returnBlue(self):
    return self._blue


'''class that represents a PPM image'''
class PortablePixmap:
    def __init__(self, magic_number, width, height, max_color_value, pixel_data):
        self._magic_number = magic_number
        self._width = width
        self._height = height
        self._max_color_value = max_color_value
        self._pixel_data = pixel_data


    def __str__(self):
        s = self._magic_number
        s += '\n' + str(self._width)
        s += ' ' + str(self._height)
        s += '\n' + str(self._max_color_value)
        for pixel in self._pixel_data:
            s += ' ' + str(pixel[0])
            s += ' ' + str(pixel[1])
            s += ' ' + str(pixel[2])

        return s

我有几个问题需要澄清.. 1. 我是否正确地创建了 Color 类? 2. 我什至需要专门在该类中提出任何异常?我们最终将从一个包含所有内容的文件中读取,但不一定在它自己的单独行上。

我真的只是想知道我是否正确地处理了这件事。这些说明似乎是逐步进行的,但我并不真正了解一切是如何联系起来的,所以我担心我做的太多或太少。

提前致谢

【问题讨论】:

    标签: python tokenize ppm


    【解决方案1】:

    从规范中并不清楚您需要检查值,并且您的检查仅在某些情况下引发异常,否则会导致副作用(打印);从重用的角度来看,如果有的话,我宁愿只有例外。除了缩进错误(我假设它只是在这里,而不是在你的源代码中)之外,Color 类看起来可以满足需求,尽管它们对于访问器来说是非常不符合标准的;可能有人受过Java培训。

    文档字符串应该在 PortablePixmap 类中,而不是在它之上。

    最引人注目的是要求您的类对 PPM 的格式不敏感 将像素存储为 8 位无符号 RGB 的组合。这使得支持所有 PPM 成为不可能,因为它们支持 16 位值(注意 PPM format 中的 maxval 字段)。

    您的 PortablePixmap 类也不使用 Color 类:“将像素数据存储为 Color 对象列表(或列表列表)”。这个要求迫使实施效率相当低,但我想整个事情都是一个练习。您需要从像素数据字符串中提取 RGB 三元组。这也是您需要指定的一项检查的地方;验证是否有正确数量的像素。如果失败,人们会期望 ValueError 异常。

    如果我正在写这种东西,我可能会使用 slots 来减少诸如 Color 之类的类的内存使用,arrays 来处理大量有限范围的数值,并且可能使用 properties 来使存储透明无需使用笨拙的 getter 方法。 splitjoin 可以更轻松地处理像素集合。

    【讨论】:

    • 哇,非常感谢您详细说明项目描述!我现在看到 Color 类对于这个项目没有当前用途。它将在以后的添加中使用。此外,Python 是我学习/正在学习的第一门语言。我只有 getter 方法的原因是因为 1. 它涵盖了要求 2. 我在弄清楚类的总体目的时遇到了问题 我最终在颜色类中取出了异常(以及 int 值'check'-真的只需打印语句)并按照您的建议实现属性。我认为它最终会是有益的。
    • 我关于属性的注释不是建议遵循指定的任务!如果您根据这些要求评分,PortablePixmap必须使用 Color,并且 Color必须包含访问器方法。
    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多