Python tkinter openimg and saveimg

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ichimarusoichi
PHP Super Member
PHP Super Member
โพสต์: 352
ลงทะเบียนเมื่อ: 07/01/2019 12:19 pm

Python tkinter openimg and saveimg

โพสต์ที่ยังไม่ได้อ่าน โดย ichimarusoichi »

การเขียนโค้ด Python tkinter ด้วย Python เพื่อให้สามารถเซฟภาพได้นั้นต้องทำการเปิดไฟล์ภาพก่อนจากนั้นก็สามารถเซฟภาพโดยภาพที่เซฟนั้นจะเซฟได้เพียง JPG และคำสั่งการทำงานนั้นก็ไม่ได้ยากมากลองปรับเปลี่ยนไฟล์ที่ใช้เซฟได้เลยครับ
Input

โค้ด: เลือกทั้งหมด

from tkinter import Tk,Menu,Label
from PIL import Image,ImageTk
from tkinter import filedialog
from tkinter import messagebox


class MainApp:
    #constructor
    def __init__(self,master):

        self.master = master
        self.master.title("Menu")
        self.lbl = Label(self.master)

    def createMenu(self):
        #สร้างเแทบมนู
        menubar = Menu(self.master)
        self.master.config(menu=menubar)
        #เมนูไฟล์
        fileMenu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Open...",command=self.openFile)
        fileMenu.add_command(label="Save as...",command=self.saveFile)
        fileMenu.add_separator()
        fileMenu.add_command(label="Exit", command=self.master.destroy)
        #เมนูแก้ไข
        editMenu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Edit", menu=editMenu)
        editMenu.add_command(label="Restore Image", command=self.imgRestore)
        #เมนูสำหรับไว้หมุนภาพ
        processMenu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Process", menu=processMenu)
        processMenu.add_command(label="Rotate", command=self.imgRotate)

    def openFile(self):
        #ชนิดไฟล์ภาพที่สามารถเปิดได้
        ftypes = [('Image files', '*.jpg *.png *.gif'),
        ('All files', '*')]
        filename = filedialog.askopenfilename(parent=self.master,title='Choose a file',filetypes=ftypes)
        if filename:
           try:
                #open image
                self.im = Image.open(filename)
                self.imOrigin = self.im.copy()
                self.showImage()
           except: pass

    def saveFile(self):
        ftypes = [('JPEG', '*.jpg')]
        filename = filedialog.asksaveasfilename(parent=self.master,title='Save image as',filetypes=ftypes)
        if len(filename)>0:
            try:

                filename = filename+".jpg"
                self.im.save(filename,"JPEG")
            except IOError:
                messagebox.showerror("Error","Cannot save image!")

    def showImage(self):
        self.imTk = ImageTk.PhotoImage(self.im)
        self.lbl.destroy()
        self.lbl = Label(self.master, image=self.imTk)
        self.lbl.pack()jp

    def imgRestore(self):
        self.im = self.imOrigin.copy()
        self.showImage()

    def imgRotate(self):
        self.im = self.im.rotate(45)
        self.showImage()

gui = Tk()
app = MainApp(gui)
app.createMenu()
gui.mainloop()
Output
saveimg.PNG
ในส่วนของภาพ output คือการเซฟภาพจากไฟล์ JPEG_example.jpg เป็น aaa.jpg แต่ภาพไม่ขึ้นเพราะก่อนหน้าผมได้ทำการเซฟภาพจากไฟล์ PNG เป็น jpg และมีเงื่อนไขอีกอย่างคือถ้าเราทำการเซฟภาพด้วยชื่อที่เหมือนกันแต่เปลี่ยนจากพิมพ์เล็กเป็นพิมพ์ใหญ่นั้นการเซฟก็จะไม่มีผลทำให้ไม่ได้ชื่อไฟล์อีกภาพ

คำสั่งของการทำงาน คือ ถ้าเราเปิดไฟล์ภาพที่เป็นสุกลอื่นแล้วทำการ save ภาพนั้นจะขึ้นภาพไฟล์ขาวๆ ยกตัวอย่างผมเปิดไฟล์ภาพ PNG เซฟเป็น jpg จะมี messagebox แจ้งเตือนขึ้นมาว่า ("Error","Cannot save image!") จากนั้นภาพที่ออกมาจะเป็นไฟล์ขาวๆไม่สามารถเปิดได้

ช่องทางการศึกษาเพิ่มเติมข่าวที่น่าสนใจเกี่ยวกับ : Python tkinter สร้างปุ่มเมนูเพื่อเปิดไฟล์ภาพ
-บทเรียน Python
-บทเรียน Python Framework Flask
-ความหมายของคำ คืออะไร
-VDO Tutorial - Python
-ถาม-ตอบ Java & Python
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Majestic-12 [Bot] และบุคลทั่วไป 52