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 do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
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
Working with Python. I had to create a jackalope, with half an image of an antelope...
Working with Python. I had to create a jackalope, with half an image of an antelope and half rabbit. Here is my code: def animalCollage(): antelope = makePicture(pickAFile()) rabbit = makePicture(pickAFile()) antPixel = getPixels(antelope) rabPixel = getPixels(rabbit)    for index in range(len(antPixel)/2,len(antPixel)): newColor = getColor(rabPixel[index]) setColor(antPixel[index], newColor) writePictureTo(antelope, "C:/Users/janin/OneDrive - Grand Canyon University/Grand Canyon University/CST-111/Week 6/jackalope.jpg") jackalope = pickAFile() show(antelope) My code worked fine, but my instructor said: "the python code to create the jackalope looks fine except I can't...
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,...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
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
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?
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...
Create a program/function using PYTHON that takes cents and returns to the customer how MANY coins...
Create a program/function using PYTHON that takes cents and returns to the customer how MANY coins it takes to make the change... Ex. if the change owed is 50 cents then return a 2 (for two quarters) if the change owed is 10 cents then return a 1 (for one dime) AGAIN please write this in java and please provide EXPLANATION of answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT