因为Python太慢了,numpy试图巧妙地应用某些操作(参见broadcasting)。使用numpy提高效率的第一个经验法则是让numpy为您处理迭代(或者换一种方式don't write your own ^{} loops)。上面代码中有趣的一点是,它在执行“示例图像处理”时只部分遵循此规则,因此该行的性能对给定给reshape的参数具有极端依赖性。
下一个大的numpy终结之谜:为什么newbyteorder()看起来像return an array,而documented返回一个dtype。如果要用dst.pixels=dst.pixels.byteswap(True).newbyteorder()转换为本机endian,则这是相关的。
以下操作仅依赖于numpy来加载图像,该图像可以是8位或16位原始PGM/PPM。我还展示了两种不同的方式来查看图像。使用PIL(
import Image
)的方法要求首先将数据转换为8位。使用说明
我最终弄明白了“它是如何决定endianness的”——它实际上是将图像作为big endian(而不是原生的)存储在内存中。这个方案可能会减慢任何非平凡的图像处理速度——尽管Python的其他性能问题可能会使这个问题变得微不足道(见下文)。
我问了一个关于持久性的问题。我还遇到了一些与endianness相关的有趣的混淆,因为我是通过使用
pnmdepth 65535
对图像进行预处理来测试endianness的,这对测试endianness是不好的(就其本身而言),因为低字节和高字节可能最终相同(我没有立即注意到,因为print(array)
输出十进制)。我也应该用pnmgamma
来拯救我自己。因为Python太慢了,} loops )。上面代码中有趣的一点是,它在执行“示例图像处理”时只部分遵循此规则,因此该行的性能对给定给
numpy
试图巧妙地应用某些操作(参见broadcasting)。使用numpy
提高效率的第一个经验法则是让numpy为您处理迭代(或者换一种方式don't write your own ^{reshape
的参数具有极端依赖性。下一个大的
numpy
终结之谜:为什么newbyteorder()
看起来像return an array,而documented返回一个dtype
。如果要用dst.pixels=dst.pixels.byteswap(True).newbyteorder()
转换为本机endian,则这是相关的。关于移植到Python 3的提示:binary input with an ASCII text header, read from stdin
这是一个基于NumPy的泛型PNM/PAM阅读器和PyPNG中的一个未记录的函数。
当然,编写这种图像格式通常不需要库的帮助。。。
您需要一个
"L;16"
模式;但是在加载PGM时,PIL的模式似乎是"L"
硬编码到File.c中。如果你想读16位的PGM,就必须write your own decoder。然而,16位图像支持仍然显得有些不稳定:
我认为PIL能够读取16位的图像,但实际存储和操作它们仍然是实验性的。
看,它只是将
0xCAFE
值解释为0xFE
,这并不完全正确。相关问题 更多 >
编程相关推荐