# scale.py, by chaynes@indiana.edu import image def scale_length(lst, length): """Return a horizontally-scaled version of a list, stretched or compressed to be of the given length. >>> scaleLength([0, 1, 2], 6) [0, 0, 1, 1, 2, 2] >>> scaleLength([0, 1, 2, 3, 4, 5], 3) [0, 2, 4] >>> """ answer = [] for i in range(length): answer.append(lst[i * len(lst) / length]) return answer def scale_matrix_width(matrix, width): """Return matrix obtained from the given matrix by scaling each row to the given width. """ answer = [] for index in range(len(matrix)): row = matrix[index] answer.append(scale_length(row, width)) return answer def main(): """Show bonsai.jpg with normal, expanded and compressed width.""" m = image.read('bonsai.jpg') width = len(m[0]) narrow = scale_matrix_width(m, width / 2) image.new(narrow).show() wide = scale_matrix_width(narrow, width * 2) image.new(wide).show() if __name__ == '__main__': main()