Question
How to write this code in python?
Linear Interpolation Write a program that will create a linear interpolation between two pixels. You are to read in two pixel
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#some values generated are slightly different by 1
#for example for the 5 the value i have got bf but in the question it is c0
#that's it all the values got the right answers
#for some values it is differed by 1
#pix.py
#function that returns the r,g,b values or else returns -1
def slice(color):
   if(color[:2] == '0x' and len(color) == 8):
       try:
           r,g,b = int(color[2:4],16) , int(color[4:6],16),int(color[6:8],16)
       except Exception as e:
           return [-1]
       return [r,g,b]
   else:
       return [-1]
#function that converts rgb to hex
def transform(color):
   res = "0x"
   for code in color:
       if(code < 15):
           res += '0'
       res += hex(code)[2:]
   return res
#interpolate a given color
def interpolate_color(one,two,factor = 0.5):
   result = [one[0], one[1], one[2]]
   for i in range(3):
       res = result[i] + factor * (two[i] - one[i])
       result[i] = round(res)
   return result

#interpolate all the colors
def interpolate_colors(one,two,steps):
   step_factor = 1 / (steps - 1)
   array = []
   for i in range(steps):
       col = interpolate_color(one,two, step_factor * i)
       array.append(col)
   return array


one = input('Enter the first pixel: ').strip().lower()
two = input('Enter the second pixel: ').strip().lower()
steps = int(input("Enter the number of colours to generate in between: ").strip())

steps += 2
one = slice(one)
two = slice(two)
if(one[0] == -1 or two[0] == -1):
   print("Entered Color is invalid. Color Format: 0xffffff")
   exit(1)
res = interpolate_colors(one, two,steps)
print("Colours: ")
for col in res:
   print(transform(col))

File Edit earch View Document Project Build Iools Help New Open PIx.py ave Save All Revert CloseBack Forward Compile BuildExe File Edit earch View Document Project Build Iools Help New Open ave Save All Revert CloseBack Forward Compile BuildExecute CoC:Windows SYSTEM32\cmd.exe Enter the first pixel: 0x002222 Enter the second pixel: 0xffdd33 Enter the number of colours to ge

Add a comment
Know the answer?
Add Answer to:
Linear Interpolation Write a program that will create a linear interpolation between two pixels. ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels contai...

    Please design the function in version 3 of python 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents the portion of the original image that is specified by the other four parameters. The extracted portion of the image should consist of the pixels that fall in the intersection of the rows of pixels that begin with row rmin...

  • When images are scaled or resized, the values between pixels need to be guessed/interpolated in o...

    Whats wrong with my code? Please use MATLAB and can you answer the questions too. When images are scaled or resized, the values between pixels need to be guessed/interpolated in order to fill in the missing gaps. Write a function that takes a grayscale image (pick your own image) and a scaling factor (any positive decimal value) as input and resizes the image using the following methods; a) Nearest neighbouring pixel b) Linear interpolation (in x-direction first, then y-direction) c)...

  • Create a program in Python to generate a binary image that identifies an object from the backgrou...

    Create a program in Python to generate a binary image that identifies an object from the background--the pixels belonging to the object will take values of one and the pixels from background will take values of zero respectively. To do this, use the subtraction operator and a threshold to identify the differences between the first image and the second image that contains the object of interest.

  • WRITE A C++ PROGRAM TO ANSWER THE QUESTIONS BELOW. You are provided with an image in...

    WRITE A C++ PROGRAM TO ANSWER THE QUESTIONS BELOW. You are provided with an image in a new file format called JBC Here is an example of the format: 2 3 100 50 40 200 66 56 12 45 65 23 67 210 0 255 100 45 32 123 The first line means that this is a 2 (width) by 3 (height) image of pixels. Then, each line represents the color of a pixel. The numbers are from 0 to...

  • please code in python 1. Write a program to request the user enter a desired number...

    please code in python 1. Write a program to request the user enter a desired number of values. Create a loop to load the desired number of user-specified values into a list. Create two lists with the same values but generated in different ways. On list will be originally initialized to have the desired length before entering the loop. The other list will begin empty and values be appended for every iteration of the loop. Output from the program should...

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • Using C#, create a class in a file MathOP.cs. MathOP.cs needs to include the following methods MathAdd: to add two numb...

    Using C#, create a class in a file MathOP.cs. MathOP.cs needs to include the following methods MathAdd: to add two numbers MathSub: to subtract two numbers Next, create a new class MathOP2 (MathOP2.cs) that Inherits from MathOP.cs. MathOP2.cs needs to include the following methods MathMult: to multiply two numbers MathDiv: to divide two numbers Allinone: accepts two parameters and set 4 class variables: v_add, v_subtract, v_multiply, and v_divide and show how these values are retrieved. Finally, create a program TestMathOP.cs...

  • Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method...

    Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method and divide method, both methods accepts two parameters and return a value. Create a test program with documentation to test all operators (at least 4) The TestMathOP should do the following      Enter the First number >> 5.5      Enter the Second Number >> 7.5      The sum of the numbers is 13      The subtract of the two numbers is -2.00      Do...

  • Create a JAVA program that in two numbers from user input, the program then does the...

    Create a JAVA program that in two numbers from user input, the program then does the following arithmetic calculations. The program will run in ascending order if the first number is small and in descending order of the first number is bigger. The program then alternates between addition(ODD) and subtraction(EVEN) depending on the previous number see examples below   [15] Sample run 1: Enter two numbers: -3 2 Output: The following arithmetic calculations were performed : Arithmetic operations = (-3) +...

  • write a java program that prompts a user to enter two different numbers, divide the first...

    write a java program that prompts a user to enter two different numbers, divide the first number by second and prints the result

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT