Question

In: Computer Science

In simple python code 1) Write a function to convert the image to grayscale. 2Write a...

In simple python code

1) Write a function to convert the image to grayscale.



2Write a function to convert an image to black and white



3)Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows:
newR = (R × 0.393 + G × 0.769 + B × 0.189)
newG = (R × 0.349 + G × 0.686 + B × 0.168)
newB = (R × 0.272 + G × 0.534 + B × 0.131)
Write a function to convert an image to sepia tone. Remember that rgb values must be integers between 0 and 255.



4Write a function to uniformly enlarge an image by a factor of 2 (double the size).




5) After you have scaled an image too much it looks blocky. One way of reducing the blockiness of the image is to replace each pixel with the average values of the pixels around it. This has the effect of smoothing out the changes in color. Write a function that takes an image as a parameter and smooths the image. Your function should return a new image that is the same as the old but smoothed.


6 Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.

Solutions

Expert Solution

1) Write a function to convert the image to grayscale.

`

original_pixels = []

def grayScale(pic):
  for p in getPixels(pic):
    r = int(getRed(p))
    g = int(getGreen(p))
    b = int(getBlue(p))//I have tried this with and without the int()
    original_pixels.append((r, g, b))
    new = (r + g + b)/3
    color= makeColor(new,new,new)
    setColor(p, color)

`

2) Write a function to convert an image to black and white

`

import cv2
import numpy as np
img1 = cv2.imread('opencvlogo.png')
row,col,ch = img1.shape
g = [ ]  #the list in which we will stuff single grayscale pixel value inplace of 3 RBG values
#this function converts each RGB pixel value into single Grayscale pixel value and appends that value to list 'g'
def rgb2gray(Img):
    global g
    row,col,CHANNEL = Img.shape
    for i in range(row) :
        for j in range(col):
        a =      (   Img[i,j,0]*0.07  +  Img[i,j,1]*0.72 +    Img[i,j,2] *0.21   ) #the algorithm i used id , G =  B*0.07 + G*0.72 + R* 0.21
                                                                                   #I found it online
        g.append(a)
rgb2gray(img1)  #convert the img1 into grayscale
gr = np.array(g)  #convert the list 'g' containing grayscale pixel values into numpy array
cv2.imwrite("test1.png" , gr.reshape(row,col)) #save the image file as test1.jpg

`

4)

`

p = img.getPixel(col/factor,row/factor)
    newImage.setPixel(col,row,p)

`

5)

`

def is_prime(x):
    # only positive numbers are allowed and the smallest prime number is 2 
    if (x > 1):
        # since the smallest prime number is 2, we start the divisor at 3
        divisor = 2

        # because the submit number can always be divided by itself we can use the
        # range function to set the correct range
        for i in range(divisor,x):
            if (x % i) == 0:
                return False
    else:
        return False

    return True

print(is_prime(3))

`


Related Solutions

please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
A simple way to write the code to convert millileters to ounces in java
A simple way to write the code to convert millileters to ounces in java
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):     count=2;     flag=0;     # Loop to check the divisors of a number.     while(count<num and flag==0):         if(num%count!=0):             # Put flag=0 if the number has no divisor.             flag=0         else:             # Put flag=1 if the number has divisor.             flag=1         # Increment the count.         count=count+1     # Return flag value.     return flag # Intialize list. list=[]...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
In python and comments your code : Exercise 1 1.Write a function waiting for two matrices...
In python and comments your code : Exercise 1 1.Write a function waiting for two matrices and returning their addition, −1 if it is not not possible. 2. Write a function associated with a matrix and a scalar and returning the produced matrix. 3. Write a function accompanying an n × m matrix and a vector n and returning the vector produced by each other, −1 if this is not possible. 4. Write a function accompanying two n × m...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
1) Write a simple python function program that can calculate the volumes of following shape: cylinder,...
1) Write a simple python function program that can calculate the volumes of following shape: cylinder, circle, cone and sphere. For each of this shape, write a separate function. Let your function take in 2 inputs only. Your program should prompt user to enter these 2 inputs and print out the results of the volumes of the shape separately?
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Write a simple matching coefficient and jaccard similarity code in python. For a example x =...
Write a simple matching coefficient and jaccard similarity code in python. For a example x = 10101 and y = 00101 what is the code to check those similarities in python?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT