Question

In: Computer Science

Working with Python. I had to create a jackalope, with half an image of an antelope...

Working with Python. I had to create a jackalope, with half an image of an antelope and half rabbit. Here is my code:

def animalCollage():
antelope = makePicture(pickAFile())
rabbit = makePicture(pickAFile())
antPixel = getPixels(antelope)
rabPixel = getPixels(rabbit)
  
for index in range(len(antPixel)/2,len(antPixel)):
newColor = getColor(rabPixel[index])
setColor(antPixel[index], newColor)

writePictureTo(antelope, "C:/Users/janin/OneDrive - Grand Canyon University/Grand Canyon University/CST-111/Week 6/jackalope.jpg")
jackalope = pickAFile()
show(antelope)

My code worked fine, but my instructor said: "the python code to create the jackalope looks fine except I can't run it because you tried to write the picture out to your hard drive. You need to write the picture out to a pickAFile() location."

I am not sure how to correct this. Can anyone please help?

Thank you

Solutions

Expert Solution

Answer

  • The reason why this won't work on any other computer is that the location provided to writePictureTo() i.e. "C:/Users/janin/OneDrive - Grand Canyon University/Grand Canyon University/CST-111/Week 6/jackalope.jpg" only exists in your system. Other machines will not have the same structure and will fail to save the image.
  • Solution:
    Input:
    filepicker.py

    import os #Import os library

    def pickAFile():

        filename = input("Enter a filename:\n") #Ask user to enter filename

        if os.path.exists(filename): #Check if file with the same name already exists

            os.remove(filename) #Remove the file

        open(filename, 'w+').close() #Create a new file

        return os.path.abspath(filename) #Return the file path

    path = pickAFile()

    print(path)

    Output:

  • In the given code, we are creating a file in the current directory of the end machine.
  • The function is returning a file path corresponding to the file.
  • You can pass this file path to writePictureTo(antelop, path).
  • Also, If you don't want the user to manually enter the filename, just replace " filename = input("Enter a filename:\n") " with " filename = "jackalope.jpg" ".
  • If you already have a function with the same name, just change the name of this function to a suitable name.

Note: Follow indentations carefully!



Related Solutions

Using cImage in Python, how to create an image of a sunset (at least 400 by...
Using cImage in Python, how to create an image of a sunset (at least 400 by 400 pixels), black at the top, becoming redder/yellower lower down. Also, randomly place in the sky a given number of white stars of random size of either one pixel or 4 (adjacent) pixels
You are working on a conservation farm with an endangered species of antelope in central Colorado....
You are working on a conservation farm with an endangered species of antelope in central Colorado. You notice that birth weight of offspring seems to correlate with air temperature, but that the relationship is not as strong in females as in males. You wonder why that could be. a) What is your testable hypothesis? b) Design an experiment to test that hypothesis. c) What is your null hypothesis? d) Describe the type of data you will get from the experiment....
I want to create an image compression program with matlab use PCA. I have the code...
I want to create an image compression program with matlab use PCA. I have the code listed below. But this code is fail, the image is colorless, but still gray. Can you help me to fix my code. clc clear all picture = im2double(imread('picture1.jpg')); Red = picture(:,:,1); premean = mean(Red(:)); premax = max(Red(:)); premin = min(Red(:)); x = size(Red,1); y = size(Red,2); Z = ones(x,y)*premean; A = (Red - Z)*(1/premax - premin); B = cov(A); [veceig,eig,C] = pcacov(B); NewRed =...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
Needs to be coded in Python. Hello i am working on a project for my computer...
Needs to be coded in Python. Hello i am working on a project for my computer programming course and i am having trouble with one part. The code needs to be able to produce two versions of an output given inputs by the user. for instance. Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or...
What would the Python code be to create the following working program using turtle: -Make 4...
What would the Python code be to create the following working program using turtle: -Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists. -Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100) -Make the green turtles start at a different randomized location somewhere on the right side of the screen within the...
I am asked to create a table of reducing potentials by setting ANY of the half-cell...
I am asked to create a table of reducing potentials by setting ANY of the half-cell potential to 0V. Show calculations. My half-cells and their Eocell is : Ag+ + e- = Ag(s) 0.80V Cu2+ + 2e- = Cu(s) 0.34V Pb2+ + 2e- = Pb(s) -0.13V Sn2+ + 2e- = Sn(s) -0.14V Fe2+ + 2e- = Fe(s) -0.44V Zn2+ + 2e- = Zn(s) -0.76V Mg2+ + 2e- = Mg(s) -2.37V
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT