A comparison between Java and C++ for GUIWhen it comes to the development of a Graphical User Interface, one of the aspects the programmer should put into consideration is making the interface as user-friendly as possible. This will ensure that any person who uses the application for whom the GUI is...
Image manipulation
######################################################
# Project: Project 3 - Image Manipulation
# UIN:
# repl.it URL: https://repl.it/@kedmiri/WeightyAlienatedOpengroup
######################################################
from PIL import Image, ImageFont, ImageDraw
from io import BytesIO
import requests
FONT_FILE = r'NerkoOne-Regular.ttf'
DATA_FILE = 'data.txt'
def get_web_image(url):
"""Retrieves an image file from the web and returns an Image object
"""
response = requests.get(url)
return Image.open(BytesIO(response.content))
def paste_image(source, destination, x, y, omit_color="None"):
"""
Copy the pixels from source to destination, starting at the x,y coordinate passed
if omit_color is "transparent", don't copy any transparent pixels
if omit_color is a color value, don't copy any pixels with that color
:return reference to the updated source Image object
"""
src_width, src_height = source.size
dst_width, dst_height = destination.size
for w in range(x, src_width+x-1):
for h in range(y, src_height+y-1):
# Source image is bigger than the destination/
if w >dst_width or h >dst_height:
continue
pixel = source.getpixel((w-x, h-y))
# Transparent pixel
if source.mode == 'RGBA' and omit_color == 'transparent' and pixel[3] == 0:
continue
destination.putpixel((w, h), pixel)
return destination
def invert_colors(image):
"""
Invert image colors
"""
width, height = image.size
result = Image.new(image.mode, image.size)
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
result.putpixel((x, y), (255-r, 255-g, 255-b))
return result
def mirror_image(image):
"""
Construct image miror
"""
width, height = image.size
result = Image.new(image.mode, image.size)
for index, x in enumerate(reversed(range(width))):
for y in range(height):
result.putpixel((index, y), image.getpixel((x, y)))
return result
def flip_image(image):
"""
Flip image
"""
width, height = image.size
result = Image.new(image.mode, image.size)
for x in range(width):
for index, y in enumerate(reversed(range(height))):
result.putpixel((x, index), image.getpixel((x, y)))
return result
def grayscale(image):
"""
Convert colored image to grayscale
"""
width, height = image.size
result = Image.new(image.mode, image.size)
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
grey = int(0.299 * r + 0.587 * g + 0.114 * b)
result.putpixel((x, y), (grey, grey, grey))
return result
def write_text_image(image, text):
width, height = image.size
result = Image.new(image.mode, image.size)
for x in range(width):
for y in range(height):
result.putpixel((x, y), image.getpixel((x, y)))
font = ImageFont.truetype(FONT_FILE, 60)
draw = ImageDraw.Draw(result)
draw.text((300, height - 100), text, fill ="white", font = font, align="center")
return result
def read_text_from_file(filename):
"""
Read file content into a string.
"""
with open(filename, 'r') as f:
return ''.join(f.readlines())
def main():
background_url_image = 'https://www.telegraph.co.uk/content/dam/Travel/2018/November/lion%20cover.jpg'
background_image = get_web_image(background_url_image)
# Resize image to (1200, 800)
background_image = background_image.resize((1200, 800))
text = read_text_from_file(DATA_FILE)
background_image = write_text_image(background_image, text)
transparent_image_url = 'https://www.knowsleysafariexperience.co.uk/media/1011/lion.png?anchor=center&mode=crop&width=680&height=640&rnd=132037090900000000'
lion_img_trans = get_web_image(transparent_image_url)
lion_img_trans = lion_img_trans.resize((300, 300))
# Project transparent image on top of the background image.
x_corrdinate = background_image.size[0] - lion_img_trans.size[0]
paste_image(lion_img_trans, background_image, x_corrdinate, 0, omit_color='transparent')
lion_small_url = 'https://sn4.scholastic.com/content/dam/classroom-magazines/sn4/issues/2019-20/100719/should-this-lion-cub-be-in-a-zoo/SN4-20191007-Zoo-HR-MO_.jpg'
reference_lion_image = get_web_image(lion_small_url)
reference_lion_image = reference_lion_image.resize((200,200))
mirrored_image = mirror_image(reference_lion_image)
paste_image(reference_lion_image, background_image, 0, 0)
paste_image(mirrored_image, background_image, 200, 0)
flipped_image = flip_image(reference_lion_image)
paste_image(flipped_image, background_image, 0, 200)
mirrored_flipped_image = flip_image(mirrored_image)
paste_image(mirrored_flipped_image, background_image, 200, 200)
grayscaled_image = grayscale(reference_lion_image)
paste_image(grayscaled_image, background_image, 0, 400)
inverted_image = invert_colors(reference_lion_image)
paste_image(inverted_image, background_image, 0, 600)
# Save result to a file.
background_image.save('artwork.jpg')
main()
Python Code for a Calculator
# Welcome message
print("Hello, welcome to the investment calculator!\n")
# get type of fund
userInput = input("Which type of investment would you like to make (savings/CD/mutual funds/bonds)? ")
# if savings
if userInput == "savings":
# get amount invested
investment = float(input("\nHow much would you like to invest? "))
# 0.6% increase for 3 years
current = round(investment * 1.006, 3)
print("\nAfter 1 year: " + str(current))
current = round(current * 1.006, 3)
print("After 2 years: " + str(current))
current = round(current * 1.006, 3)
print("After 3 years: " + str(current))
# if CD
elif userInput == "CD":
# get amount invested
investment = float(input("\nHow much would you like to invest? "))
print()
# if amount at least 2500
if investment >= 2500:
# 2.5% increase for 4 years
current = round(investment * 1.025, 3)
print("After 1 year: " + str(current))
current = round(current * 1.025, 3)
print("After 2 years: " + str(current))
current = round(current * 1.025, 3)
print("After 3 years: " + str(current))
current = round(current * 1.025, 3)
print("After 4 years: " + str(current))
else:
# 1% increase for 4 years
current = round(investment * 1.01, 3)
print("After 1 year: " + str(current))
current = round(current * 1.01, 3)
print("After 2 years: " + str(current))
current = round(current * 1.01, 3)
print("After 3 years: " + str(current))
# if mutual funds
elif userInput == "mutual funds":
# get amount invested
investment = float(input("\nHow much would you like to invest? "))
# 5% increase for 3 years in best case scenario
print("\nBest case\n---------")
current = round(investment * 1.05, 3)
print("After 1 year: " + str(current))
current = round(current * 1.05, 3)
print("After 2 years: " + str(current))
current = round(current * 1.05, 3)
print("After 3 years: " + str(current))
# 1% decrease for 3 years in worst case
print("\nWorst case\n---------")
current = round(investment * 0.99, 3)
print("After 1 year: " + str(current))
current = round(current * 0.99, 3)
print("After 2 years: " + str(current))
current = round(current * 0.99, 3)
print("After 3 years: " + str(current))
# if bonds
elif userInput == "bonds":
# get amount invested
investment = float(input("\nHow much would you like to invest? "))
# if amount at least 5000
if investment >= 5000:
# 2% increase for 3 years
current = round(investment * 1.02, 3)
print("After 1 year: " + str(current))
current = round(current * 1.02, 3)
print("After 2 years: " + str(current))
current = round(current * 1.02, 3)
print("After 3 years: " + str(current))
else:
# can't invest
print("Investments under $5000 do not qualify for investment")
# else invalid choice
else:
print(
"Sorry, this investment calculator does not support money markets. Please try again using savings, CD, mutual funds, or bonds.")