0% found this document useful (0 votes)
15 views43 pages

Graphics

The document discusses graphics and image processing in Python. It provides an overview of the turtle graphics module, which allows drawing simple shapes. It describes how to instantiate a Turtle object and use methods to control the turtle's movement and drawing. It also discusses representing and manipulating digital images, including file formats, sampling, pixel grids, and algorithms for converting images to black and white or grayscale.

Uploaded by

almulla7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views43 pages

Graphics

The document discusses graphics and image processing in Python. It provides an overview of the turtle graphics module, which allows drawing simple shapes. It describes how to instantiate a Turtle object and use methods to control the turtle's movement and drawing. It also discusses representing and manipulating digital images, including file formats, sampling, pixel grids, and algorithms for converting images to black and white or grayscale.

Uploaded by

almulla7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter 7: Simple Graphics

and Image Processing


Simple Graphics

• Graphics: Discipline that underlies the


representation and display of geometric shapes in
two- and three-dimensional space
• A Turtle graphics toolkit provides a simple and
enjoyable way to draw pictures in a window
– turtle is a non-standard, open-source Python
module

Fundamentals of Python: First Programs 2


Overview of Turtle Graphics

• Turtle graphics originally developed as part of the


children’s programming language Logo
– Created by Seymour Papert and his colleagues at
MIT in the late 1960s
• Analogy: Turtle crawling on a piece of paper, with a
pen tied to its tail
– Sheet of paper is a window on a display screen
– Position specified with (x, y) coordinates
• Cartesian coordinate system, with origin (0, 0) at the
center of a window

Fundamentals of Python: First Programs 3


Overview of Turtle Graphics
(continued)

• Together, these attributes make up a turtle’s state


Fundamentals of Python: First Programs 4
Turtle Operations

Fundamentals of Python: First Programs 5


Turtle Operations (continued)

Fundamentals of Python: First Programs 6


Turtle Operations (continued)

• Interface: set of methods of a given class


– Used to interact with an object
– Use docstring mechanism to view an interface
• help(<class name>)
• help(<class name>.<method name>)

Fundamentals of Python: First Programs 7


Object Instantiation and the
turtle Module
• Before you apply any methods to an object, you
must create the object (i.e., an instance of)
• Instantiation: Process of creating an object
• Use a constructor to instantiate an object:

• To instantiate the Turtle class:

Fundamentals of Python: First Programs 8


Object Instantiation and the
turtle Module (continued)

• To close a turtle’s window, click its close box


• Attempting to manipulate a turtle whose window
has been closed raises an error
Fundamentals of Python: First Programs 9
Object Instantiation and the
turtle Module (continued)

Fundamentals of Python: First Programs 10


Drawing Two-Dimensional Shapes

• Many graphics applications use vector graphics,


or the drawing of simple two-dimensional shapes,
such as rectangles, triangles, and circles

Fundamentals of Python: First Programs 11


Drawing Two-Dimensional Shapes
(continued)

Fundamentals of Python: First Programs 12


Taking a Random Walk

• Like any animal, a turtle can wander around


randomly:

Fundamentals of Python: First Programs 13


Taking a Random Walk (continued)

Fundamentals of Python: First Programs 14


Colors and the RGB System

• Display area on a computer screen is made up of


colored dots called picture elements or pixels
• Each pixel represents a color – the default is black
• RGB is a common system for representing colors
– RGB stands for red, green, and blue
– Each color component can range from 0 – 255
• 255 à maximum saturation of a color component
• 0 à total absence of that color component
– A true color system

Fundamentals of Python: First Programs 15


Colors and the RGB System (cont’d)

• Each color component requires 8 bits; total number


of bits needed to represent a color value is 24
– Total number of RGB colors is 224 (16,777,216)
Fundamentals of Python: First Programs 16
Example: Drawing with Random
Colors
• The Turtle class includes a pencolor method
for changing the turtle’s drawing color
– Expects integers for the three RGB components

Fundamentals of Python: First Programs 17


Examining an Object's Attributes

• Mutator methods change the internal state of a


Turtle method
– Example: pencolor method

• Accessor methods return the values of a Turtle


object’s attributes without altering its state
– Example: position method

Fundamentals of Python: First Programs 18


Manipulating a Turtle’s Screen
• The Screen object’s attributes include its width
and height in pixels and its background color
• Use t.screen to access a turtle’s Screen
object, then call a Screen method on this object

Fundamentals of Python: From First Programs Through Data Structures 19


Image Processing

• Digital image processing includes the principles


and techniques for the following:
– The capture of images with devices such as flatbed
scanners and digital cameras
– The representation and storage of images in efficient
file formats
– Constructing the algorithms in image-manipulation
programs such as Adobe Photoshop

Fundamentals of Python: First Programs 20


Analog and Digital Information

• Computers must use digital information which


consists of discrete values
– Example: Individual integers, characters of text, or
bits
• The information contained in images, sound, and
much of the rest of the physical world is analog
– Analog information contains a continuous range
of values
• Ticks representing seconds on an analog clock’s
face represent an attempt to sample moments of
time as discrete values (time itself is analog)
Fundamentals of Python: First Programs 21
Sampling and Digitizing Images

• A visual scene projects an infinite set of color and


intensity values onto a two-dimensional sensing
medium
– If you sample enough of these values, digital
information can represent an image more or less
indistinguishable (to human eye) from original scene
• Sampling devices measure discrete color values at
distinct points on a two-dimensional grid
– These values are pixels
– As more pixels are sampled, the more realistic the
resulting image will appear
Fundamentals of Python: First Programs 22
Image File Formats

• Once an image has been sampled, it can be stored


in one of many file formats
• A raw image file saves all of the sampled
information
• Data can be compressed to minimize its file size
– JPEG (Joint Photographic Experts Group)
• Uses lossless compression and a lossy scheme
– GIF (Graphic Interchange Format)
• Uses a lossy compression and a color palette of up
to 256 of the most prevalent colors in the image

Fundamentals of Python: First Programs 23


Image-Manipulation Operations

• Image-manipulation programs either transform the


information in the pixels or alter the arrangement of
the pixels in the image
• Examples:
– Rotate an image
– Convert an image from color to grayscale
– Blur all or part of an image
– Sharpen all or part of an image
– Control the brightness of an image
– Perform edge detection on an image
– Enlarge or reduce an image’s size
Fundamentals of Python: First Programs 24
The Properties of Images

• The coordinates of pixels in the two-dimensional


grid of an image range from (0, 0) at the upper-left
corner to (width-1, height-1) at lower-right corner
– width/height are the image’s dimensions in pixels
– Thus, the screen coordinate system for the display
of an image is different from the standard Cartesian
coordinate system that we used with Turtle graphics
• The RGB color system is a common way of
representing the colors in images

Fundamentals of Python: First Programs 25


The images Module

• Non-standard, open-source Python tool


– Image class represents an image as a two-
dimensional grid of RGB values

Fundamentals of Python: First Programs 26


The images Module (continued)

Fundamentals of Python: First Programs 27


A Loop Pattern for Traversing a Grid

• Most of the loops we have used in this book have


had a linear loop structure
• Many image-processing algorithms use a nested
loop structure to traverse a two-dimensional grid
of pixels

Fundamentals of Python: First Programs 28


A Loop Pattern for Traversing a Grid
(continued)

• Previous loop uses a row-major traversal


– We use this template to develop many of the
algorithms that follow:

Fundamentals of Python: First Programs 29


A Word on Tuples

• A pixel’s RGB values are stored in a tuple:

Fundamentals of Python: First Programs 30


Converting an Image to Black and
White
• For each pixel, compute average of R/G/B values
• Then, reset pixel’s color values to 0 (black) if the
average is closer to 0, or to 255 (white) if the
average is closer to 255

Fundamentals of Python: First Programs 31


Converting an Image to Black and
White (continued)

Fundamentals of Python: First Programs 32


Converting an Image to Grayscale
• Black and white photographs contain various
shades of gray known as grayscale
• Grayscale can be an economical scheme (the only
color values might be 8, 16, or 256 shades of gray)
• A simple method:

– Problem: Does not reflect manner in which different


color components affect human perception
• Scheme needs to take differences in luminance
into account
Fundamentals of Python: First Programs 33
Converting an Image to Grayscale
(continued)

Fundamentals of Python: First Programs 34


Copying an Image
• The method clone builds and returns a new image
with the same attributes as the original one, but
with an empty string as the filename

Fundamentals of Python: First Programs 35


Blurring an Image
• Pixilation can be mitigated by blurring

Fundamentals of Python: First Programs 36


Edge Detection

• Edge detection removes the full colors to uncover


the outlines of the objects represented in the image

Fundamentals of Python: First Programs 37


Edge Detection (continued)

Fundamentals of Python: First Programs 38


Reducing the Image Size
• The size and the quality of an image on a display
medium depend on two factors:
– Image’s width and height in pixels
– Display medium’s resolution
• Measured in pixels, or dots per inch (DPI)
• The resolution of an image can be set before the
image is captured
– A higher DPI causes sampling device to take more
samples (pixels) through the two-dimensional grid
• A size reduction usually preserves an image’s
aspect ratio

Fundamentals of Python: First Programs 39


Reducing the Image Size (continued)

• Reducing size throws away some pixel information


Fundamentals of Python: First Programs 40
Summary

• Object-based programming uses classes, objects,


and methods to solve problems
• A class specifies a set of attributes and methods
for the objects of that class
• The values of the attributes of a given object make
up its state
• A new object is obtained by instantiating its class
• The behavior of an object depends on its current
state and on the methods that manipulate this state
• The set of a class’s methods is called its interface
Fundamentals of Python: First Programs 41
Summary (continued)

• Turtle graphics is a lightweight toolkit used to draw


pictures in a Cartesian coordinate system
• RGB system represents a color value by mixing
integer components that represent red, green, and
blue intensities
• A grayscale system uses 8, 16, or 256 distinct
shades of gray

Fundamentals of Python: First Programs 42


Summary (continued)

• Digital images are captured by sampling analog


information from a light source, using a device such
as a digital camera or a flatbed scanner
– Can be stored in several formats, like JPEG and GIF
• When displaying an image file, each color value is
mapped onto a pixel in a two-dimensional grid
– A nested loop structure is used to visit each position
• Image-manipulation algorithms either transform
pixels at given positions or create a new image
using the pixel information of a source image

Fundamentals of Python: First Programs 43

You might also like