目标任务:
- 读取txt,excel,csv文件并显示
- 读取一幅彩色图形并显示,同时进行灰度化,并对该图像进行图像增强
配置:python3.6、pip20.1
用到的文本资源等我发在我的资源里,零积分即可下载,如有需要请自取。
效果图:
import csv
import xlrd
#txt
with open(r"C:/Users/Administrator/Desktop/expir1_txt.txt", "r") as f:
data = f.read()
print(data)
for i in range(2):
print("\n")
#excel
excel_path = "C:/Users/Administrator/Desktop/expir1_xls.xls"
excel = xlrd.open_workbook(excel_path, encoding_override="utf-8")
all_sheet = excel.sheets()
sheet_name = []
sheet_row = []
sheet_col = []
for sheet in all_sheet:
sheet_name.append(sheet.name)
print("该Excel共有{0}个sheet,当前sheet名称为{1},该sheet共有{2}行,{3}列".format(len(all_sheet), sheet.name, sheet.nrows, sheet.ncols))
for each_row in range(sheet.nrows):
print("当前为%s行:" % each_row, type(each_row))
print(sheet.row_values(each_row), type(sheet.row_values(each_row)))
if sheet.nrows:#如果表非空,打印第一行
first_row_value = sheet.row_values(0) # 打印指定的某一行
print("第一行的数据是:%s" % first_row_value)
for i in range(2):
print("\n")
#csv
with open('C:/Users/Administrator/Desktop/expir1_csv.csv') as f:
reader = csv.reader(f)
for row in reader:
print(row)
for i in range(2):
print("\n")
#图片灰度化
from skimage import io,transform, data
import matplotlib.pyplot as plt
img=io.imread('C:/Users/Administrator/Desktop/expir1_jpg.jpg')
io.imshow(img)
plt.show()
img_gray=io.imread('C:/Users/Administrator/Desktop/expir1_jpg.jpg',as_gray=True)
io.imshow(img_gray)
plt.show()
# 调亮
from skimage import data, exposure, img_as_float
import matplotlib.pyplot as plt
image = img_as_float(data.moon())
gam2= exposure.adjust_gamma(image, 0.5)
plt.figure('adjust_gamma',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(image,plt.cm.gray)
plt.axis('on')
plt.subplot(133)
plt.title('gamma=0.5')
plt.imshow(gam2,plt.cm.gray)
plt.axis('on')
plt.show()