针对来自LCJ尘埃大佬的python电子相册的相关内容修正
针对来自LCJ尘埃大佬的python电子相册的相关内容修正
源博客链接在这:https://blog.csdn.net/LCJ106/article/details/84037145#commentsedit
我发现如果直接使用大佬的代码的话会出现
NameError: name ‘self’ is not defined
或者
‘return’ outside function的报错
因为我是初学者,所以有些不到位甚至错误的地方麻烦大家多多指教
我通过通读大佬的代码,发现从第14行开始后面的代码几乎都往前进了4个空格导致各个方法与类的关系发生混乱
因此我通过修改得到的可运行的代码如下:
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')
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()
注意最后两行代码我建议加上if语句方便其他文件引用:
if __name__ == '__main__':
app = wxGUI()
app.MainLoop()