使用Python 3.x来正确读取ppm文件 [英] Using Python 3.x to properly read ppm files

查看:527
本文介绍了使用Python 3.x来正确读取ppm文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  P3 
400 200
255
255 255 255
0 255
0
0 0 0

我需要说明前4个数字不是像素数据,而是创建图像的重要信息。我已经写了下面的代码,但我不知道如何正确地把前4个数字,用于我的程序的不同部分,然后读取其余的图像数据。

解决方案

你是微型优化。将整个PPM图像读入一个字符串不是非常麻烦。事实证明,我有一个720x540的PPM,所以我用Python打开了它:

  Python 3.4.0默认情况下,2014年3月17日,23:20:09)
[GCC 4.8.2 20140206(prerelease)] on linux
输入help,copyright,credits或license信息。
>>>以ppm开头('ppm / cake.ppm','r'):
... data = ppm.read()
...

因为我没有参数调用 read ,所以 data 现在包含整个图像作为一个字符串。注意这些命令像往常一样立即执行。没有明显的放缓。我可以对它们执行正常的操作:

 >>> values = data.split()
>>>值[0]
'P3'
>>>值[1]
'720'
>>>值[2]
'540'
>>>值[3]
'255'

所以你可以看到它是一个P3编码PPM,720px高,540px宽。请注意,现在 value [3:] 只包含颜色信息(不是标题)。



即使我双倍的信息量我得不到任何的性能下降或滞后,而不采取任何特殊的预防措施:

 >>>值+ =值[3:] 
>>> len(values)
2332805

有很多事情可以处理,甚至更大数据比Python中的数据(例如,使用生成器)更高,而且这个代码在两个桌面(年龄和硬件配置大不相同),现代笔记本电脑和虚拟机上对我也是如此。总之,你不必担心这样的预防措施。只要简单的做一些事情,然后继续用你的PPM图像做一些很酷的事情。 :)



编辑

找到需要一秒的东西。一秒钟。

 >>>编码,高度,宽度,* values = data.split()
>>> rgb = [tuple(values [i:i + 3])in range in(0,len(values),3)]
>>> rgb [:10]
[('255','123','125'),('124','124','126'),('125','127','129' ),('128','129','131'),('130','128','130'),('129','126','128'),('127',' ('133​​','132','135'),('134','135','140'),('139','137','143')]


I am trying to read in data from a file, say:

    P3
    400 200
    255
    255 255 255
    0 255
    0
    0 0 0

And I need to account for the first 4 numbers not being pixel data but important information for the creation of the image. I have written the code below, but I'm not sure how to properly take the first 4 numbers, use them for a different part of my program, and then read the rest of the image data.

解决方案

You're micro-optimizing. Reading an entire PPM image into a string is not "very cumbersome". As it turns out, I have a 720x540 PPM lying around, so I opened it up with Python:

Python 3.4.0 (default, Mar 17 2014, 23:20:09) 
[GCC 4.8.2 20140206 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('ppm/cake.ppm', 'r') as ppm:
...   data = ppm.read()
... 

Since I called read with no argument, data now contains the entire image as a string. Note these commands execute instantaneously as usual. There is no noticeable slowdown. I can perform normal operations on them:

>>> values = data.split()
>>> values[0]
'P3'
>>> values[1]
'720'
>>> values[2]
'540'
>>> values[3]
'255'

So you can see it is a P3-encoded PPM, 720px high, 540px wide. Note that value[3:] now contains just the color information (not the header).

Even if I double the amount of information I get no performance degradation or lag, without taking any special precautions:

>>> values += values[3:]
>>> len(values)
2332805

There's all kinds of things you can do to handle even larger data than this in Python (using generators, for example), and this code handles the same for me on two desktops (of vastly different ages and hardware configurations), a modern laptop, and a virtual machine. In short, you don't need to worry about such precautions. Just do things the easy way and get on to doing something cool with your PPM image. :)

EDIT:

Hey, look, I did find something that takes a second. A second.

>>> encoding, height, width, *values = data.split()
>>> rgb = [tuple(values[i:i+3]) for i in range(0, len(values), 3)]
>>> rgb[:10]
[('255', '123', '125'), ('124', '124', '126'), ('125', '127', '129'), ('128', '129', '131'), ('130', '128', '130'), ('129', '126', '128'), ('127', '132', '134'), ('133', '132', '135'), ('134', '135', '140'), ('139', '137', '143')]

这篇关于使用Python 3.x来正确读取ppm文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆