Question

In: Computer Science

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


Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code with no comments, for easy copying

import image
import random

def create_sunset(num_stars=10):
    picture=image.EmptyImage(400,400)
    divisions=255/picture.getHeight()
    for i in range(picture.getHeight()):
        pix=image.Pixel(int(divisions*i),int(divisions*i*0.7),0)
        for j in range(picture.getWidth()):
            picture.setPixel(j,i,pix)
    pix=image.Pixel(255,255,255)
    for i in range(num_stars):
        row=random.randrange(0,picture.getHeight()//3)
        col=random.randrange(0,picture.getWidth())
        picture.setPixel(col,row,pix)
    return picture




win = image.ImageWin("Image", 400, 400)
img = create_sunset(20)
img.draw(win)
win.mainloop()

#same code with comments, for learning

import image
import random


# method to create a 400x400 sunset image, and add num_stars number of stars
def create_sunset(num_stars=10):
    # creating a 400x400 image
    picture = image.EmptyImage(400, 400)
    # dividing 255 by height to get a value to interpolate between colors
    divisions = 255 / picture.getHeight()
    # looping through each row
    for i in range(picture.getHeight()):
        # based on the row, creating a pixel. it will be darker in top, and reddish/yellow in bottom
        # change 0.7 to 0.6 or 0.8 depending on you much reddish yellow you want.
        pix = image.Pixel(int(divisions * i), int(divisions * i * 0.7), 0)
        # looping through each column in current row, filling with this pixel
        for j in range(picture.getWidth()):
            picture.setPixel(j, i, pix)
    # creating a pixel for white color
    pix = image.Pixel(255, 255, 255)
    # looping for num_stars number of times
    for i in range(num_stars):
        # generating a random set of row and col indices
        # making sure that row is somewhere at the top (sky)
        row = random.randrange(0, picture.getHeight() // 3)
        col = random.randrange(0, picture.getWidth())
        # filling that position with white
        picture.setPixel(col, row, pix)
    # returning picture
    return picture


# creating a window, creating and displaying a sunset image, with 20 stars
win = image.ImageWin("Image", 400, 400)
img = create_sunset(20)
img.draw(win)
win.mainloop()

#output


Related Solutions

How to create a FTP server using python and TCP Ports How to create a FTP...
How to create a FTP server using python and TCP Ports How to create a FTP server using python and UDP Ports
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Using Matlab 1. Create a color image of size 200 × 200. The image should have...
Using Matlab 1. Create a color image of size 200 × 200. The image should have a blue background. 2. Create 100 yellow regions within the image that have a variable size. More specifically, their width should be an odd number and vary between 3 and 7 and their height should also be an odd number and vary between 3 and 7. For example, you may get rectangular regions of size 3 × 3, 3 × 5, 5 × 3,...
in python how would create permuntations of a list with digits 0-9 using recursion with at...
in python how would create permuntations of a list with digits 0-9 using recursion with at most two for loops and no other lib allowed? needs to return all perm length between 3-7 if possbile
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
list at least two weaknesses of using sound to image an object. can these weaknesses be...
list at least two weaknesses of using sound to image an object. can these weaknesses be overcome, how?
Create a Home Page using HTML and CSS Part 1 • Contain an image or logo...
Create a Home Page using HTML and CSS Part 1 • Contain an image or logo about your website. • Must contain three navigation links. Home link. Registration link. Animations link. • Footer will contain links to web pages to any other site you wish (just be outside your webpage, like YouTube,etc.) Open links in new tab, not current tab. Create a Registration page: • Registration Fields: 1) User Name - Input Text 2) Password - Input Password 3) Repeat...
This is using Python, it is utilizing code from a Fraction class to create a Binary...
This is using Python, it is utilizing code from a Fraction class to create a Binary Class Please leave comments so I may be able to learn from this. Instruction for Binary Class: Exercise 6.18: Design an immutable class BinaryNumber, in the style of our Fraction class. Internally, your only instance variable should be a text string that represents the binary value, of the form '1110100'. Implement a constructor that takes a string parameter that specifies the original binary value....
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside...
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside of the ServerClass class, that: 1. Takes the IP 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the Server object with the larger sum Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) Hint Modify get_server_ip in the previous problem. -------------------- Please use this code to start class ServerClass: """ Server class for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT