SlideServe
Menu
Préférences
© SlideServe · v2026

CPSC 231 Tutorial

ivrit avatar
9 diapositives · 251 Views · Jul 31, 2014

This tutorial provides a step-by-step guide to creating a Python program named `inv-img.py`, which reads black and white PBM images (P1 format), inverts their pixel values, and writes the inverted image to a new file. It covers reading image headers, processing pixel data, and writing the output in the correct format. Use the provided code snippets to understand the key functions such as `readimage`, `invert`, and `writeimage`. Get hands-on experience with programming and manipulating bitmap images in Python.

CPSC 231 Tutorial - slide 1
01 / 9CPSC 231 Tutorial
1 / 17

Télécharger la présentation

CPSC 231 Tutorial
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CPSC 231 Tutorial Xin Liu April 1, 2013

  2. Just announced • Due: 4pm Friday, 12 April Assignment 4

  3. Black and White bitmap image • Plain text file (ASCII) • First line: P1 • Comments starting with # • Width height • Data (1 for white, 0 for black, separated by space or Carriage Return) • See the four files for As4 for examples PBM file - type p1

  4. Create a program inv-img.py to • Read a pbm image • Invert the bits (1-> 0; 0 -> 1) • Write out the inverted image Exercise

  5. defreadimage(filename): infile = open(filename, 'r') img = [] width = 0 height = 0 # read the image header while True: line = infile.readline() if line == 'P1\n': continue; if line[0] == '#': continue; fields = line.split() width = int(fields[0]) height = int(fields[1]) break

  6. # read the image data row = 0 fields = [] i = 0 while row < height: col = 0 listrow = [] while col < width: if i == len(fields): line = infile.readline() fields = line.split() i = 0 listrow.append(int(fields[i])) col = col + 1 i = i + 1 img.append(listrow) row = row + 1 infile.close() return img

  7. definvert(img): invimg = [] width = len(img[0]) height = len(img) for row in range(0, height): listrow = [] for col in range(0, width): listrow.append(1 - img[row][col]) invimg.append(listrow) return invimg

  8. defwriteimage(filename, img): outfile = open(filename, 'w') outfile.write('P1\n') outfile.write('# inversed image\n') width = len(img[0]) height = len(img) outfile.write(str(width) + ' ' + str(height) + '\n') for row in range(0, height): line = '' for col in range(0, width): line = line + str(img[row][col]) + ' ' outfile.write(line + '\n') outfile.close()

  9. # main program import sys if len(sys.argv) != 3: print('usage: python3', sys.argv[0], 'infileoutfile') exit() infilename = sys.argv[1] outfilename = sys.argv[2] inimg = readimage(infilename) outimg = invert(inimg) writeimage(outfilename, outimg)