Write a function that takes as input an image object

Assignment Help Python Programming
Reference no: EM131149089

Please complete Lab 03 and 04 during today's session.

Lab 03 and 04 File Requirements:

Lab 03

Lab 04

Lab03_chk1.py

Lab04_chk1.py

Lab03_chk2.py

Lab04_chk2.py

Lab03_chk3.py

Lab04_chk3.py

Lab 03 Overview -

This lab explores the use of images, as well as external modules to obtain data (in this case, the images are the data). You can find all image functions described at this URL (so read through this first): https://pillow.readthedocs.org/en/latest/handbook/tutorial.html

In this lab, you have two mandatory checkpoints. For the third checkpoint, there are two options to choose from; you must complete at least one of these, but you are welcome to try them both.

Checkpoint 1: Two-by-two wallpaper

To start this part, first download the lab05.zip ZIP folder from the course website. This file contains many images and a file called check1.py. Unzip this folder in your Dropbox folder for Lab5. Make sure that programs are saved in the same directory as these images. And you must save your file first before executing.

In this first part of the lab, you are going to create a two-by-two wallpaper using any four of the given images. We recommend using the first four images in the given image_files list (i.e. ca.jpg, im.jpg, hk.jpg, bw.jpg), but you can use any images here that you wish.

First, your program must first create a 512x512 blank image document.

Next, your program must open each image, resize it to 256x256, and then paste them into this new image.

When you're done, have your program call the show() function to check that the wallpaper looks correct. You will see that the images are distorted because the original images are not square (and therefore become distorted when resized). We will fix that in the next part!

Here are a few image functions that might help you accomplish this:

from PIL import Image ## must import Image first to be able to use it # create a new image object im; use mode RGB and color white

# note that size is always a tuple im = Image.new( mode, size, color )
# return a new image object resized to given width and height im = im.resize( ( width, height ) )

# modify image im by pasting pasted_image into im at given coordinates im.paste( pasted_image, ( x, y ) )
# ( x, y ) coordinates of upper left corner # save an image to a file im.save( filename )

# open a window to show the image object im.show()

To complete Checkpoint 1, show a mentor your code and the output of your program.

Checkpoint 2: Image correction

To start this part, create a new file called check2_helper.py. You will write a function in this file that will later be used as a module.

What we want to accomplish in this checkpoint is to crop images so that they are square; as a result, resizing will not distort them. If the image is too wide, your program must crop it along the x-axis. If the image is too long, then your program must crop it along the y-axis. Always start from the top left corner.

Here are a few image functions that might help you accomplish this: from PIL import Image ## must import Image first to be able to use it

# this 4-tuple defines a box (or rectangle) with upper

# left point ( 10, 10 ) and lower right point ( 100, 100 ) box = ( 10, 10, 100, 100 )

# return a new image cropped based on the given box region = im.crop( box )

# open a window to show the image object region.show()

Write a function that takes as input an image object, crops it to make it a square, and returns the resulting object. You must write a function to accomplish this.

Next, write code in check2_helper.py to test this function with an example image. Open the image, crop it to a square using the function, and then show the result. Are you convinced that it works? If so, comment out all of the test code, leaving only the function.

Copy your solution from checkpoint 1 to a new file called check2.py. Modify your code so that:

• You import your own module check2_helper.py
• You use your own function from this module to crop each image in the wallpaper to a square before resizing them.

To complete Checkpoint 2, show a mentor your code and the output of your program. We will check that you are calling your module correctly, and that your program has correct structure.

Checkpoint 3 (option 1): Use an external source for images

Instead of using the files given to you, you can use external sources. To accomplish this, we have written a module called panoramio.py that finds images taken at a specific address (using images from Google maps).

You have a few functions that will be useful to you in this part: import panoramio as pan urls = pan.getPhotos( Eiffel Paris France, 5 ) # Return 5 pics of Eiffel Tower

im = pan.openphoto( urls[0] ) # Open the first image URL urls = pan.getPhotos( RPI Troy NY, 6 )

im = pan.openphoto( urls[1] ) # Open the second image URL

The function getPhotos() takes an address and the number of desired images as input. It finds all photos near the given address (up to the given number), and returns a list of URLs of these images. There may be no photos at a given address, especially if your address is not understood by the program. In this case, you will get an empty list (so check for this!).

For each photo, the URL is a string like this:

+u'https://mw2.google.com/mw-panoramio/photos/medium/59461095.jpg'+ Don't worry about the u prefix, it just means the string is encoded in Unicode.

To open an image from a URL, we will use a different method than Image.open(). We provided a function for you called openphoto that does this as shown above.

Your task now in this checkpoint is easy:

• Ask the user for an address
• Read the user input, and find 4 images at this address
• If the returned list has at least 4 images, construct the wallpaper using these images and show the result.

To complete Checkpoint 3, show a mentor your code and the output of your program.

Checkpoint 3 (option 2): A different layout for wallpaper

Here is a bit of challenge for you. We will change the wallpaper layout so that you put 6 images in a line, alternating the vertical location as shown in the figure below. Each white box is an image. We recommend you use the last 6 images (1.jpg,..,6.jpg) in the list for this.

To accomplish this:

• You will need to resize the images (no cropping) proportional to their original size so that their height is 256.
• Paste them into a wallpaper of size 1000x360.
• The first image starts at (30,20).
• Images have 10 pixels in between along the horizontal axis.
• The images move up and down 40 pixels along the vertical axis.

To complete Checkpoint 3, show a mentor your code and the output of your program.

Lab 04 Overview - Soduko Summer

In this lab, we use Sudoku puzzles to investigate the use of logic, nested lists, and nested loops. Please start by downloading the lab06_files.zip file from the course website. This file includes a Python utility for reading files, and several Sudoku puzzles. You will use the utility function only in the final checkpoint. Until then, you will work with a given board.

Sudoku

Sudoku is a popular logic puzzle, typically using the digits 1 through 9. There are many Sudoku books and websites. The puzzle below was taken from https://www.websudoku.com, where you can learn a bit more about the rules of the Sudoku.

In a Sudoku solution, each row, column, and 3x3 block contains the digits 1 through 9 exactly once. A Sudoku puzzle starts with only some of the squares having numbers, and there is generally only one way the remaining squares may be legally filled in. Sometimes finding this solution is easy. Other times it seems impossible.

Checkpoint 0: Double loops

Before you start this lab, we will do a small exercise that will allow you to complete the rest of the lab much faster. This initial checkpoint is an exercise and will not be checked off.

The idea is that you will need to write a few loops that generate pairs of values of different types. Once you have these loops in place, you can easily use them as indices for your code later. The best idea is to write them in a separate file. Try doing these both with while and for loops.

1. Write a loop to count from 0 up to 8 (all ranges inclusive from now on).
2. Write a loop to generate pairs of values from 0 up to 8 (basically, for each value above, you will generate a second value between 0-8). As a special challenge, I added a space and line to separate each 3x3 block.

These will serve as the indices for the Sudoku board entries.

3. Write a loop to generate all items in a given row, say row

4. Write a loop to generate all items in a single column, say column

5. Finally, write a loop to generate the valid indices for the first 3x3 piece of the board.

Think about how you would modify this to generate the other 3x3 blocks. What are the starting and end indices?

The above exercises are so you can see the patterns of how we can write loops to traverse and work with parts of a Sudoku puzzle. Next, we will use these loops to actually complete the lab.

Checkpoint 1: Representing and Building the Board

We will represent the Sudoku board as a list of lists of single character strings. Start by looking at the code in check1.py. It has an example board, stored in the variable bd. Each ‘.' is an empty location on the Sudoku board, essentially a placeholder.

The given code prints the length of bd, the length of the 0-th list stored in bd, the entry in row 0, column 0, and finally the entry in row 8, column 8. Run this code, and make sure you understand the output you are seeing.

Write nested for or while loops to print the whole board on the screen. You will first go through each row with one loop, then for each row, you will go through each column using a second loop (see index range 2 from Checkpoint 0 above).

Print each item with space on both sides, and a | after every third item and third row. Remember, you have exactly 9 rows and 9 columns.

Hint. Double loops can be difficult, so I recommend you start slowly and add complexity. This will also help you in the other parts.

First, read each row as a list, and print each list on a single line. This is doable with a single loop.

Next, add the second loop for formatting each row. Go through each item in the row with a second loop, and construct a string containing your whole line. For each item, you will append a space before and after the item, and | at the beginning and end. Once done, print this string.

Now that you are printing reasonable lines, figure out how to add the | after every third column in the row.

Finally, add the code to print the line of hyphens (-) as needed. Always do things in small steps, and add complexity.

Checkpoint 2: Assigning Numbers to Cells

Recall that the completed Sudoku board has no repeated numbers in a row, in a column, or in any of the nine 3x3 blocks. In Checkpoint 2, your code will ask the user to enter a row (starting at index 0), a column (also starting at index 0), and a number. It will then call function ok_to_add(), which you must write, to check to see if the number can safely be added to that particular row and column based on the current contents of the Sudoku board. You will then either tell the user, This number cannot be added, or if it can be added, change the board and reprint it.

To start Checkpoint 2, copy and paste your code from Checkpoint 1. The actual work of Checkpoint 2 is the function ok_to_add(). It has quite a few checks you will need to write. For example, if the user asks to put a 2 in row 1, column 8, the function should

• check if row 1 contains a 2 already,
• check if column 8 contains a 2 already, and
• check if the 3x3 block starting at row 0, column 6 contains a 2 already.

Also, what about location (1,8) itself? You need to check that there is nothing there already. It is better to move this check to outside of this function. That way, we can use this function for multiple purposes.

For the Sudoku board from Checkpoint 1, ok_to_add() should return False because there is already a 2 in the 3x3 block. The ok_to_add() function should also check to see if the row index, the column index, and the number are legal values | remember that human users make mistakes!

The function ok_to_add() will have separate loops to check the row, the column, and the 3x3 block. The latter is the hardest because you need to find the lowest row and column indices in the block and then write nested loops to check all 9 locations. The code should return False immediately when it finds a mistake, but it should wait until all checking is complete before returning True.

Note that when ok_to_add() returns True, it does not mean that the placement of the number is actually correct. It is really just a sanity check.

Checkpoint 3: Sudoku Verifier

We are going to make a few changes to your code from Checkpoint 2 to complete this checkpoint. First, you will use the utility function given to you (in the lab06_util.py module) to read a board from a file. Prompt the user for a file name and read the board from the file by importing the given code.

Next, you will write a Sudoku solution verifier by utilizing the ok_to_add() function. The verification of a correct solution to a Sudoku board can be broken down into two steps:

• Verify that there are no empty (‘.') spaces in the board at all.
• Verify that for every number in the solution, it is ok_to_add() in its current position. For example, if there is a 3 in position (0,1), we want to make sure that ok_to_add(0,1,3,board) returns True.

If all numbers are not empty and all are ok_to_add() in their current position, then the solution is valid. Your job in Checkpoint 3 is to write the function verify_board() that does just that. To verify your solution, you can use the files solved.txt, unsolved1.txt and unsolved2.txt.

To complete Checkpoint 3, combine all your code into a while loop that does the following:

ask user for file name
read board from file while ( board is not solved according to verify_board ): ask user for input to the puzzle
if ok_to_add()
add value print the board

https://www.dropbox.com/s/jzdb8v6f6uqm9lu/lab_05_06.zip?dl=0

Reference no: EM131149089

Questions Cloud

Undirected weighted graph with distinct edge weights : In an undirected weighted graph with distinct edge weights, both the lightest and the second lightest edge are in some MST (Minimum Spanning Tree). Is this true or false?
Research the criminological and victimization theories : research the criminological and victimization theories presented in the group lists below.- Select two criminological theories, - two theories of victimization.
Calculate the mass moment of inertia : Consider 500mm to be the max size of the flywheel dia. Calculate the mass moment of inertia required for the flywheel of the punching press machine
What is artificial intelligence : What is Artificial intelligence - Which is not the commonly used programming language for AI?
Write a function that takes as input an image object : Write a function that takes as input an image object, crops it to make it a square, and returns the resulting object. You must write a function to accomplish this.
What indicators might see as anthony progresses : Consider and discuss the 5 stages of change. Based upon the information provided discuss what stage Anthony is in, and provide a rationale for your decision. Next, discuss the other stages of change and what indicators we might see as Anthony prog..
Calculates the employees gross pay : Calculates the employee's gross pay, based on their hourly pay and how many hours they worked and determines the mean household size in Marengo, the largest household size and the smallest household size. The program must complete these tasks witho..
Write a one page issue proposal : Write a one-page Issue Proposal. - The issue you are proposing is the topic you will pursue for your course project.
What is the molar mass of the mixture : Inside a rigid container, a mixture of three ideal gases with mass fractions of 30% N2, 50% O2 and 20% CO2 has a total mole of 10 kmol. What's the molar mass of the mixture

Reviews

Write a Review

Python Programming Questions & Answers

  Write a program to that displays a table

Write a program to that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit and Kelvin equivalents

  Implement functions for insertion sort

Implement functions for Insertion sort and bubble sort in python programming language and a function that calculates the function execution time.

  Program that allows the user to enter the type of coin

Create a program that allows the user to enter the type of coin to be evaluated and the number of coins. The program should calculate the total weight of the coins and the height of the coins when stacked. Name the program coins.py. Use the table bel..

  Design a function that accepts an integer

Design a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, . . . 50. Use recur..

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Most popular name would be the one with the biggest number

the program will ask you to insert the year that the user is interested in, then ask the gender that the user is interested in, and print out the most popular names corresponding to the year and gender. FYI, the number on the right represents how man..

  Create a ride share simulator assignment

Create a ride share simulator assignment. In this particular function, I am trying to create a list of the drivers in a Driver Class that are currently listed as idle. Since they are idle, I can then match them up with an awaiting rider. The assignme..

  Modify your code to ask the user for the id of a restaurant

Copy check1.py to check2.py and continue to work on check2.py. Modify your code to ask the user for the id of a restaurant between 1 and 155 (humans don't need to know about list ids starting at 0).

  Takes a directory containing files that record

Create a program called grading.py that takes a directory containing files that record how student's performed on their assignments and from these files determines each students grades in addition to the course statistics.

  Find the number of vowels in the string

Using list and one definition for find the Number of vowels in the String.

  Find a python script

Find a python script

  Interaction between the customer and the machine

In Python:Simulate a cash register or ATM including the interaction between the customer and the machine (i.e. assume that you are automating the responses)

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd