dropdown menu

Back To Home

Thursday, November 26, 2020

Solved: AttributeError_ type object 'Image' has no attribute 'open'

When we facing this issue, we need to add this module PIL.Image and use image=PIL.Image.open('/home/pi/Desktop/scene.jpg').


Code:

from tkinter import Tk, Canvas

from PIL import ImageTk, Image

from tkinter import *

import tkinter.font as tkFont

import PIL.Image   #Add this module

root = Tk()

topFrame =Frame(root,width=100,height=100)

topFrame.grid(row=1,column=0,rowspan=2)


def image1():

    print("hello")

#image=Image.open('/home/pi/Desktop/scene.jpg')    #comment out this

image=PIL.Image.open('/home/pi/Desktop/scene.jpg')   #use this 

photo = ImageTk.PhotoImage(image)

#a.config(image=mi,compound=BOTTOM)

a=Button(topFrame,image=photo,text="All stones preview",command=image1,height=290,width=335,font=tkFont.Font(weight="bold",size=15))

a.grid(row=1, column=1)

root.mainloop()


Video:



Sunday, September 6, 2020

Python tkinter: print preview

Print preview of text using python.


Python Code:

from tkinter import *

root = Tk()

configfile = Text(root, wrap=WORD, width=45, height= 20)

configfile.pack(fill="none", expand=TRUE)

filename='N.txt'

with open(filename,"rt", encoding="utf-8") as f:

    configfile.insert(INSERT, f.read())





Monday, June 29, 2020

Python - Read open excel file data

Sometimes we want to read real time update data for excel that haven't save.
So below code is useful.


Python code:

import win32com.client as win32
import pandas as pd

#here we use win32com to save open excel
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
    if wb.Name == 'da.xlsx' :

        print("WB:",wb.Name)
        wb.Save()

#here we use panda module to read and check
df = pd.read_excel (r'da.xlsx', sheet_name='Sheet1')
print (df)


Monday, May 25, 2020

Solved: UnicodeEncodeError: 'charmap' codec can't encode characters

Below are code to solve this error.

Code:


import xlsxwriter 
  
workbook = xlsxwriter.Workbook('skill.csv') 
  
# By default worksheet names in the spreadsheet will be  
# Sheet1, Sheet2 etc., but we can also specify a name. 
worksheet = workbook.add_worksheet("skill") 
  
# Some data we want to write to the worksheet. 
details = ( 
    ['SN', 'Name','Contribution'], 
    [1, "Linus Torvalds", "Linux Kernel"], 
    [2, "Tim Berners-Lee", "World Wide Web"], 
    [3, "Guido van Rossum", "Python Programming"],
    [4, "吴刚", "中文"],
 ) 

# Start from the first cell. Rows and 
# columns are zero indexed. 
row = 0
col = 0
  
# Iterate over the data and write it out row by row. 
for SN, Name , Contribution in (details): 
    worksheet.write(row, col, SN) 
    worksheet.write(row, col + 1, Name)
    worksheet.write(row, col + 2, Contribution)
    row += 1
  
workbook.close() 




Saturday, May 23, 2020

python - table generation in pdf using python

First need to install module reportlab. Then create table using code below:

Output pdf:



Code:

from reportlab.lib import colors   #import module
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

doc = SimpleDocTemplate("simple_table_grid.pdf", pagesize=letter)   #define pdf document name
# container for the 'Flowable' objects
elements = []   #define the data

data= [['00', '01', '02', '03', '04'],
       ['10', '11', '12', '13', '14'],
       ['20', '21', '22', '23', '24'],
       ['30', '31', '32', '33', '34']]
t=Table(data,5*[0.8*inch], 4*[0.8*inch])     #define the table format width and legth
t.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                       ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                       ('VALIGN',(0,0),(0,-1),'TOP'),
                       ('TEXTCOLOR',(0,0),(0,-1),colors.blue),
                       ('ALIGN',(0,-1),(-1,-1),'CENTER'),
                       ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                       ('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ]))

elements.append(t)        #then write the data
# write the document to disk

doc.build(elements)




Tuesday, May 5, 2020

Python tkinter - textbox


Here is the code for python gui textbox.


Python code:

from tkinter import *

root=Tk()

name=StringVar()

a=Entry(root,textvariable=name)
a.pack()

def enter():
    print(name.get())

    print("the text we get"+name.get())


b=Button(root,text="enter",command=enter)
b.pack()

root.mainloop()

Saturday, April 11, 2020

No module named PIL solved

No module named PIL solved.
At command propmpt: pip install pillow
 

Install free python IDE for window

Here is the step to install free python IDE for window.


No module named cv2 solved

No module named cv2 solved.

At command prompt type: pip install opencv-python.



Saturday, March 21, 2020

python-resize image

we can resize image using cv2 module.

Python code:
import cv2

img = cv2.imread('/home/pi/Desktop/download.jpeg') #read the image
width = int(1280 * 0.5) #width to resize
height = int(720 * 0.5) #height to resize
dim = (width, height)
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)  #using API of cv2 resize
cv2.imwrite('/home/pi/Desktop/download_resize.jpg',resized)  #then use cv2 to write out image

original image:


resized image:


Friday, January 3, 2020

Python tkinter - video preview in label

Video preview by using python tkinter.

Python code:

from tkinter import *
from PIL import ImageTk, Image
import cv2

root2=Tk()
root2.title('Video preview')


#Layout of display
topFrame =Frame(root2,width=100,height=100)
topFrame.grid(rowspan=2)



#topFrame
lmain = Label(topFrame,width=600,height=500)
lmain.grid()

cap=cv2.VideoCapture(0)


def video_stream():


    _, frame = cap.read()
  

   
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(1, video_stream)


video_stream()

   
root2.mainloop()