Skip to content

Commit

Permalink
更新了第15天的文档
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed May 4, 2019
1 parent d4b6a8a commit cfc872e
Show file tree
Hide file tree
Showing 35 changed files with 128 additions and 48 deletions.
Binary file not shown.
Binary file removed Day01-15/Day15/code/res/Python课程大纲.pdf
Binary file not shown.
Binary file added Day01-15/Day15/code/res/luohao.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-crop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-paste.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-putpixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-rotate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-show.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Day15/res/image-transpose.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 100 additions & 22 deletions Day01-15/Day15/图像和办公文档处理.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,122 @@
## 图像和办公文档处理

用程序来处理图像以及办公文档在实际开发中非常常见,我们可以通过Python生态圈中的第三方模块来完成这些操作
用程序来处理图像和办公文档经常出现在实际开发中,Python的标准库中虽然没有直接支持这些操作的模块,但我们可以通过Python生态圈中的第三方模块来完成这些操作

### 操作图像

#### 计算机图像相关知识

1. 颜色。如果你有使用颜料画画的经历,那么一定知道混合红、黄、蓝三种颜料可以得到其他的颜色,事实上这三种颜色就是被我们称为美术三原色的东西,它们是不能再分解的基本颜色。在计算机中,我们可以将红、绿、蓝三种色光以不同的比例叠加来组合成其他的颜色,因此这三种颜色就是色光三原色,所以我们通常会将一个颜色表示为一个RGB值或RGBA值(其中的A表示Alpha通道,它决定了透过这个图像的像素,也就是透明度)。

| 名称 | RGBA值 | 名称 | RGBA值 |
| :---: | :------------------: | :----: | :----------------: |
| White | (255, 255, 255, 255) | Red | (255, 0, 0, 255) |
| Green | (0, 255, 0, 255) | Blue | (0, 0, 255, 255) |
| Gray | (128, 128, 128, 255) | Yellow | (255, 255, 0, 255) |
| Black | (0, 0, 0, 255) | Purple | (128, 0, 128, 255) |

2. 像素。对于一个由数字序列表示的图像来说,最小的单位就是图像上单一颜色的小方格,这些小方块都有一个明确的位置和被分配的色彩数值,而这些一小方格的颜色和位置决定了该图像最终呈现出来的样子,它们是不可分割的单位,我们通常称之为像素(pixel)。每一个图像都包含了一定量的像素,这些像素决定图像在屏幕上所呈现的大小。

#### 用Pillow操作图像

- 剪裁图片
- 调整图片大小
- 旋转和翻转
- 操作像素
- 添加水印
Pillow是由从著名的Python图像处理库PIL发展出来的一个分支,通过Pillow可以实现图像压缩和图像处理等各种操作。可以使用下面的命令来安装Pillow。

```Shell
pip install pillow
```

Pillow中最为重要的是Image类,读取和处理图像都要通过这个类来完成。

```Python
>>> from PIL import Image
>>>
>>> image = Image.open('./res/guido.jpg')
>>> image.format, image.size, image.mode
('JPEG', (500, 750), 'RGB')
>>> image.show()
```

![](./res/image-show.png)

1. 剪裁图像

```Python
>>> image = Image.open('./res/guido.jpg')
>>> rect = 80, 20, 310, 360
>>> image.crop(rect).show()
```

![](./res/image-crop.png)

2. 生成缩略图

```Python
>>> image = Image.open('./res/guido.jpg')
>>> size = 128, 128
>>> image.thumbnail(size)
>>> image.show()
```

![](./res/image-thumbnail.png)

3. 缩放和黏贴图像

```Python
>>> image1 = Image.open('./res/luohao.png')
>>> image2 = Image.open('./res/guido.jpg')
>>> rect = 80, 20, 310, 360
>>> guido_head = image2.crop(rect)
>>> width, height = guido_head.size
>>> image1.paste(guido_head.resize((int(width / 1.5), int(height / 1.5))), (172, 40))
```

![](./res/image-paste.png)

4. 旋转和翻转

```Python
>>> image = Image.open('./res/guido.png')
>>> image.rotata(180).show()
>>> image.transpose(Image.FLIP_LEFT_RIGHT).show()
```

![](./res/image-rotate.png)

![](./res/image-transpose.png)

5. 操作像素

```Python
>>> image = Image.open('./res/guido.jpg')
>>> for x in range(80, 310):
... for y in range(20, 360):
... image.putpixel((x, y), (128, 128, 128))
...
>>> image.show()
```

![](./res/image-putpixel.png)

6. 滤镜效果

```Python
>>> from PIL import Image, ImageFilter
>>>
>>> image = Image.open('./res/guido.jpg')
>>> image.filter(ImageFilter.CONTOUR).show()
```

![](./res/image-filter.png)

### 处理Excel电子表格

Python的OpenPyXL模块让我们可以在Python程序中读取和修改Excel电子表格,LibreOffice Calc和OpenOffice Calc也都能处理Excel的电子表格文件,这就意味着OpenPyXL模块也能处理来自这些应用程序的电子表格。关于OpenPyXL的使用手册和使用文档可以查看它的[官方文档](https://openpyxl.readthedocs.io/en/stable/#)
Python的openpyxl模块让我们可以在Python程序中读取和修改Excel电子表格,当然实际工作中,我们可能会用LibreOffice Calc和OpenOffice Calc来处理Excel的电子表格文件,这就意味着openpyxl模块也能处理来自这些软件生成的电子表格。关于openpyxl的使用手册和使用文档可以查看它的[官方文档](https://openpyxl.readthedocs.io/en/stable/#)

### 处理Word文档

| 属性 | 描述 |
| ------------- | ---------- |
| bold | 粗体 |
| italic | 斜体 |
| underline | 下划线 |
| strike | 删除线 |
| double_strike | 双删除线 |
| all_caps | 大写首字母 |
| small_caps | 大写首字母 |
| shadow | 带阴影 |
| outline | 轮廓显示 |
| rtl | 从右向左 |
| imprint | 凹嵌页面 |
| emboss | 凸出页面 |
利用python-docx模块,Pytho 可以创建和修改Word文档,当然这里的Word文档不仅仅是指通过微软的Office软件创建的扩展名为docx的文档,LibreOffice Writer和OpenOffice Writer都是免费的字处理软件。


### 处理PDF文档

PDF是Portable Document Format的缩写,使用.pdf作为文件扩展名。
PDF是Portable Document Format的缩写,使用.pdf作为文件扩展名。接下来我们就研究一下如何通过Python实现从PDF读取文本内容和从已有的文档生成新的PDF文件。
16 changes: 8 additions & 8 deletions Day41-55/01.快速上手.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django实战(01) - 快速上手
## 快速上手

Web开发的早期阶段,开发者需要手动编写每个页面,例如一个新闻门户网站,每天都要修改它的HTML页面,随着网站规模和体量的增大,这种方式就变得极度糟糕。为了解决这个问题,开发人员想到了用外部程序来为Web服务器生成动态内容,也就是说HTML页面以及页面中的动态内容不再通过手动编写而是通过程序自动生成。最早的时候,这项技术被称为CGI(公共网关接口),当然随着时间的推移,CGI暴露出的问题也越来越多,例如大量重复的样板代码,总体性能较为低下等,因此在时代呼唤新英雄的背景下,PHP、ASP、JSP这类Web应用开发技术在上世纪90年代中后期如雨后春笋般涌现。通常我们说的Web应用是指通过浏览器来访问网络资源的应用程序,因为浏览器的普及性以及易用性,Web应用使用起来方便简单,免除了安装和更新应用程序带来的麻烦,而且也不用关心用户到底用的是什么操作系统,甚至不用区分是PC端还是移动端。

Expand Down Expand Up @@ -292,15 +292,15 @@ Django诞生于2003年,它是一个在真正的应用中成长起来的项目
output.write('\t<hr>\n')
output.write('\t<table>\n')
output.write('\t\t<tr>\n')
output.write('\t\t\t<th>部门编号</th>\n')
output.write('\t\t\t<th>部门名称</th>\n')
output.write('\t\t\t<th>所在地</th>\n')
output.write('\t\t\t<th width=120>部门编号</th>\n')
output.write('\t\t\t<th width=180>部门名称</th>\n')
output.write('\t\t\t<th width=180>所在地</th>\n')
output.write('\t\t</tr>\n')
for dept in depts_list:
output.write('\t\t<tr>\n')
output.write(f'\t\t\t<td>{dept["no"]}</td>\n')
output.write(f'\t\t\t<td>{dept["name"]}</td>\n')
output.write(f'\t\t\t<td>{dept["location"]}</td>\n')
output.write(f'\t\t\t<td align=center>{dept["no"]}</td>\n')
output.write(f'\t\t\t<td align=center>{dept["name"]}</td>\n')
output.write(f'\t\t\t<td align=center>{dept["location"]}</td>\n')
output.write('\t\t</tr>\n')
output.write('\t</table>\n')
output.write('</body>\n')
Expand All @@ -310,7 +310,7 @@ Django诞生于2003年,它是一个在真正的应用中成长起来的项目

7. 刷新页面查看程序的运行结果。

![](./res/runserver01.png)
![](./res/show-depts.png)

#### 使用视图模板

Expand Down
2 changes: 1 addition & 1 deletion Day41-55/02.深入模型.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django实战(02) - 深入模型
## 深入模型

在上一个章节中,我们提到了Django是基于MVC架构的Web框架,MVC架构追求的是“模型”和“视图”的解耦合。所谓“模型”说得更直白一些就是数据,所以通常也被称作“数据模型”。在实际的项目中,数据模型通常通过数据库实现持久化操作,而关系型数据库在很长一段时间都是持久化的首选方案,下面我们以MySQL为例来说明如何使用关系型数据库来实现持久化操作。

Expand Down
6 changes: 4 additions & 2 deletions Day41-55/03.静态资源和Ajax请求.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django实战(03) - 静态资源和Ajax请求
## 静态资源和Ajax请求

基于前面两个章节讲解的知识,我们已经可以使用Django框架来实现Web应用的开发了。接下来我们就尝试实现一个投票应用,具体的需求是用户进入应用首先查看到“学科介绍”页面,该页面显示了一个学校所开设的所有学科;通过点击某个学科,可以进入“老师介绍”页面,该页面展示了该学科所有老师的详细情况,可以在该页面上给老师点击“好评”或“差评”,但是会先跳转到“登录页”要求用户登录,登录成功才能投票;对于未注册的用户,可以在“登录页”点击“新用户注册”进入“注册页”完成用户注册,注册成功后会跳转到“登录页”,注册失败会获得相应的提示信息。

Expand Down Expand Up @@ -229,10 +229,12 @@ urlpatterns = [
]
```

启动服务器运行项目,效果如下图所示
启动服务器运行项目,进入首页查看学科信息

![](/Users/Hao/Desktop/Python-100-Days/Day41-55/res/show-subjects.png)

点击学科查看老师信息。

![](./res/show-teachers.png)

### Ajax请求
Expand Down
2 changes: 1 addition & 1 deletion Day41-55/04.表单的应用.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django实战(04) - 表单的应用
## 表单的应用

我们继续来完成上一章节中的项目,实现“用户注册”和“用户登录”的功能,并限制只有登录的用户才能为老师投票。Django框架中提供了对表单的封装,而且提供了多种不同的使用方式。

Expand Down
3 changes: 3 additions & 0 deletions Day41-55/05.Cookie和Session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Cookie和Session


3 changes: 0 additions & 3 deletions Day41-55/05.Cookie和会话.md

This file was deleted.

2 changes: 1 addition & 1 deletion Day41-55/06.中间件的应用.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战06:中间件的应用
## 中间件的应用



2 changes: 1 addition & 1 deletion Day41-55/07.日志和调试.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战-07:日志和调试
## 日志和调试



2 changes: 1 addition & 1 deletion Day41-55/08.文件上传和富文本编辑.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战08:文件上传和富文本编辑
## 文件上传和富文本编辑



2 changes: 1 addition & 1 deletion Day41-55/09.文件下载和报表.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战08:文件下载和报表
## 文件下载和报表



2 changes: 1 addition & 1 deletion Day41-55/10.RESTful架构和DRF入门.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战10:RESTful架构和DRF入门
## RESTful架构和DRF入门



2 changes: 1 addition & 1 deletion Day41-55/11.RESTful架构和DRF进阶.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战11:RESTful架构和DRF进阶
## RESTful架构和DRF进阶



2 changes: 1 addition & 1 deletion Day41-55/12.使用缓存.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战12:使用缓存
## 使用缓存



2 changes: 1 addition & 1 deletion Day41-55/13.短信和邮件.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战13:短信和邮件
## 短信和邮件



2 changes: 1 addition & 1 deletion Day41-55/14.异步任务和定时任务.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Django 2实战14:异步任务和定时任务
## 异步任务和定时任务



2 changes: 1 addition & 1 deletion Day41-55/15.单元测试和项目上线.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Django 2实战15:单元测试和项目上线
## 单元测试和项目上线


Binary file removed Day41-55/res/runserver01.png
Binary file not shown.
Binary file removed Day41-55/res/runserver02.png
Binary file not shown.
Binary file removed Day41-55/res/runserver03.png
Binary file not shown.
Binary file added Day41-55/res/show-depts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Day41-55/res/show-subjects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Day41-55/res/show-teachers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

#### Day44 - [表单的应用](./Day41-55/04.表单的应用.md)

#### Day45 - [Cookie和会话](./Day41-55/05.Cookie和会话.md)
#### Day45 - [Cookie和Session](./Day41-55/05.Cookie和Session.md)

#### Day46 - [中间件的应用](./Day41-55/06.中间件的应用.md)

Expand Down

0 comments on commit cfc872e

Please sign in to comment.