Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Letters color changed using numpy and image management #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions Text To Handwriting.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,86 @@
#Importing Library
from PIL import Image
# Importing Library
from PIL import Image, ImageEnhance
from sys import argv
import numpy as np
import re #regex

# List of colors
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
purple = (142, 68, 173)

# if you'd rather not use the command line, put the path to your file here
fileName = "dummy.txt" # path of your text file
fileName = "dummy.txt" # path of your text file

# read file that user wants converted from command line. If file can't be read, assign
color = black # RGB Color, by default black


# read file that user wants converted from command line. If file can't be read, assign
# the file to a file in the directory
try:
txt=open(argv[1], "r")
txt = open(argv[1], "r")
except IndexError:
print("No file entered. Using default file...")
txt=open(fileName, "r")
txt = open(fileName, "r")
except FileNotFoundError:
print("Could not find file. Using default file...")
txt=open(fileName, "r")
txt = open(fileName, "r")


BG=Image.open("myfont/bg.png") #path of page(background)photo (I have used blank page)
sheet_width=BG.width
# path of page(background)photo (I have used blank page)
BG = Image.open("myfont/bg.png")
sheet_width = BG.width
gap, ht = 0, 0


# for each letter in the uploaded txt file, read the unicode value and replace it with
# the corresponding handwritten file in the "myfont" folder.
for i in txt.read().replace("\n",""):
lines = txt.readlines()
for line in lines:
if re.search("txtColor=", line) != None: ##Check if flag color is set
colortmp = line.split("=")[1].rstrip("\n")
print(colortmp)
if colortmp == "red": color = red
if colortmp == "blue": color = blue
if colortmp == "green": color = green
if colortmp == "purple": color = purple
if colortmp == "black": color = black
continue
for i in line:
if(i == "\n"):
print("Newline") # Accepting newline
gap, ht = 0, ht+140
continue
cases = Image.open("myfont/{}.png".format(str(ord(i))))
cases = cases.convert("RGBA")

#Add contrast to the image to make letters darkers
enhancer = ImageEnhance.Contrast(cases)
factor = 5
im_output = enhancer.enhance(factor)
data = np.array(im_output)
red, green, blue, alpha = data.T

#Getting black pixels
black_areas = (red == 0) & (blue == 0) & (green == 0)
data[..., :-1][black_areas.T] = black #Make the pixels black
try:
data[..., :-1][black_areas.T] = color #Change color of letter
except:
print("No change of color")
cases = Image.fromarray(data)

BG.paste(cases, (gap, ht))
size = cases.width
height=cases.height
#print(size)
height = cases.height
# print(size)
print("Running...........")
gap+=size
gap += size

if sheet_width < gap or len(i)*115 >(sheet_width-gap):
gap,ht=0,ht+140
if sheet_width < gap or len(i)*115 > (sheet_width-gap):
gap, ht = 0, ht+140

print(gap)
print(sheet_width)
BG.show()

10 changes: 5 additions & 5 deletions dummy.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Deep learning is an artificial intelligence function that imitates the workings of the
human brain in processing data and creating patterns for use in decision making.
Deep learning is a subset of machine learning in artificial intelligence (AI) that has
networks capable of learning unsupervised from data that is unstructured or unlabeled.
Also known as deep neural learning or deep neural network.
txtColor=red
REPORT 1. LAB

txtColor=purple
Swarm intelligence )SI( is the collective behavior of decentralized, self-organized systems, natural or artificial. The concept is employed in work on artificial intelligence. The expression was introduced by Gerardo Beni and Jing Wang in 1989, in the context of cellular robotic systems.

SI systems consist typically of a population of simple agents or boids interacting locally with one another and with their environment. The inspiration often comes from nature, especially biological systems. The agents follow very simple rules, and although there is no centralized control structure dictating how individual agents should behave, local, and to a certain degree random, interactions between such agents lead to the emergence of intelligent global behavior, unknown to the individual agents.