Question

In: Computer Science

Draw a landscape image using python coding.

Draw a landscape image using python coding.

Solutions

Expert Solution

def draw_layers(layers, width, height, color_dict=None):
    # Default color palette
    if color_dict is None:
        color_dict = {'0': (195, 157, 224), '1': (158, 98, 204),
                      '2': (130, 79, 138), '3': (68, 28, 99), '4': (49, 7, 82),
                      '5': (23, 3, 38), '6': (240, 203, 163)}
    else:
        # len(color_dict) should be at least: # of layers +1 (background color)
        if len(color_dict) < len(layers)+1:
            raise ValueError("Num of colors should be bigger than the num of layers")

    # Create image into which the terrain will be drawn
    landscape = Image.new('RGBA', (width, height), color_dict[str(len(color_dict)-1)])
    landscape_draw = ImageDraw.Draw(landscape)
    # Draw the sun
    landscape_draw.ellipse((50, 25, 100, 75), fill=(255, 255, 255, 255))
    # Sample the y values of all x in image
    final_layers = []
    for layer in layers:
        sampled_layer = []
        for i in range(len(layer)-1):
            sampled_layer += [layer[i]]
            # If x difference is greater than 1
            if layer[i+1][0]-layer[i][0] > 1:
                # Linearly sample the y values in the range x_[i+1]-x_[i]
                # This is done by obtaining the equation of the straight
                # line (in the form of y=m*x+n) that connects two consecutive
                # points
                m = float(layer[i+1][1]-layer[i][1])/(layer[i+1][0]-layer[i][0])
                n = layer[i][1]-m*layer[i][0]
                r = lambda x: m*x+n  # straight line
                for j in range(layer[i][0]+1, layer[i+1][0]):  # for all missing x
                    sampled_layer += [[j, r(j)]]  # Sample points
        final_layers += [sampled_layer]

    final_layers_enum = enumerate(final_layers)
    for final_layer in final_layers_enum:
        # traverse all x values in the layer
        for x in range(len(final_layer[1])-1):
            # for each x value draw a line from its y value to the bottom
            landscape_draw.line((final_layer[1][x][0], height-final_layer[1][x][1],
                                 final_layer[1][x][0], height),
                                 color_dict[str(final_layer[0])])

    return landscape

Related Solutions

Using python coding, test for convergence of an infinite sequence or series. keep the coding at...
Using python coding, test for convergence of an infinite sequence or series. keep the coding at beginner level please!
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
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Provide a detailed code for Double Encryption using Image Steganography in PYTHON First encrypt the data(using...
Provide a detailed code for Double Encryption using Image Steganography in PYTHON First encrypt the data(using RC4/AES algorithm) before storing it in the image, after encrypted data is stored in the image, use image steganography to encrypt the image. Hence, we are using double encryption
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep to tell whether the participant are of the ages through 18 to 60 and sleep less than 6 hours per day? 2. Develop codes to check whether the sleep function you defined can make correct judgement. Make sure you write comments or create informative vairable name so that I can understand how you check the sleep function. (Hints: You can create toy data/dataframe to...
Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im...
Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im not too familar with how all the plotting and drawing functions work.
Write a function that draw your initials. (Using python 3 turtle) (T, S)
Write a function that draw your initials. (Using python 3 turtle) (T, S)
Python Describe the procedure for setting up the display of an image in a window.
Python Describe the procedure for setting up the display of an image in a window.
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT