Tuesday, April 19, 2022

View maps with tkintermapview

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


from tkinter import *

import tkintermapview


win = Tk()

win.title("Tkinter MapView")

win.geometry("900x700")


lbl1 = LabelFrame(win)

lbl1.pack(pady=20)


map_widget = tkintermapview.TkinterMapView(lbl1, width=800, height=600, corner_radius=0)

#map_widget.set_position(9.3346, 76.7077) # Kozhencherry

map_widget.set_zoom(20) # Max zoom


map_widget.set_address("Dilkusha Street, Kolkata 700017, West Bengal, India")

#map_widget.set_address("Thiruvalla Kumbazha Road, Kozhencherry, Kerala, India")

map_widget.pack()


win.mainloop()

Sunday, April 17, 2022

Convert images to movie (Using moviepy.editor)

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


from moviepy.editor import *


clips = []

clip1 = ImageClip("Koala.jpg").set_duration(1)

clip2 = ImageClip("Penguins.jpg").set_duration(2)

clip3 = ImageClip("Desert.jpg").set_duration(3)


clips.append(clip1)

clips.append(clip2)

clips.append(clip3)


video_clip = concatenate_videoclips(clips, method='compose')

video_clip.write_videofile("Video_output.mp4", fps=24, remove_temp=True, codec="libx264", audio_codec="aac")


Monday, April 4, 2022

Audio & Video editing with Python (MoviePy)

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


from moviepy.editor import VideoFileClip, concatenate_videoclips, vfx

from moviepy.editor import AudioFileClip, afx, CompositeAudioClip


clip1 = VideoFileClip("1.mp4").subclip(0, 5)

clip2 = VideoFileClip("1.mp4").subclip(5, 10).fx(vfx.colorx, 1.5).fx(vfx.lum_contrast, 0, 50, 128)

clip3 = VideoFileClip("1.mp4").subclip(10, 15)

clip4 = VideoFileClip("1.mp4").subclip(15, 20).fx(vfx.colorx, 1.5).fx(vfx.lum_contrast, 0, 50, 128)

combined = concatenate_videoclips([clip1, clip2, clip3, clip4])

combined.write_videofile("Combined1.mp4")


clip1 = VideoFileClip("1.mp4").subclip(0, 5).fx(vfx.fadein, 1).fx(vfx.fadeout, 1)

clip2 = VideoFileClip("1.mp4").subclip(5, 10).fx(vfx.fadein, 1).fx(vfx.fadeout, 1)

clip3 = VideoFileClip("1.mp4").subclip(10, 15).fx(vfx.fadein, 1).fx(vfx.fadeout, 1)

clip4 = VideoFileClip("1.mp4").subclip(15, 20).fx(vfx.fadein, 1).fx(vfx.fadeout, 1)

combined = concatenate_videoclips([clip1, clip2, clip3, clip4])

combined.write_videofile("Combined2.mp4")


audio = AudioFileClip("1.mp3").fx(afx.audio_fadein, 1)  # Could pull audio from .mp4 also

combined = concatenate_videoclips([clip1, clip2, clip3, clip4])

combined.audio = CompositeAudioClip([audio])

combined.write_videofile("Combined.mp4")


Store Passwords Safely in Python (the bcrypt library)

Store Passwords Safely in Python (the bcrypt library)

When storing passwords, one of the greatest risks is that someone may steal the database and be able to decrypt them. To avoid this, you could hash the passwords before storing them. We need to install bcrpyt using pip: pip install bcrypt

It is good practice (although not required) to create a new virtual environment for this project. If you want to learn more about this, check How to Create and Manage Virtual Environments in Python

What is Password Hashing?

A hashing function is a function that takes a string of bytes, and “transforms” it to another string of bytes, from which it is impossible to determine the initial string. Furthermore, there aren’t two different inputs that give the same output. This means that if we store the hashed password, even if someone stole the database they would not be able to determine what the plain text passwords are.

Suppose a user has registered with the password "HelloWorld". To execute the login, we check if the password entered is the same as the stored one. To do so, we hash the password used for the login, and check if this hash corresponds to the stored one. Since by the definition there aren’t two different inputs that give the same output, the two hashes will be equal only if the password entered by the user is the same as the one used during registration.

The only weakness is that if the password is short, an attacker may try to hash all possible passwords until he finds the correct one. However, this is unlikely if the password is long enough since there are too many combinations. But how to make sure that the password is long? Usually, before hashing a password we will add a salt, i.e. a random sequence of characters. In this way, we know that even if the user uses a short password, it will still be secure.

Testing the Code

Here is the complete code of our PasswordDatabase:

import bcrypt

 

class PasswordDatabase:

  def __init__(self):

    self.data = dict()

  def register(self, user, password):

    if user in self.data:

      return False

    hashed = bcrypt.hashpw(password, bcrypt.gensalt(10))

    self.data[user] = hashed

    return True

  def login(self, user, password):

    if user not in self.data:

      return False

    pwd_bytes = password.encode("utf-8")

    return bcrypt.checkpw(pwd_bytes, self.data[user])

# Driver code:

 

db = PasswordDatabase()

print("Registering users")

print(db.register("John", b"password"))

print(db.register("Seth", b"HelloWorld"))

print(db.register("John", b"myname"))   # False - John already exists

 

print("Logging in users")

print(db.login("Abc", "password"))

print(db.login("John", "pwd"))

print(db.login("John", "password"))     # True - matches password

Useful links:

Python bcrypt tutorial shows how to hash passwords in Python with the bcrypt library… zetcode.com

Hashing Passwords In Python: Bcrypt Tutorial with Examples | HackerNoon

Create Word Cloud (WordCloud) using Python



# https://youtu.be/vRbSnlRyJNQ

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import numpy as np
import matplotlib.pyplot as plt
import PIL.Image

#STOPWORDS.add("ABCD")  Add other words to be excluded
#print(STOPWORDS)
text = "Python is a high-level, general-purpose programming language. \
Its design philosophy emphasizes code readability, using significant \
indentation. Its language constructs & OOPs approach aim to help \
programmers write clear, logical code for small and large-scale projects. \
Python is dynamically-typed and garbage-collected. It supports multiple \
programming paradigms, including structured (particularly procedural), \
object-oriented and functional programming. It is often described as a \
\"batteries included\" language due to a comprehensive standard library. \
Guido van Rossum began working on Python in the 1980s as a successor to \
the ABC programming language and released it in 1991 as Python 0.9.0. \
Python 2.0 was released in 2000 and introduced new features such as list \
comprehensions, cycle-detecting garbage collection, reference counting, & \
Unicode support. Python 3, released in 2008, was a major revision that is \
not completely backward-compatible with earlier versions. Python 2 was \
discontinued with in 2020. Python consistently ranks as one of the most \
popular programming languages."

'''
wc = WordCloud(stopwords=STOPWORDS).generate(text)
plt.imshow(wc)
plt.axis("off")
plt.show()
'''

python_mask = np.array(PIL.Image.open("PythonLogo1.png"))
colormap = ImageColorGenerator(python_mask)
wc = WordCloud(stopwords=STOPWORDS, mask=python_mask,
               background_color="white", contour_color="black",
               contour_width=15, min_font_size=3,
               max_words=400).generate(text)
wc.recolor(color_func=colormap)
plt.imshow(wc)
plt.axis("off")
plt.show()