enlarge.py """ File: enlarge.py Project 7.11 Defines and tests a function to enlarge an image. """ from images import Image def enlarge(imag

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
enlarge.py
"""
File: enlarge.py
Project 7.11

Defines and tests a function to enlarge an image.
"""

from images import Image

def enlarge(image, factor):
"""Builds and returns a copy of the image which is larger
by the factor."""

def block_copy_pixel_to(image, row_index, line, pixel_to_copy, factor):
for x in range(factor):
for y in range(factor):
image.setPixel(row_index + x, line + y, pixel_to_copy)

old_height = image.getHeight()
old_width = image.getWidth()
new_height = old_height * factor
new_width = old_width * factor

new_image = Image(new_width, new_height)

nhi = 0 # new height index
for ohi in range(old_height): # original image height index
# new left & right indices
nli = 0
nri = new_width - 1
# original left & right indices
oli = 0
ori = old_width - 1

# Copy blocks of the same pixel to the new image
while oli < ori:
old_left_pixel = image.getPixel(oli, ohi)
block_copy_pixel_to(new_image, nli, nhi, old_left_pixel, factor)
old_right_pixel = image.getPixel(ori, ohi)
block_copy_pixel_to(new_image, nri - factor, nhi, old_right_pixel, factor)

# Increment the image indices
nli += factor
nri -= factor

oli += 1
ori -= 1

# increment new image row index
nhi += factor

return new_image

def main():
filename = input("Enter the image file name: ")
image = Image(filename)
newimage = enlarge(image, 2)
newimage.draw()

if __name__ == "__main__":
main()
Instructions
To enlarge an image, one must fill in new rows and
columns with color information based on the colors of
neighboring positions in the original image.
• Develop and test a function named enlarge.
• This function should expect an image and an integer
factor as arguments.
• The function should build and return a new image
that represents the expansion of the original image by
the factor.
Hint: Copy each row of pixels in the original image to one
or more rows in the new image
To copy a row, use two index variables, one that starts on
the left of the row and one that starts on the right. These
two indexes converge to the middle. This will allow you to
copy each pixel to one or more positions of a row in the
new image.)
An example of the program is shown below:
Original
Enlarged
Transcribed Image Text:Instructions To enlarge an image, one must fill in new rows and columns with color information based on the colors of neighboring positions in the original image. • Develop and test a function named enlarge. • This function should expect an image and an integer factor as arguments. • The function should build and return a new image that represents the expansion of the original image by the factor. Hint: Copy each row of pixels in the original image to one or more rows in the new image To copy a row, use two index variables, one that starts on the left of the row and one that starts on the right. These two indexes converge to the middle. This will allow you to copy each pixel to one or more positions of a row in the new image.) An example of the program is shown below: Original Enlarged
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Image Element
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education