一:操作表格
1 .
2.
二:实例操作。
- 三种打开文件操作。
>>> f1 = open('F:/test.txt','w')
>>> f1
<_io.TextIOWrapper name='F:/test.txt' mode='w' encoding='cp936'>
>>> f1.close ()
>>>
>>> f1 = open('F://test.txt','w')
>>> f1.close ()
>>>
>>> f1 = open('F:\\test.txt','w')
>>> f1.close ()
- 一些示例操作。
>>>f = open('F:\\img.txt')
>>>
>>> f.read()
"import tkinter as tk\nimport tkinter.filedialog \nimport cv2\nimport numpy as np\n##from matplotlib import pyplot as plt\n\n##制作窗口\nwindow = tk.Tk()\nwindow.title('My Window')\nwindow.geometry('640x480')\nlabel = tk.Label(window,text = '图像处理', bg = 'blue',font = ('楷体',12),width = 30)\nlabel.pack()\n\n## 解决中文路径函数问题\ndef cv_imread(file_path):\n cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)\n return cv_img\n\n## 按键功能函数\ndef selectpath():\n file_path = tkinter.filedialog.askopenfilename()\n global img\n img = cv_imread(file_path)\n cv2.namedWindow('image',0)\n ## cv2.resizeWindow('image',700,600)\n cv2.imshow('image',img)\n k = cv2.waitKey(0)\n cv2.destroyAllWindows()\n \ndef rotate():\n imginfo = img.shape\n height = imginfo[0]\n width = imginfo[1]\n matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5) #mat rotate 1 center 2 angle 3 sc\n dst = cv2.warpAffine(img,matRotate,(height,width))\n cv2.namedWindow('dst',0)\n cv2.imshow('dst',dst)\n cv2.waitKey(0)\n cv2.destroyAllWindows()\n \n## 制作按键\nbutton1 = tk.Button(window,text = '打开图片', bg='yellow', font =('楷体',12),width = 10,height = 1,command = selectpath)\nbutton2 = tk.Button(window,text = '图片旋转', bg='yellow', font =('楷体',12),width = 10,height = 1,command = rotate)\nbutton1.place(x=80,y=100)\nbutton2.place(x=80,y=150)\n#button1.pack()\nwindow.mainloop()\n"
>>> f.read() # 读完后,指针指向末尾
''
>>> f.seek(1,0) # 重置指针
1
>>> f.read()
"mport tkinter as tk\nimport tkinter.filedialog \nimport cv2\nimport numpy as np\n##from matplotlib import pyplot as plt\n\n##制作窗口\nwindow = tk.Tk()\nwindow.title('My Window')\nwindow.geometry('640x480')\nlabel = tk.Label(window,text = '图像处理', bg = 'blue',font = ('楷体',12),width = 30)\nlabel.pack()\n\n## 解决中文路径函数问题\ndef cv_imread(file_path):\n cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)\n return cv_img\n\n## 按键功能函数\ndef selectpath():\n file_path = tkinter.filedialog.askopenfilename()\n global img\n img = cv_imread(file_path)\n cv2.namedWindow('image',0)\n ## cv2.resizeWindow('image',700,600)\n cv2.imshow('image',img)\n k = cv2.waitKey(0)\n cv2.destroyAllWindows()\n \ndef rotate():\n imginfo = img.shape\n height = imginfo[0]\n width = imginfo[1]\n matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5) #mat rotate 1 center 2 angle 3 sc\n dst = cv2.warpAffine(img,matRotate,(height,width))\n cv2.namedWindow('dst',0)\n cv2.imshow('dst',dst)\n cv2.waitKey(0)\n cv2.destroyAllWindows()\n \n## 制作按键\nbutton1 = tk.Button(window,text = '打开图片', bg='yellow', font =('楷体',12),width = 10,height = 1,command = selectpath)\nbutton2 = tk.Button(window,text = '图片旋转', bg='yellow', font =('楷体',12),width = 10,height = 1,command = rotate)\nbutton1.place(x=80,y=100)\nbutton2.place(x=80,y=150)\n#button1.pack()\nwindow.mainloop()\n"
>>> f.tell() #指针位置
1466
>>>list(f) #变成列表
['import tkinter as tk\n', 'import tkinter.filedialog \n', 'import cv2\n', 'import numpy as np\n', '##from matplotlib import pyplot as plt\n', '\n', '##制作窗口\n', 'window = tk.Tk()\n', "window.title('My Window')\n", "window.geometry('640x480')\n", "label = tk.Label(window,text = '图像处理', bg = 'blue',font = ('楷体',12),width = 30)\n", 'label.pack()\n', '\n', '## 解决中文路径函数问题\n', 'def cv_imread(file_path):\n', ' cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)\n', ' return cv_img\n', '\n', '## 按键功能函数\n', 'def selectpath():\n', ' file_path = tkinter.filedialog.askopenfilename()\n', ' global img\n', ' img = cv_imread(file_path)\n', " cv2.namedWindow('image',0)\n", " ## cv2.resizeWindow('image',700,600)\n", " cv2.imshow('image',img)\n", ' k = cv2.waitKey(0)\n', ' cv2.destroyAllWindows()\n', ' \n', 'def rotate():\n', ' imginfo = img.shape\n', ' height = imginfo[0]\n', ' width = imginfo[1]\n', ' matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5) #mat rotate 1 center 2 angle 3 sc\n', ' dst = cv2.warpAffine(img,matRotate,(height,width))\n', " cv2.namedWindow('dst',0)\n", " cv2.imshow('dst',dst)\n", ' cv2.waitKey(0)\n', ' cv2.destroyAllWindows()\n', ' \n', '## 制作按键\n', "button1 = tk.Button(window,text = '打开图片', bg='yellow', font =('楷体',12),width = 10,height = 1,command = selectpath)\n", "button2 = tk.Button(window,text = '图片旋转', bg='yellow', font =('楷体',12),width = 10,height = 1,command = rotate)\n", 'button1.place(x=80,y=100)\n', 'button2.place(x=80,y=150)\n', '#button1.pack()\n', 'window.mainloop()\n']
>>> f.seek(0)
0
>>> for each in f:
print(each) #文件对象迭代出来
import tkinter as tk
import tkinter.filedialog
import cv2
import numpy as np
。。。。。。。。。。。。。。。。。。。。后面省略。。。。。。。。。。。。。