Wednesday, June 29, 2022

Create Index for text file (Idea from 9 Algorithms that changed the world - Google PageRank)

# Contents of Names.txt

Abdul Abdul Babu Geetha Joseph Leela Mary Mohammed Muhammed Suresh

Abdul Rajan Thomas Geetha Leela Mini Omana Rajesh Usha

Abdul Biju Fathima Geetha Muhammad Sindhu


filename = "Names.txt"

temp = "Temp.txt"


S = ''

def Eliminate(): # Add only alphabets, space & newline to string S

  inn = open(filename)

  s = inn.read()

  global S

  for i in s:

    if i.isalpha() or i == ' ' or i == '\n':

      S += i

    if i == '-':  # Hyphenated words become 2 words

      S += ' '

  inn.close()


def WriteToTemp(): # Write S to Temporary file

  out = open(temp, 'w')

  out.write(S)

  out.close()


def Indexing(): # 1st Indexing method

  Index = {}    # File index

  lineNo = 0

  inn = open(temp)

  

  while True:

    D = {}  # Line index

    line = inn.readline()

    if line == '':

      break

    lineNo += 1

    L = line.split()

    for word in L:  # Build line index

      if word in D.keys():

        D[word] += 1

      else:

        D[word] = 1


    for k, v in D.items():  # Use line index to

      if k in Index.keys(): # build file index

        Index[k].append(lineNo)

      else:

        Index[k] = [lineNo]


  for k, v in Index.items():

    print(k, v)


def Indexing(): # 2nd Indexing method

  lineNo = 0

  inn = open(temp)

  D = {}

  

  while True:

    line = inn.readline()

    if line == '':

      break

    lineNo += 1

    L = line.split()

    for word in L:  # Build index

      if word not in D.keys():

        D[word] = {lineNo}

      else:

        D[word].add(lineNo)


  for k, v in D.items():

    print(k, v)


Eliminate()

WriteToTemp()

Indexing()


Friday, June 24, 2022

Split Screen Videos with MoviePy (Neural Nine) (NeuralNine)

# https://www.youtube.com/watch?v=p293_8oYXL4

# pip install moviepy


from moviepy.editor import VideoFileClip, clips_array


clip1 = VideoFileClip("1.mp4").subclip(0, 5).margin(8) # From 0 secs

clip2 = VideoFileClip("2.mp4").subclip(0, 5).margin(8) # to 5 secs

clip3 = VideoFileClip("A.mp4").subclip(0, 5).margin(8) # Margin of 8

clip4 = VideoFileClip("D.mp4").subclip(0, 5).margin(8) # pixels


combined = clips_array([[clip1, clip2],

                                        [clip3, clip4]])

combined.write_videofile("Test.mp4")


Tuesday, June 21, 2022

Create & format MS-Word file in Python

# Create & format MS-Word file in Python

# https://www.youtube.com/watch?v=so2illANiRw


from docx import Document


doc = Document()

doc.add_heading("Hello World", 0)

doc.add_heading("Hello World")

doc.add_heading("Hello World", 2)

doc.add_heading("Hello World", 6)


p = doc.add_paragraph("Sample text")

p.add_run(" This text is bold.").bold = True

p.add_run(" This text is italicised.").italic = True


doc.add_paragraph("Item 1", style="List Bullet")

doc.add_paragraph("Item 2", style="List Bullet")

doc.add_paragraph("Item 3", style="List Bullet")


table_header = ["Name", "Age", "Job"]

data = [["John", 46, "Programmer"],

        ["Mary", 55, "Programmer"],

        ["Anna", 27, "Accountant"],

        ["Bob",  46, "Chef"]

       ]

table = doc.add_table(rows=1, cols=3)

for i in range(3):

  table.rows[0].cells[i].text = table_header[i]


for name, age, job in data:

  cells = table.add_row().cells

  cells[0].text = name

  cells[1].text = str(age)

  cells[2].text = job


doc.add_page_break()

doc.add_paragraph("Hello new page")

doc.add_picture("Thejus.png")


doc.save("Test.docx")

Saturday, June 4, 2022

Write text to image (Using textwrap) (LimeGuru)

# https://www.youtube.com/watch?v=g85jQfhG2MU


import cv2, textwrap


def put_text(img, text, x_value, y_value):

  font = cv2.FONT_HERSHEY_DUPLEX

  wrapped_text = textwrap.wrap(text, width=10)

  x, y = 200, 40

  font_size = 2

  font_thickness = 3


  for i, line in enumerate(wrapped_text):

    textsize = cv2.getTextSize(line, font, font_size, font_thickness)[0]

    gap = textsize[1] + 40

    y = y_value + i * gap

    x = int((img.shape[1] - textsize[0]) / 2)

    cv2.putText(img, line, (x_value, y), font, font_size,

                (255, 255, 255), font_thickness, lineType=cv2.LINE_AA)


img = cv2.imread("Image.jpg")

put_text(img, "Creative Minds Are Best", 80, 150)

cv2.imwrite("Output.jpg", img)