Question

In: Computer Science

Write a python function image compress() that takes one argument called filename, which is the name...

Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”.

A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades of gray.

The bitmap representation of the above 5 × 5 image would be a text file as follows:

255 255 125 200 000
255 255 255 255 255
125 255 255 255 255
200 255 255 255 255
000 255 255 255 255

The function compresses the image by storing information about only those pixels which have color values other than 255 by storing only those pixels which are not white. It returns a list of tuples such that each tuple contains three elements: (the row of the pixel, the column of the pixel, the colour value)

For this example,for the above image,the returned list would be[(0, 2, 125), (0, 3, 200), (0, 4, 0), (2, 0, 125), (3, 0, 200), (4, 0, 0)].

Additional Examples:

Input file: image1.txt

255 255 125 200 000
255 125 255 255 255
125 255 000 255 255
200 255 255 000 255
000 255 255 255 000

Return value of image compress ("image1.txt"):[(0, 2, 125), (0, 3, 200), (0, 4, 0), (1, 1, 125), (2, 0, 125), (2, 2, 0), (3, 0, 200), (3, 3, 0), (4, 0, 0),
(4, 4, 0)]

Input file: image2.txt

255 255 255
000 255 255
255 000 255
255 255 000

Return value of image compress("image2.txt"): [(1, 0, 0), (2, 1, 0), (3, 2, 0)]

Input file: image3.txt

128 255 255 255 192
128 000 255 255 180
000 255 000 255 149
076 255 255 000 134

Return value of image compress("image3.txt"):[(0, 0, 128), (0, 4, 192), (1, 0, 128), (1, 1, 0), (1, 4, 180), (2, 0, 0), (2, 2, 0), (2, 4, 149), (3, 0, 76),
(3, 3, 0), (3, 4, 134)]

Solutions

Expert Solution

filename=input("Enter filename: ")
data=[]
with open(filename,"r") as f:
   data=f.read().split('\n')
result=[]
for i in range(0,len(data)):#loop through data got from file
   ref=data[i].split(' ')#split the data using space
   for j in range(0,len(ref)):
       if ref[j]!="255":#compare with 255 then make a note of it
           t1=tuple((i,j,int(ref[j])))
           result.append(t1)
print(result)
f.close()

Note:please follow indentation if everything correct give me rating


Related Solutions

Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
Write a Python script that takes an input image and output's the name of the dominant...
Write a Python script that takes an input image and output's the name of the dominant color in that image(i.e. red, green, blue).  
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and...
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and returns the maximum increase from one element in the sequence to an element at a higher index. Assumptions and restrictions: The function must return a number. If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements. You can assume that the argument...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT