Excel整理数据功能虽然很强大,但在Python面前,曾经统治职场的它也的败下阵来。因为Python在搜集数据整理分析数据的过程中更加便捷,通过几行代码还可以实现自动化操作。如果你学会Python,便可以从网上找一些数据筛选、汇总的兼职工作来赚点小钱。
2、兼职查询资料
学会Python之后,很容易通过几行代码在网络上爬取各种各样的资源。目前有很多工作,都会招一些查阅汇总资料的兼职。学会Python的小白,便可以利用业余时间,通过Python帮助个人或者一些公司企业进行资料搜罗整理,还可以赚一笔小钱丰富自己的生活。
3、兼职P图
通过Python可以利用相关代码进行批量处理图片,不管是缩放、旋转、镜像、裁剪、灰度、添加文本等等,都可以在Python的帮助下获得,再也不用一张一张去P图了。如果学会了Python进行P图,平时可以和一些照相馆合作,帮助他们处理大量图片。
cv2.imshow("left", img_left)filename3=str(number)+'n3'+'.jpg' #打印第number张图片+增值方式+保存类型
cv2.imwrite(savedpath + filename3, img_left)
"""
# 数据增强实现
"""
import cv2
import numpy as np
import os
# 图像平移
def img_translation(image):
# 图像平移 下、上、右、左平移
M = np.float32([[1, 0, 0], [0, 1, 100]])
img_down = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, 0], [0, 1, -100]])
img_up = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, 100], [0, 1, 0]])
img_right = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, -100], [0, 1, 0]])
img_left = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
# 保存图片,需要保存上述的哪一图片,就在cv2.imwrite()中,将哪一图片名放入。
# filename='xxx' +'.jpeg'
# cv2.imwrite(savedpath + filename, img_left)
# 显示图形
cv2.imshow("down", img_down)
filename0=str(number)+'n0'+'.jpg'
cv2.imwrite(savedpath + filename0, img_down)
cv2.imshow("up", img_up)
filename1=str(number)+'n1'+'.jpg'
cv2.imwrite(savedpath + filename1, img_up)
cv2.imshow("right", img_right)
filename2=str(number)+'n2'+'.jpg'
cv2.imwrite(savedpath + filename2, img_right)
cv2.imshow("left", img_left)
filename3=str(number)+'n3'+'.jpg'
cv2.imwrite(savedpath + filename3, img_left)
# 图像缩放
def img_scale(image):
result = cv2.resize(image, (224, 224))
cv2.imshow("scale", result)
filename=str(number)+'n5'+'.jpg'
cv2.imwrite(savedpath + filename, result)
# 图像翻转
def img_flip(image):
# 0以X轴为对称轴翻转,>0以Y轴为对称轴翻转, <0X轴Y轴翻转
horizontally = cv2.flip(image, 0) # 水平镜像
vertically = cv2.flip(image, 1) # 垂直镜像
hv = cv2.flip(image, -1) # 水平垂直镜像
# 显示图形
cv2.imshow("Horizontally", horizontally)
filename1=str(number)+'n6'+'.jpg'
cv2.imwrite(savedpath + filename1, horizontally)
cv2.imshow("Vertically", vertically)
filename2=str(number)+'n7'+'.jpg'
cv2.imwrite(savedpath + filename2, vertically)
cv2.imshow("Horizontally &Vertically", hv)
filename3=str(number)+'n8'+'.jpg'
cv2.imwrite(savedpath + filename3, hv)
# 图像旋转
def img_rotation(image):
# 原图的高、宽 以及通道数
rows, cols, channel = image.shape
# 绕图像的中心旋转
# 参数:旋转中心 旋转度数 scale
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(image, M, (cols, rows))
# 显示图像
cv2.imshow("rotated", rotated)
filename1=str(number)+'n9'+'.jpg'
cv2.imwrite(savedpath + filename1, rotated)
#选装60度
W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated1 = cv2.warpAffine(image, W, (cols, rows))
cv2.imshow("rotated", rotated)
filename2=str(number)+'n12'+'.jpg'
cv2.imwrite(savedpath + filename2, rotated1)
#选装145度
W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated2 = cv2.warpAffine(image, W, (cols, rows))
cv2.imshow("rotated", rotated)
filename3=str(number)+'n13'+'.jpg'
cv2.imwrite(savedpath + filename3, rotated2)
# 图像加噪
def img_noise(image, mean=0, var=0.001):
'''
添加高斯噪声
mean : 均值
var : 方差
'''
image = np.array(image / 255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() <0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out * 255)
cv2.imshow("noise", out)
filename3=str(number)+'n10'+'.jpg'
cv2.imwrite(savedpath + filename3, out)
# 图像亮度调节
def img_brightness(image):
contrast = 1 # 对比度
brightness = 100 # 亮度
pic_turn = cv2.addWeighted(image, contrast, image, 0, brightness)
# cv2.addWeighted(对象,对比度,对象,对比度)
'''cv2.addWeighted()实现的是图像透明度的改变与图像的叠加'''
cv2.imshow('bright', pic_turn) # 显示图片
filename3=str(number)+'n11'+'.jpg'
cv2.imwrite(savedpath + filename3, pic_turn)
if __name__ == '__main__':
i = 0
path = '../Data/'
print(path)
savedpath = './result_new/'
filelist = os.listdir(path)
total_num = len(filelist)
for item in filelist:
number = i + 1
i = number
print("######")
print("打印到第",i,"张图片")
src = cv2.imread(path + item)
img_translation(src)
img_scale(src)
img_flip(src)
img_rotation(src)
img_noise(src)
img_brightness(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
代码较为繁琐,有空之后进行优化
输出结果
pip 是 python 必不可少的的包管理工具,但是要在国内用得爽,必须要配置镜像源。
有哪些镜像站可用,以及如何配置,网上都有很多分享了。
我常用的是 阿里云镜像站 。
这里有一点比较麻烦的地方,就是是 Linux 和 Windows 环境下的 pip 配置文件的名字和位置都不同,经常混淆。
今天就教大家一招,快速搞定:
执行完上面两条命令就可以啦。
pip config set 命令能自动把配置写入到用户对应的配置文件中:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
命令虽然方便,但是参数格式复杂,记住不太容易,要是想改一下也挺麻烦,
所以我们进一步了解一下细节。
“ 下面我以 Windows 系统为例,实际在 Linux 系统也是类似
查看配置
执行 pip config list 命令可以显示已经有了哪些配置:
pip config list
global.index-url='http://mirrors.aliyun.com/pypi/simple/'
install.trusted-host='mirrors.aliyun.com'
带上一个 -v 选项:
pip config list -v
For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\davy\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\davy\AppData\Roaming\pip\pip.ini'
For variant 'site', will try loading 'c:\users\davy\appdata\local\programs\python\python38\pip.ini'
global.index-url='http://mirrors.aliyun.com/pypi/simple/'
install.trusted-host='mirrors.aliyun.com'
“ 这里有一点不太好的地方是不显示配置是在哪个文件里。
就把它尝试获取的配置文件名完整路径列出来了。前面的 global 和 user 和 site 分别表示配置文件生效的范围:
global - 全局,一般不用
user - 当前用户,推荐
site - 只针对某一个 python 解释器
可以看到 user 有两个地方,其中配置任何一个都是可以的。
有的网络文章推荐的手动创建文件地址是前面那个,但是 pip 默认创建的是后者。
编辑配置
在命令行直接执行 pip config edit 会自动为我们打开配置文件,但是在 Windows 环境下还不行:
pip config edit
ERROR: Could not determine editor to use.
需要手动指定一个编辑器,就用记事本就行了:
pip config edit --editor notepad
“ Linux 系统中编辑器可以使用 vi,也可以是你习惯的其它编辑器
如果你从来没有设置过,它会报 找不到指定路径,这是因为相应的文件夹没有创建。
设置配置
通过 pip config set 命令可以直接设置配置项,它会自动创建没有的文件夹和文件。但是必须要给定一个配置项:
pip config set
ERROR: Got unexpected number of arguments, expected 2. (example: "pip config set [name] [value]")
我们随便写一个配置:
pip config set x.y z
Writing to C:\Users\davy\AppData\Roaming\pip\pip.ini
然后再执行上面的
pip config edit --editor notepad
就能自动打开配置文件,把拷贝好的配置文件内容贴进去就可以啦。