python - How do I pull out the first 3 lines of each ppm file? -
all have done opened small file , appended contents list called files
. files contains list of tiny ppm lines.
how remove first 3 lines of file existence?
here's example of .ppm
file looks like, it's called, tiny.ppm.
p3 5 5 255 255 0 0 100 150 175 100 150 175 100 150 175 100 150 175 100 150 175 255 0 0 100 150 175 100 150 175 100 150 175 100 150 175 100 150 175 255 0 0 100 150 175 100 150 175 100 150 175 100 150 175 100 150 175 255 0 0 100 150 175 100 150 175 100 150 175 100 150 175 100 150 175 255 0 0
my code below, however, want 'files' contain list of 9 lists containing 9 different files' information, , remove first 3 lines of of too.
def readfiles(): files = [] files.append(open('tiny.ppm','ru').readlines()) print(files)
if want more robust reading images , performing various operations on them, recommend pillow package in python.
from pil import image glob import glob def readfiles(): images = [] f in glob("*.ppm"): image = image.open(f) image_pix = list(image.getdata()) # retrieve raw pixel values list images.append(image_pix) return images # images list of lists of pixel values
for example, can crop image:
box = (100, 100, 400, 400) region = image.crop(box)
more examples , tutorial here: http://pillow.readthedocs.org/en/latest/handbook/tutorial.html
Comments
Post a Comment