Skip to content

Instantly share code, notes, and snippets.

@ohhh25
Created August 12, 2020 02:20
Show Gist options
  • Save ohhh25/9bede2a829733790d1927789c446f765 to your computer and use it in GitHub Desktop.
Save ohhh25/9bede2a829733790d1927789c446f765 to your computer and use it in GitHub Desktop.
Description At The Top of Code.
# This file will create a image of the integer, "1".
# The image is created with a NumPy array and then converted to a Pillow Image.
# The image is contains only one channel, "L", with a dtype of "np.uint8".
# I made this file to have fun and also understand how images on a computer
# work.
# I know most of my code could go into a function or many functions but because
# I am doing this for fun, I was too lazy.
# I was also trying to follow the PEP 8 Python Style, if I failed somewhere to
# follow PEP 8, please do not hesitate to email me at, "lukascao06@gmail.com".
# You may take this code to use or play around with. Enjoy and Have Fun!!!!
# imports
import numpy as np
from PIL import Image
# setting up image
img_w, img_h = 200, 200
data = np.zeros((img_h, img_w), dtype=np.uint8)
data[40:160, 90:110] = 255
data[160:180, 60:140] = 255
# my magic global variables that will be modified
start_y = 40
stop_y = 50
start_x = 90
stop_x = 110
index = 0
# logic for image of integer "1"
for size in range(10):
data[start_y:stop_y, start_x:stop_x] = 255
if size == 9:
data[int((start_y + stop_y) / 2):stop_y, start_x:stop_x] = 0
else:
start_y += 5
stop_y += 5
start_x -= 4
stop_x -= 4
for test in range(4):
data[start_y:int((start_y + stop_y) / 2), start_x + test] = 0
start_x += 4
for portion in range(20):
data[(start_y + index):int((start_y + stop_y) / 2), start_x] = 0
if (portion + 1) % 4 == 0:
index += 1
start_x += 1
# image saving
img = Image.fromarray(data, 'L')
img.save('1.png')
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment