Python电子相册

功能:
(1)实现图形的显示
(2)完成图形的放大、缩小、剪切、旋转等功能
(3)图片的自动播放功能
(4)图片播放的同时,带有背景音乐
注:照片格式需为jpg

源代码

import wx
from PIL import Image
import matplotlib.pyplot as plt
import os
import tkinter
import re
import pygame

class wxGUI(wx.App):
    def OnInit(self):
        self.frame=wx.Frame(parent=None,title='x',size=(600,600))
        self.panel=wx.Panel(self.frame,-1)
        
        wx.StaticText(parent=self.panel,label='相册路径名',pos=(120,60))#静态文本控件
        self.inputN1=wx.TextCtrl(parent=self.panel,pos=(240,60))#文本框
        
        #添加按钮
        self.buttonCheck1=wx.Button(parent=self.panel,label='确定',pos=(400,60))
        self.Bind(wx.EVT_BUTTON,self.OnButtonCheck1,self.buttonCheck1)

        self.buttonCheck6=wx.Button(parent=self.panel,label='播放相册',pos=(140,400))#添加按钮
        self.Bind(wx.EVT_BUTTON,self.OnButtonCheck6,self.buttonCheck6)

        self.buttonCheck7=wx.Button(parent=self.panel,label='停止播放',pos=(300,400))#添加按钮
        self.Bind(wx.EVT_BUTTON,self.OnButtonCheck7,self.buttonCheck7)

        wx.StaticText(parent=self.panel,label='裁剪',pos=(120,140))#静态文本控件
        self.inputN2=wx.TextCtrl(parent=self.panel,pos=(240,140))#文本框
        self.buttonCheck2=wx.Button(parent=self.panel,label='确定',pos=(400,140))#添加按钮
        self.Bind(wx.EVT_BUTTON,self.OnButtonCheck2,self.buttonCheck2)

        wx.StaticText(parent=self.panel,label='缩放',pos=(120,190))#静态文本控件
        self.buttonCheck3=wx.Button(parent=self.panel,label='确定',pos=(400,190))#添加按钮
        self.inputN3=wx.TextCtrl(parent=self.panel,pos=(240,190))#文本框
        self.Bind(wx.EVT_BUTTON,self.OnButtonCheck3,self.buttonCheck3)

        wx.StaticText(parent=self.panel,label='旋转',pos=(120,240))#静态文本控件
        self.inputN4=wx.TextCtrl(parent=self.panel,pos=(240,240))#文本框
        self.buttonCheck4=wx.Button(parent=self.panel,label='确定',pos=(400,240))#添加按钮
        self.Bind(wx.EVT_BUTTON,self.OnButtonCheck4,self.buttonCheck4)



        #组合框
        self.comboBox1=wx.ComboBox(self.panel,value='click here',choices=[],pos=(100,100),size=(200,130))
        self.Bind(wx.EVT_COMBOBOX,self.OnCombo1,self.comboBox1)
        
        self.frame.Show()
        return True

    #按钮设置实时更新组合框
    def OnButtonCheck1(self,event):
        path=self.inputN1.GetValue()
        images=[f for f in os.listdir(path) if f.endswith('.jpg')]
        self.comboBox1.Set(images)

    #裁剪
    def OnButtonCheck2(self,event):
        path=self.inputN1.GetValue()
        name=self.comboBox1.GetValue()
        f=path+'\\'+name
        img=Image.open(f)
        x=self.inputN2.GetValue()
        pattern=re.compile(r',')
        y=pattern.split(x)
        print(y)
        left=int(y[0])
        upper=int(y[1])
        right=int(y[2])
        lower=int(y[3])
        box=(left,upper,right,lower)
        img1=img.crop(box)#该tuple中信息为(left, upper, right, lower)。系统的原点(0,0)为图片的左上角。坐标中的数字单位为像素点。
        plt.figure(2)
        plt.title('img1')
        plt.imshow(img1),plt.axis('off')
        plt.show()

    #缩放
    def OnButtonCheck3(self,event):
        path=self.inputN1.GetValue()
        name=self.comboBox1.GetValue() 
        f=path+'\\'+name
        img=Image.open(f)         
        m=self.inputN3.GetValue()
        pattern=re.compile(r',')
        n=pattern.split(m)
        width=int(n[0])
        height=int(n[1])
        img2=img.resize((width,height),Image.ANTIALIAS)
        plt.title('img2')
        plt.imshow(img2)
        plt.axis('off')
        plt.show()

    def OnButtonCheck4(self,event):
        plt.figure(4)
        path=self.inputN1.GetValue()
        name=self.comboBox1.GetValue()
        f=path+'\\'+name
        img=Image.open(f)         
        angle=int(self.inputN4.GetValue())
        img3=img.rotate(angle)
        plt.title('img3')
        plt.imshow(img3),plt.axis('off')
        plt.show()



    #相册播放   
    def OnButtonCheck6(self,event):
        global i
        i=1
        path=self.inputN1.GetValue()
        images=[f for f in os.listdir(path) if f.endswith('.jpg')]
        pygame.mixer.init()
        plt.figure(5)
        file=r'F:\QQ.music\刘瑞琦 - 歌路.mp3'
        #file=file.encode('utf-8')
        file = file.encode()
        pygame.mixer.music.load(file)
        pygame.mixer.music.play(-1)
        plt.ion()
        while 1:            
            for f in images:
                f=path+'\\'+f
                im=Image.open(f)
                plt.imshow(im)
                plt.axis('off')
                plt.pause(3)
                if(i==0):
                    break
            if(i==0):
                break
        plt.close()
        plt.ioff()
        
    #停止播放           
    def OnButtonCheck7(self,event):
        global i
        i=0
        pygame.mixer.music.stop()


    #组合框选定显示对应图片
    def OnCombo1(self,event):
        path=self.inputN1.GetValue()
        name=self.comboBox1.GetValue()
        plt.figure(1)
        f=path+'\\'+name
        im=Image.open(f)
        print(im.size)
        plt.imshow(im)
        plt.axis('off')
        plt.show()
app=wxGUI()
app.MainLoop()

  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
好的,我可以为您提供一个简单的Python电子相册程序示例,具体步骤如下: 1.首先,您需要安装Python中的Tkinter模块,这是Python中的标准GUI库。如果您还没有安装它,可以使用以下命令进行安装: ```python pip install tkinter ``` 2.创建一个新的Python文件,并导入Tkinter模块和os模块,用于访问文件系统中的图像文件。 ```python import tkinter as tk from PIL import Image, ImageTk import os ``` 3.创建一个名为“PhotoAlbum”的类,并在其中包含初始化方法和显示图像的方法。 ```python class PhotoAlbum: def __init__(self, master): self.master = master self.master.title("电子相册") self.images = [] self.current_image = 0 self.load_images() self.create_widgets() def load_images(self): for filename in os.listdir("images"): if filename.endswith(".jpg") or filename.endswith(".png"): image = Image.open(os.path.join("images", filename)) self.images.append(ImageTk.PhotoImage(image)) def create_widgets(self): self.image_label = tk.Label(self.master, image=self.images[self.current_image]) self.image_label.pack() self.prev_button = tk.Button(self.master, text="上一张", command=self.prev_image) self.prev_button.pack(side="left") self.next_button = tk.Button(self.master, text="下一张", command=self.next_image) self.next_button.pack(side="right") def prev_image(self): self.current_image -= 1 if self.current_image < 0: self.current_image = len(self.images) - 1 self.image_label.config(image=self.images[self.current_image]) def next_image(self): self.current_image += 1 if self.current_image >= len(self.images): self.current_image = 0 self.image_label.config(image=self.images[self.current_image]) ``` 4.最后,实例化PhotoAlbum类并运行主循环。 ```python if __name__ == "__main__": root = tk.Tk() app = PhotoAlbum(root) root.mainloop() ``` 5.将您的图像文件保存在“ images”文件夹中,并将程序文件保存在与该文件夹相同的目录中。您现在可以运行程序并浏览您的电子相册了! 请注意,这只是一个简单的示例,您可以根据自己的需要进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值