'Tkinter'에 해당되는 글 2건

  1. 2024.01.06 Pillow PDF Converter PDF 변환기 2
  2. 2018.12.29 tkinter GUI
반응형

Python Pillow 라이브러리를 이용해 PDF 변환기를 만들어 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
from tkinter import filedialog
from PIL import Image
 
filename = filedialog.askopenfilename(
    filetypes = (("이미지 파일""*.jpg *.png"), ("모든 파일""*.*")),
    initialdir = os.getcwd())
# tkinter.filedialog.askopenfilename(**options)
# tkinter.filedialog.askopenfilenames(**options)
# The above two functions create an Open dialog and return the selected
# filename(s) that correspond to existing file(s).
 
newname = filename[0:-4]
ext = ".pdf"
 
try:
    with Image.open(filename) as pf:
        pf.save(newname + ext)
        print(filename + "  ===>  " + newname + ext)
except:
    print("Error")
 

 

파이썬 코드를 작성하고 실행한다.

 

파일 선택 다이얼로그에서 원하는 이미지 파일을 선택한다.

 

변환에 문제가 없다면 결과 화면이 출력된다.

 

PDF 파일이 생성된다.

 

※ 참고

Tkinter Dialogs

 

반응형
Posted by J-sean
:

tkinter GUI

OpenCV 2018. 12. 29. 10:06 |
반응형

OpenCV와 함께 tkinter를 이용해 아래처럼 GUI를 원하는대로 만들 수 있다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2
import sys
import tkinter as tk
from PIL import Image, ImageTk
# The ImageTk module contains support to create and modify Tkinter BitmapImage and PhotoImage objects from PIL images
 
main = tk.Tk()
main.title("OpenCV-tkinter")
 
cvFrame = tk.Frame(main)
# A frame is basically just a container for other widgets.
cvFrame.grid(row = 0, column = 0, padx = 10, pady = 10)
 
lbl1 = tk.Label(cvFrame)
lbl1.grid(row = 0, column = 0)
 
lbl2 = tk.Label(cvFrame)
lbl2.grid(row = 0, column = 1)
 
def ExitButton():
    sys.exit()
 
btn = tk.Button(cvFrame, text = "Exit", font = ('Arial''30''bold'), foreground = "Red", command = ExitButton) # (.., height = 2, width = 60, ..)
# fron - As a tuple whose first element is the font family, followed by a size (in points if positive, in pixels if negative), optionally followed by
# a string containing one or more of the style modifiers bold, italic, underline, and overstrike.
btn.grid(row = 1, column = 0, columnspan = 2, sticky = tk.N + tk.S + tk.W + tk.E)
# columnspan - Normally a widget occupies only one cell in the grid. However, you can grab multiple cells of a row and merge them into one
# larger cell by setting the columnspan option to the number of cells. For example, w.grid(row=0, column=2, columnspan=3) would place widget
# w in a cell that spans columns 2, 3, and 4 of row 0.
# sticky - This option determines how to distribute any extra space within the cell that is not taken up by the widget at its natural size.
 
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print('frame_size =', frame_size)
 
def show_frame():
    retval, frame = cap.read()
    frame = cv2.flip(frame, 1)
 
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    # Creates an image memory from an object exporting the array interface (using the buffer protocol).
    imgtk = ImageTk.PhotoImage(image = img)
    # A Tkinter-compatible photo image. This can be used everywhere Tkinter expects an image object.
    # If the image is an RGBA image, pixels having alpha 0 are treated as transparent.
    lbl1.imgtk = imgtk
    lbl1.configure(image = imgtk)
    # Set the values of one or more options.
 
    lbl2.imgtk = imgtk
    lbl2.configure(image = imgtk)
 
    # lbl.after(10, show_frame)
    main.after(10, show_frame)
    # Requests Tkinter to call function callback with arguments args after a delay of at least delay_ms milliseconds.
 
show_frame()
main.mainloop()
cs


반응형
Posted by J-sean
: