Thursday, November 26, 2020

Create QR codes, embed QR code in image, embed image in QR code, using Python

Create QRCODE


import qrcode


img = qrcode.make('Binoy Thomas')

print(type(img))   # <class 'qrcode.image.pil.PilImage'>

print(img.size)    # (290, 290)

img.save('QR.png')





Embed QRCode in image



import qrcode
from PIL import Image

img = Image.open('Anupam.jpg')
info = '''Actor Anupam Kher was born on 7 March 1955 in \
Shimla. He is regarded as an outstanding cineaste and is \
the recipient of 2 National Film Awards and 8 Filmfare \
Awards. Kher has appeared in over 500 films in several \
languages. His break-through performance was in Saaransh \
(1984), where he essayed the role of a septugenerian, when \
still in his 20's. His biography "Lessons Life Taught Me \
Unknowingly" is published by Penguin Random House.'''

qr = qrcode.QRCode(box_size=2)
qr.add_data(info)
qr.make()
Qr = qr.make_image()

pos = (img.size[0] - Qr.size[0], img.size[1] - Qr.size[1])

img.paste(Qr, pos)
img.save('Anupam1.png')



Embed image in QRCode



import qrcode
from PIL import Image

face = Image.open('AnupamSmall.jpg').crop((100, 40, 175, 125))

Qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
Qr.add_data('I am Anupam')
Qr.make()
img = Qr.make_image().convert('RGB')

pos = ((img.size[0] - face.size[0])//2, (img.size[1] - face.size[1])//2)

img.paste(face, pos)
img.save('AnupamSmall.png')


Generate QR Code within GUI

import qrcode
from tkinter import *

qr = qrcode.QRCode(version=1, error_correction=qrcode.ERROR_CORRECT_L,
                   box_size=10, border=4)

win = Tk()
win.geometry("500x500")

S1 = StringVar()
S1.set("https://www.google.com")
ent = Entry(win, width=30, textvariable=S1, font=("Verdana", 16, "bold"))
ent.place(x=20, y=0)
ent.focus()

def generateQRCode():
  qr.add_data(S1)
  qr.make() # generate QR code
  img = qr.make_image()
  img.save("QRCode.png")
  pic = PhotoImage(file="QRCode.png")
  lbl = Label(win, image=pic)
  lbl.place(x=110, y=50)
  lbl.config(image=pic)
  lbl.image=pic

btnGenerate = Button(win, text="Generate", font=("Verdana", 16, "bold"), command=generateQRCode)
btnGenerate.place(x=185, y=400)

win.mainloop()

Saturday, November 14, 2020

Steganography using Python with tkinter

 https://www.section.io/engineering-education/steganography-in-python/




# pip install opencv-python

from tkinter import *

from PIL import Image, ImageTk

from tkinter import filedialog

import cv2

import numpy as np

import math


global path_image


image_display_size = 300, 300


def OnClick():

  global path_image

  path_image = filedialog.askopenfilename()

  load_image = Image.open(path_image)

  load_image.thumbnail(image_display_size, Image.ANTIALIAS)

  np_load_image = np.asarray(load_image)

  np_load_image = Image.fromarray(np.uint8(np_load_image))

  render = ImageTk.PhotoImage(np_load_image)

  img = Label(app, image=render)

  img.image = render

  img.place(x=20, y=50)


def Encrypt():

  global path_image

  data = txt.get(1.0, "end-1c")

  img = cv2.imread(path_image)

  data = [format(ord(i), '08b') for i in data]    # break image to binary chararacters

  _, width, _ = img.shape

  PixReq = len(data) * 3                 # algorithm to encode the image


  RowReq = PixReq/width

  RowReq = math.ceil(RowReq)


  count = 0

  charCount = 0

  for i in range(RowReq + 1):

    while(count < width and charCount < len(data)):

      char = data[charCount]

      charCount += 1

      for index_k, k in enumerate(char):

        if((k == '1' and img[i][count][index_k % 3] % 2 == 0) or (k == '0' and img[i][count][index_k % 3] % 2 == 1)):

          img[i][count][index_k % 3] -= 1

        if(index_k % 3 == 2):

          count += 1

        if(index_k == 7):

          if(charCount*3 < PixReq and img[i][count][2] % 2 == 1):

            img[i][count][2] -= 1

          if(charCount*3 >= PixReq and img[i][count][2] % 2 == 0):

            img[i][count][2] -= 1

          count += 1

    count = 0


  cv2.imwrite("EncryptedImage.png", img)


  success_label = Label(app, text="Encrypted!", bg='lavender', font=("Helvetica", 20))

  success_label.place(x=130, y=360)


app = Tk()

app.configure(background='lavender')

app.title("Encrypt")

app.geometry('400x400')


btnSelect = Button(app, text="Select PNG Image (Max 500 x 300)", command=OnClick)

btnSelect.place(x=100, y=10)


txt = Text(app, wrap=WORD, width=30)

txt.place(x=80, y=220, height=100)


encrypt_button = Button(app, text="Encode", bg='white', fg='black', command=Encrypt)

encrypt_button.place(x=180, y=328)


app.mainloop()

#############################################################################




import cv2
from tkinter import filedialog, Tk, Button, Label
from PIL import Image, ImageTk
import numpy as np

image_display_size = 500, 350

def decrypt():
  load = Image.open("EncryptedImage.png")
  load.thumbnail(image_display_size, Image.ANTIALIAS)
  load = np.asarray(load)
  load = Image.fromarray(np.uint8(load))
  render = ImageTk.PhotoImage(load)
  img = Label(app, image=render)
  img.image = render
  img.place(x=100, y=50)

  img = cv2.imread("EncryptedImage.png")
  data = []
  stop = False
  for index_i, i in enumerate(img):
    i.tolist()
    for index_j, j in enumerate(i):
      if((index_j) % 3 == 2):
        data.append(bin(j[0])[-1])  # first pixel
        data.append(bin(j[1])[-1])  # second pixel
        if(bin(j[2])[-1] == '1'):   # third pixel
          stop = True
          break
      else:
        data.append(bin(j[0])[-1])  # first pixel
        data.append(bin(j[1])[-1])  # second pixel
        data.append(bin(j[2])[-1])  # third pixel
    if(stop):
      break

  message = []
  for i in range(int((len(data)+1)/8)):  # join bits to form letters
    message.append(data[i*8:(i*8+8)])
  message = [chr(int(''.join(i), 2)) for i in message]  # join all letters to form the message.
  message = ''.join(message)
  message_label = Label(app, text=message, bg='lavender', font=("Times New Roman", 10))
  message_label.place(x=30, y=400)

app = Tk()
app.configure(background='lavender')
app.title("Decrypt")
app.geometry('600x600')

main_button = Button(app, text="Decrypt", bg='white', fg='black', command=decrypt)
main_button.place(x=250, y=10)
app.mainloop()





Digital Clock (Blessen Aby)




from tkinter import *

import time

import sys


root=Tk()

root.title("Digital Clock")


def get_time():

  time_string=time.strftime("%I:%M:%S %p")

  clock.config(text=time_string)

  clock.after(170, get_time)


clock = Label(root, font=("time", 90, "bold"), bg="green")

clock.pack()


get_time()


root.mainloop()