Created
August 18, 2020 23:22
-
-
Save RyanFleck/280c9740f2321b0d497cb874c781204c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from PIL import Image, ImageDraw, ImageFont, ImageFilter | |
from sys import exit | |
import random | |
""" | |
This is a script to demonstrate profile image generation for wise users. | |
TASK: | |
3. (Bonus) Draw cool profile pics. | |
- Crop to random part of image | |
- Apply different colors | |
- Apply different filters | |
REQUIREMENTS: | |
1. Pillow library | |
2. person_mask_inv_ww.png -> Person cutout with person in WHITE and background in TRANSPARENCY | |
""" | |
def main(): | |
print("\n\nmain:\n Running pillow tests...") | |
# Generate 10 "Wise Guys". | |
for x in range(1, 11): | |
filename = f"wiseguy_{x}.png" | |
print(f"\n\nmain:\n Generating {filename} ...") | |
generate_wiseguy(filename) | |
print("\n\nmain:\n Done.") | |
exit(0) | |
def generate_wiseguy(save_to_filename): | |
print("generate_wiseguy:\n Making a new Wise Guy...") | |
pic = Image.new('RGBA', (800, 800), 'white') | |
pic_base = Image.new('RGBA', (600, 600), 'white') | |
mask = load_image("person_mask_inv_ww.png") | |
draw = ImageDraw.Draw(pic) | |
""" | |
Square Coords: | |
0,0 400,0 800,0 | |
0,400 400,400 800,400 | |
0,800 400,800 800,800 | |
""" | |
print("generate_wiseguy:\n Add colored squares.") | |
draw_color(draw, (0, 0, 400, 400)) | |
draw_color(draw, (400, 0, 800, 400)) | |
draw_color(draw, (0, 400, 400, 800)) | |
draw_color(draw, (400, 400, 800, 800)) | |
print("generate_wiseguy:\n Rotate image.") | |
rotated = pic.rotate(10) | |
cropped = rotated.crop((100, 100, 700, 700)) | |
print("generate_wiseguy:\n Blur image.") | |
blurred = cropped.filter(ImageFilter.BoxBlur(radius=90)) | |
blurred = blurred.filter(ImageFilter.BoxBlur(radius=90)) | |
blurred = blurred.filter(ImageFilter.BoxBlur(radius=90)) | |
blurred = blurred.filter(ImageFilter.BoxBlur(radius=90)) | |
blurred = blurred.filter(ImageFilter.BoxBlur(radius=90)) | |
blurred = blurred.filter(ImageFilter.BoxBlur(radius=90)) | |
blurred = blurred.filter(ImageFilter.BoxBlur(radius=90)) | |
print("generate_wiseguy:\n Add mask to image.") | |
pic_base.paste(blurred, mask=mask) | |
#final_pic = pic_base.effect_spread(20) | |
#effect = pic_base.filter(ImageFilter.DETAIL) | |
print("generate_wiseguy:\n Saving image.") | |
final_pic = pic_base | |
print_image_info(final_pic) | |
final_pic.save(save_to_filename) | |
def draw_color(draw_obj, coords): | |
random_colors_v1 = [ | |
"red", | |
"blue", | |
"limegreen", | |
"yellow", | |
"purple", | |
"magenta", | |
"orange", | |
"salmon"] | |
random_colors_v2 = ["blue", "yellow", "purple", "orange", "magenta"] | |
color = random.choice(random_colors_v1) | |
print(f"draw_color:\n Painting a {color} rectangle at {coords}") | |
draw_obj.rectangle(coords, fill=color) | |
def convert_to_jpg(image: Image, image_title: str) -> Image: | |
"""Returns a new copy of the image, converted to jpg.""" | |
print("convert_to_jpg:\n Converting image...") | |
jpg_image = file(image_title) | |
image.save(jpg_image, 'jpg') | |
img = Image.open(jpg_image) | |
return img | |
def resize(image: Image) -> Image: | |
width = image.size[0] | |
height = image.size[1] | |
newsize = (int(width / 4), int(height / 4)) | |
resized_image = image.resize(newsize, resample=Image.LANCZOS) | |
print(f"resize:\n Image was resized to {newsize}") | |
print_image_info(resized_image) | |
return resized_image | |
def load_image(img_filename: str) -> Image: | |
"""Loads an image into memory.""" | |
try: | |
img = Image.open(img_filename.strip()) | |
print(f"load_image:\n Loaded image, type: {type(img)}") | |
return img | |
except IOError: | |
print("load_image:\n Failed to load image.") | |
exit(1) | |
def print_image_info(image: Image) -> None: | |
height = image.size[1] | |
width = image.size[0] | |
print( | |
f"print_image_info:\n Got image, format is {image.format}, size is {width}x{height} WxH, mode is {image.mode}.") | |
def watermark(image: Image, text: str) -> None: | |
"""Applies a watermark to the passed image object.""" | |
idraw = ImageDraw.Draw(image) | |
height = image.size[1] | |
width = image.size[0] | |
font_size = int(max(height, width) / 30) | |
font = ImageFont.truetype("FreeMono.ttf", size=font_size) | |
fill = "#000000" | |
idraw.text((10, 10), text, fill=fill, font=font) | |
print(f"watermark:\n Applied watermark, font size {font_size}.") | |
def save_original_format(image: Image, file_title: str, file_extension: str): | |
image.save(f"{file_title}_mod.{file_extension}") | |
def save_as_png(image, file_title): | |
pass | |
def save_as_jpg(image, file_title): | |
pass | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment