from random import randint from PIL import Image class TextAnalyzer(): def __init__(self): self.sentence_lengths = None self.word_lengths = None self.letter_freq = None self.letter_correl = None def analyze_text_letters(self, filename): self.sentence_lengths = {} self.word_lengths = {} self.letter_freq = {} self.letter_correl = {} input_file = open(filename, "r", encoding="utf8") sentences = input_file.read()\ .replace('"', "") \ .replace("'", "") \ .replace('-', "") \ .replace('‘', "") \ .replace('’', "") \ .replace(';', "") \ .replace('*', "") \ .replace(',', "") \ .replace('“', "") \ .replace('[', "") \ .replace('(', "") \ .replace(')', "") \ .replace(':', "") \ .replace("\n", "") \ .replace("\t", "") \ .strip() \ .replace('?', ".") \ .replace("!", ".") \ .lower() \ .split(".") print(sentences) for sentence in sentences: words = sentence.split(" ") if len(words) not in self.sentence_lengths: self.sentence_lengths[len(words)] = 1 else: self.sentence_lengths[len(words)] += 1 for w in words: if len(w) not in self.word_lengths.keys(): self.word_lengths[len(w)] = 1 else: self.word_lengths[len(w)] += 1 for i in range(len(w)): if w[i] not in self.letter_freq.keys(): self.letter_freq[w[i]] = 1 else: self.letter_freq[w[i]] += 1 if i > 0: if w[i - 1] not in self.letter_correl.keys(): self.letter_correl[w[i - 1]] = {} if w[i] not in self.letter_correl[w[i - 1]].keys(): self.letter_correl[w[i - 1]][w[i]] = 1 else: self.letter_correl[w[i - 1]][w[i]] += 1 else: # first letter in a word if " " not in self.letter_correl.keys(): self.letter_correl[" "] = {} if w[i] not in self.letter_correl[" "].keys(): self.letter_correl[" "][w[i]] = 1 else: self.letter_correl[" "][w[i]] += 1 def print_stats(self): print("Sentence lengths") print(self.sentence_lengths) print() print("Word lentghs") print(self.word_lengths) print() print("Letter frequency") print("\t" + str(sorted(self.letter_freq.items(), key=lambda s: -s[1])[:8]) + "...") print() print("Letter corelation") for c in self.letter_correl.keys(): print("\t" + c + ":\t" + str(sorted(self.letter_correl[c].items(), key=lambda s: -s[1])[:8]) + "...") print() def generate_letter(self, prev=" "): prev = prev.lower() d = self.letter_correl[prev] r = randint(0, sum(d.values())-1) for i in d.items(): r -= i[1] if r < 0: return i[0] def generate_word_length(self): r = randint(0, sum(self.word_lengths.values())) #print(r) for i in self.word_lengths.items(): r -= i[1] if r <= 0: return i[0] def generate_word(self, first_capital=False): result = "" word_length = self.generate_word_length() if word_length > 0: result += self.generate_letter() if first_capital: result = result.upper() for _ in range(word_length-1): result += self.generate_letter(prev=result[-1]) return result def generate_sentence_length(self): r = randint(0, sum(self.sentence_lengths.values())) # print(r) for i in self.sentence_lengths.items(): r -= i[1] if r <= 0: return i[0] def generate_sentence(self): result = [] sentence_length = self.generate_word_length() if sentence_length > 0: result.append(self.generate_word(first_capital=True)) for _ in range(sentence_length - 1): result.append(self.generate_word()) return " ".join(result) + "." if len(result) > 0 else "" # with open("find_this_file", 'w') as file: # file.write("here i am") # file.close() # infile = open("alice.txt", "r", encoding="UTF8") # text = infile.read() # infile.close() # outfile = open("test.txt", "w") # text_processed = text.replace("A", "#") # outfile.write(text_processed) # outfile.close() analyzer = TextAnalyzer() analyzer.analyze_text_letters("alice.txt") analyzer.print_stats() # for i in range(10): # print(analyzer.generate_sentence()) def draw_circle(im_size, r): center = im_size // 2 im = Image.new("RGB", (im_size, im_size), (255, 255, 255)) for i in range(center-r, center+r): for j in range(center-r, center+r): if (center - i)**2 + (center - j)**2 <= r**2: im.putpixel((i, j), (0, 0, i)) #im.putpixel((i, j), (0, 255, 0)) im.save("zz_circ.png") def draw_square(im_size, a): center = im_size // 2 im = Image.new("RGB", (im_size, im_size), (255, 255, 255)) for i in range(center-a//2, center+a//2): for j in range(center-a//2, center+a//2): im.putpixel((i, j), (0, 0, 255)) im.save("zz_square.png") def draw_gradient(im_size): im = Image.new("L", (im_size, im_size)) for i in range(0, im_size): for j in range(0, im_size): new_color = min(i, 255) im.putpixel((i, j), new_color) im.save("zz_grad.png") def clonestamp(filename, stamp_size, source_x, source_y, target_x, target_y): im = Image.open(filename) for x in range(stamp_size): for y in range(stamp_size): new_color = im.getpixel((source_x + x, source_y + y)) im.putpixel((target_x + x, target_y + y), new_color) im.save("zz_cloned.png") def process_image(filename): im = Image.open(filename) for i in range(im.width): for j in range(im.height): old_color = im.getpixel((i, j)) new_color = (min(255, old_color[0]-50), min(255, old_color[1]-50), min(255, old_color[1]-50)) #new_color = (255-old_color[0],255-old_color[1], 255-old_color[2]) #new_color = (min(255, old_color[0]-50), min(255, old_color[1]-50), min(255, old_color[1]-50)) im.putpixel((i, j), new_color) im.save("zz_processed.png") draw_square(250, 60) process_image("input.jpg") draw_gradient(400) clonestamp("input.jpg", 60, 100, 100, 170, 170) draw_circle(250, 70)