diff --git "a/Day01-15/Day15/code/res/Docker\345\205\245\351\227\250\346\225\231\347\250\213.pdf" "b/Day01-15/Day15/code/res/Docker\345\205\245\351\227\250\346\225\231\347\250\213.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..10e98241b09f1603b9557e74a2e6da7b404fa62a Binary files /dev/null and "b/Day01-15/Day15/code/res/Docker\345\205\245\351\227\250\346\225\231\347\250\213.pdf" differ diff --git "a/Day01-15/Day15/code/res/Python\350\257\276\347\250\213\345\244\247\347\272\262.pdf" "b/Day01-15/Day15/code/res/Python\350\257\276\347\250\213\345\244\247\347\272\262.pdf" deleted file mode 100644 index 90e2a92b6e6f2fcff033793f3551906ac171e7fe..0000000000000000000000000000000000000000 Binary files "a/Day01-15/Day15/code/res/Python\350\257\276\347\250\213\345\244\247\347\272\262.pdf" and /dev/null differ diff --git a/Day01-15/Day15/code/res/luohao.png b/Day01-15/Day15/code/res/luohao.png new file mode 100644 index 0000000000000000000000000000000000000000..31593b5ae4b56e5664ced9c11a5ff1cbb0ba0991 Binary files /dev/null and b/Day01-15/Day15/code/res/luohao.png differ diff --git a/Day01-15/Day15/res/image-crop.png b/Day01-15/Day15/res/image-crop.png new file mode 100644 index 0000000000000000000000000000000000000000..bb4a9bf573c35c3c26e6aec04820da76a865b6e0 Binary files /dev/null and b/Day01-15/Day15/res/image-crop.png differ diff --git a/Day01-15/Day15/res/image-filter.png b/Day01-15/Day15/res/image-filter.png new file mode 100644 index 0000000000000000000000000000000000000000..aac571ca36878655b587868e9999d3d547d76e4f Binary files /dev/null and b/Day01-15/Day15/res/image-filter.png differ diff --git a/Day01-15/Day15/res/image-paste.png b/Day01-15/Day15/res/image-paste.png new file mode 100644 index 0000000000000000000000000000000000000000..6c5c39d97a8f693ca7796fe6e6cabe6fc1bf7380 Binary files /dev/null and b/Day01-15/Day15/res/image-paste.png differ diff --git a/Day01-15/Day15/res/image-putpixel.png b/Day01-15/Day15/res/image-putpixel.png new file mode 100644 index 0000000000000000000000000000000000000000..54712b7e03b89f95ca4a694e02ccee0b34037c2c Binary files /dev/null and b/Day01-15/Day15/res/image-putpixel.png differ diff --git a/Day01-15/Day15/res/image-rotate.png b/Day01-15/Day15/res/image-rotate.png new file mode 100644 index 0000000000000000000000000000000000000000..5db7459b8b539780441ef31f6d0bf9a07875c926 Binary files /dev/null and b/Day01-15/Day15/res/image-rotate.png differ diff --git a/Day01-15/Day15/res/image-show.png b/Day01-15/Day15/res/image-show.png new file mode 100644 index 0000000000000000000000000000000000000000..396cb5a7a224830993ed68a8f813e573f6dd9f16 Binary files /dev/null and b/Day01-15/Day15/res/image-show.png differ diff --git a/Day01-15/Day15/res/image-thumbnail.png b/Day01-15/Day15/res/image-thumbnail.png new file mode 100644 index 0000000000000000000000000000000000000000..91c623f205598eab771823f7dfb60cd5286863ca Binary files /dev/null and b/Day01-15/Day15/res/image-thumbnail.png differ diff --git a/Day01-15/Day15/res/image-transpose.png b/Day01-15/Day15/res/image-transpose.png new file mode 100644 index 0000000000000000000000000000000000000000..685a280d5bd8152a68c740960588a8dcf54e1116 Binary files /dev/null and b/Day01-15/Day15/res/image-transpose.png differ diff --git "a/Day01-15/Day15/\345\233\276\345\203\217\345\222\214\345\212\236\345\205\254\346\226\207\346\241\243\345\244\204\347\220\206.md" "b/Day01-15/Day15/\345\233\276\345\203\217\345\222\214\345\212\236\345\205\254\346\226\207\346\241\243\345\244\204\347\220\206.md" index 5ae9892652df357e4bc4d783824150506c87bac8..d17be261caa368ee5a618cc0c7d68fae54f5d9eb 100644 --- "a/Day01-15/Day15/\345\233\276\345\203\217\345\222\214\345\212\236\345\205\254\346\226\207\346\241\243\345\244\204\347\220\206.md" +++ "b/Day01-15/Day15/\345\233\276\345\203\217\345\222\214\345\212\236\345\205\254\346\226\207\346\241\243\345\244\204\347\220\206.md" @@ -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作为文件扩展名。 \ No newline at end of file +PDF是Portable Document Format的缩写,使用.pdf作为文件扩展名。接下来我们就研究一下如何通过Python实现从PDF读取文本内容和从已有的文档生成新的PDF文件。 \ No newline at end of file diff --git "a/Day41-55/01.\345\277\253\351\200\237\344\270\212\346\211\213.md" "b/Day41-55/01.\345\277\253\351\200\237\344\270\212\346\211\213.md" index 8890beb03d3ea287b7ec0d11b6eb1b4f0e82e164..aff16dc87381494023455912cd4e632a3fb6e25d 100644 --- "a/Day41-55/01.\345\277\253\351\200\237\344\270\212\346\211\213.md" +++ "b/Day41-55/01.\345\277\253\351\200\237\344\270\212\346\211\213.md" @@ -1,4 +1,4 @@ -## Django实战(01) - 快速上手 +## 快速上手 Web开发的早期阶段,开发者需要手动编写每个页面,例如一个新闻门户网站,每天都要修改它的HTML页面,随着网站规模和体量的增大,这种方式就变得极度糟糕。为了解决这个问题,开发人员想到了用外部程序来为Web服务器生成动态内容,也就是说HTML页面以及页面中的动态内容不再通过手动编写而是通过程序自动生成。最早的时候,这项技术被称为CGI(公共网关接口),当然随着时间的推移,CGI暴露出的问题也越来越多,例如大量重复的样板代码,总体性能较为低下等,因此在时代呼唤新英雄的背景下,PHP、ASP、JSP这类Web应用开发技术在上世纪90年代中后期如雨后春笋般涌现。通常我们说的Web应用是指通过浏览器来访问网络资源的应用程序,因为浏览器的普及性以及易用性,Web应用使用起来方便简单,免除了安装和更新应用程序带来的麻烦,而且也不用关心用户到底用的是什么操作系统,甚至不用区分是PC端还是移动端。 @@ -292,15 +292,15 @@ Django诞生于2003年,它是一个在真正的应用中成长起来的项目 output.write('\t
部门编号 | \n') - output.write('\t\t\t部门名称 | \n') - output.write('\t\t\t所在地 | \n') + output.write('\t\t\t部门编号 | \n') + output.write('\t\t\t部门名称 | \n') + output.write('\t\t\t所在地 | \n') output.write('\t\t
---|---|---|---|---|---|
{dept["no"]} | \n') - output.write(f'\t\t\t{dept["name"]} | \n') - output.write(f'\t\t\t{dept["location"]} | \n') + output.write(f'\t\t\t{dept["no"]} | \n') + output.write(f'\t\t\t{dept["name"]} | \n') + output.write(f'\t\t\t{dept["location"]} | \n') output.write('\t\t