Saturday, December 26, 2020

Rock Paper Scissors in Tkinter

 







RPS.png is 446 x 446
Other 3 images are 58 x 46


from tkinter import *

from random import randint

import time


win = Tk()

win.title("ROCK - PAPER - SCISSORS")

win.geometry("800x560+120+0")

win.configure(bg='black')


def Rock():

  pic = PhotoImage(file="Rock.gif")

  CChoice = randint(1, 3)

  if CChoice == 1:

    lblResult.config(text="Both chose Rock - Draw", bg="black", fg="white")

  elif CChoice == 2:

    lblResult.config(text="Computer's Paper covers Your Rock - Computer Wins", bg="red", fg="white")

    I1.set(I1.get() + 1)

  else:

    lblResult.config(text="Your Rock smashes Computer's Scissors - You Win", bg="green", fg="white")

    I2.set(I2.get() + 1)

      

def Paper():

  pic = PhotoImage(file="Paper.gif")

  CChoice = randint(1, 3)

  if CChoice == 1:

    lblResult.config(text="Your Paper covers Computer's Rock - You Win", bg="green", fg="white")

    I2.set(I2.get() + 1)

  elif CChoice == 2:

    lblResult.config(text="Both chose Paper - Draw", bg="black", fg="white")

  else:

    lblResult.config(text="Computer's Scissors cuts Your Paper - Computer Wins", bg="red", fg="white")

    I1.set(I1.get() + 1)

    

def Scissors():

  pic = PhotoImage(file="Scissors.gif")

  CChoice = randint(1, 3)

  if CChoice == 1:

    lblResult.config(text="Computer's Rock smashes Your Scissors - Computer Wins", bg="red", fg="white")

    I1.set(I1.get() + 1)

  elif CChoice == 2:

    lblResult.config(text="Your Scissors cuts Computer's Paper - You Win", bg="green", fg="white")

    I2.set(I2.get() + 1)

  else:

    lblResult.config(text="Both chose Scissors - Draw", bg="black", fg="white")


pic1 = PhotoImage(file="RPS.gif")

lblMain = Label(win, image=pic1, bg="black");  lblMain.place(x=185, y=0)


I1, I2 = IntVar(), IntVar()

lblCompScore1 = Label(win, text="Computer score", bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblCompScore1.place(x=20, y=30)

lblCompScore2 = Label(win, textvariable=I1, bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblCompScore2.place(x=100, y=70)

lblPlayerScore1 = Label(win, text="Player score", bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblPlayerScore1.place(x=630, y=30)

lblPlayerScore2 = Label(win, textvariable=I2, bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblPlayerScore2.place(x=690, y=70)


lblResult = Label(win, bg="white", width=44, font=("Helvetica", 16, "bold"), text="Hello")

lblResult.place(x=120, y=460)


pic2     = PhotoImage(file="Rock.gif")

rock     = Button(win, image=pic2, bg="black", command=Rock)

rock.place(x=300, y=500)

pic3     = PhotoImage(file="Paper.gif")

paper    = Button(win, image=pic3, bg="black", command=Paper)

paper.place(x=380, y=500)

pic4     = PhotoImage(file="Scissors.gif")

scissors = Button(win, image=pic4, bg="black", command=Scissors)

scissors.place(x=450, y=500)


win.mainloop()


Thursday, December 10, 2020

Generating ASCII Art from image using Python


   

# https://dev.to/anuragrana/generating-ascii-art-from-colored-image-using-python-4ace

import sys

from PIL import Image


image_path = "Thejus1.png"

img = Image.open(image_path)


width, height = img.size

aspect_ratio = height/width

new_width = 120

new_height = aspect_ratio * new_width * 0.55

img = img.resize((new_width, int(new_height)))

img = img.convert('L')


pixels = img.getdata()


chars = ["B","S","#","&","@","$","%","*","!",":"," "]

new_pixels = [chars[pixel//25] for pixel in pixels]

new_pixels = ''.join(new_pixels)


new_pixels_count = len(new_pixels)

ascii_image = [new_pixels[index:index + new_width] for index in range(0, new_pixels_count, new_width)]

ascii_image = "\n".join(ascii_image)

print(ascii_image)


with open("ThejusASCII.txt", "w") as f:

  f.write(ascii_image)


Friday, December 4, 2020

Grayscale & Dither in Python

 


#https://www.youtube.com/watch?v=3RVnDX8cO4s&list=PL1H1sBF1VAKXCayO4KZqSmym2Z_sn6Pha&index=5


from tkinter import *

from PIL import ImageTk, Image


win = Tk()

win.title("Converting images")

win.geometry("600x510+50+50")


file1 = "..\\Images\\Originals\\Elephant.png"

img1 = Image.open(file1)


def Select(e):

  global select

  selected = lbox.curselection()

  select = lbox.get(selected)


  img = Image.open("..\\Images\\Originals\\Elephant.png")

  if select == "Normal":

    imgConv = img.convert(None)

  elif select == "Grayscale":

    imgConv = img.convert("L")

  elif select == "Dither":

    imgConv = img.convert("1")


  imgConv.save("..\\Images\\Modified\\ElephantConvert.png")

  pic = PhotoImage(file="..\\Images\\Modified\\ElephantConvert.png")

  lbl1.config(image=pic)

  lbl1.image = pic


pic = PhotoImage(file=file1)

lbl1 = Label(win, image=pic)

lbl1.place(x=30, y=60)

lbl1.image = pic


lbox = Listbox(win, height=3, font=("Arial", 11))

lbox.insert(1, "Normal")

lbox.insert(2, "Grayscale")

lbox.insert(3, "Dither")

lbox.place(x=215, y=410)

lbox.bind('<<ListboxSelect>>', Select)


win.mainloop()



Blend images with PIL in Python


# https://www.youtube.com/watch?v=7IdBppdM23Y&list=PL1H1sBF1VAKXCayO4KZqSmym2Z_sn6Pha&index=3

# Before loading files, ensure images to be blended have same dimensions


from tkinter import *

from PIL import ImageTk, Image


win = Tk()

win.title("Blending images")

win.geometry("720x700")


file1 = "..\\Images\\Amazon.png"

file2 = "..\\Images\\Elephant.png"


def Save(e):

  img1 = Image.open(file1)

  img2 = Image.open(file2)

  imgBlend = Image.blend(img1, img2, D.get()/100)

  imgBlend.save('..\\Images\\Blended.png')

  pic = PhotoImage(file='..\\Images\\Blended.png')

  lblBlend.config(image=pic)

  lblBlend.image = pic

  del img1, img2

  

D = DoubleVar()

scl1 = Scale(win, variable=D, from_=0, to=100, orient=HORIZONTAL, length=600, sliderlength=10)

scl1.bind("<Motion>", Save)

scl1.place(x=30, y=10)


openImage1 = Image.open("..\\Images\\Amazon.png")

openImage1.thumbnail((300, 300))

img1 = ImageTk.PhotoImage(openImage1)

lbl1 = Label(win, image=img1)

lbl1.place(x=30, y=60)


openImage2 = Image.open("..\\Images\\Elephant.png")

openImage2.thumbnail((300, 300))

img2 = ImageTk.PhotoImage(openImage2)

lbl2 = Label(win, image=img2)

lbl2.place(x=375, y=60)


openImage2 = Image.open("..\\Images\\Amazon.png")

imgStart = ImageTk.PhotoImage(openImage2)

lblBlend = Label(win, image=imgStart)

lblBlend.place(x=75, y=260)


Thursday, December 3, 2020

Blur Types (Simple, Box, Gaussian) in PIL using Tkinter


from tkinter import *

from PIL import ImageTk, ImageFilter, Image


win = Tk()

win.title("Blur Image")

win.geometry("600x600")


D = IntVar()


def Blur(type):

  d = D.get()     # Convert Tkinter variable to object

  oriImage = Image.open('..\\Images\\Boy.png')

  if type == "Simple":

    blurImage = oriImage.filter(ImageFilter.BLUR)

  elif type == "Box":

    blurImage = oriImage.filter(ImageFilter.BoxBlur(d))

  elif type == "Gaussian":

    blurImage = oriImage.filter(ImageFilter.GaussianBlur(d))


  blurImage.save('..\\Images\\BoyBlur.png')

  pic1 = PhotoImage(file="..\\Images\\BoyBlur.png")

  lbl.config(image=pic1)

  lbl.image = pic1


scl1 = Scale(win, variable=D, from_=0, to=20, orient=HORIZONTAL)

scl1.place(x=30, y=10)


btnSimpleBlur   = Button(win, text="Simple Blur",   command=lambda:Blur("Simple"))

btnSimpleBlur.place(x=50, y=500)

btnBoxBlur      = Button(win, text="Box Blur",      command=lambda:Blur("Box"))

btnBoxBlur.place(x=150, y=500)

btnGaussianBlur = Button(win, text="Gaussian Blur", command=lambda:Blur("Gaussian"))

btnGaussianBlur.place(x=250, y=500)


openImage = Image.open("..\\Images\\Boy.png")

img = ImageTk.PhotoImage(openImage)

lbl = Label(win, image=img)

lbl.place(x=30, y=60)


win.mainloop()


Rotate image using PIL



from tkinter import *

from PIL import ImageTk, Image


win = Tk()

win.title("Rotate Image")

win.geometry("300x300")


I = IntVar()

  

def Save(e):

  degree = I.get()

  openImage = Image.open("..\\Images\\Python.png")

  rotatedImage = openImage.rotate(degree)

  rotatedImage.save('..\\Images\\PythonRotated.png')

  pic1 = PhotoImage(file="..\\Images\\PythonRotated.png")

  lbl.config(image=pic1)

  lbl.image = pic1


scl1 = Scale(win, variable=I, from_=0, to=360, orient=HORIZONTAL)

scl1.bind("<ButtonRelease-1>", Save)

scl1.place(x=30, y=10)


openImage = Image.open("..\\Images\\Python.png")

img = ImageTk.PhotoImage(openImage)

lbl = Label(win, image=img)

lbl.place(x=30, y=60)


win.mainloop()