Third commit
This commit is contained in:
73
Picture_Reading.py
Normal file
73
Picture_Reading.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022-7-24 0024 16:00
|
||||
# @Author : Qing
|
||||
# @Email : derighoid@gmail.com
|
||||
# @File : Picture_Reading.py
|
||||
# @Software: PyCharm
|
||||
|
||||
# 导入库
|
||||
import os.path
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
# 建立图片读取类
|
||||
class pictureRead(object):
|
||||
def __init__(self, filePath):
|
||||
self.filePath = filePath
|
||||
|
||||
# 图片读取
|
||||
def fileRead(self):
|
||||
if self.filePath == " ":
|
||||
print("Please full in the file path")
|
||||
else:
|
||||
face = Image.open(self.filePath)
|
||||
return face
|
||||
|
||||
# 图片转矩阵
|
||||
def imageMatrix(self):
|
||||
im = np.array(self.fileRead())
|
||||
return im
|
||||
|
||||
# 图片转灰色
|
||||
def imageGrey(self):
|
||||
im_gray = np.array(self.fileRead().convert("L"))
|
||||
return im_gray
|
||||
|
||||
# 打印图片类型
|
||||
def get_imageType(self):
|
||||
print(type(self.imageGrey()))
|
||||
|
||||
# 打印图片矩阵元素类型
|
||||
def get_imageDtype(self):
|
||||
print(self.imageGrey().dtype)
|
||||
|
||||
# 打印图片大小
|
||||
def get_imageSize(self):
|
||||
print(self.imageMatrix().shape)
|
||||
|
||||
# 图片plt展示
|
||||
def plt_image(self):
|
||||
# 原图片显示
|
||||
plt.imshow(self.imageMatrix())
|
||||
plt.show()
|
||||
# 灰色图片显示
|
||||
plt.imshow(self.imageGrey())
|
||||
plt.show()
|
||||
|
||||
# 图片保存
|
||||
def image_save(self):
|
||||
path = 'E:\\桌面\\Python_Picture_Analysis\\data\\numpy_red.png'
|
||||
root="E:\\桌面\\Python_Picture_Analysis\\data\\"
|
||||
if not os.path.exists(root):
|
||||
os.mkdir(root) # 判断是否存在文件夹
|
||||
else:
|
||||
if os.path.exists(path): # 判断是否已经存在该文件
|
||||
print("File already exists")
|
||||
else:
|
||||
Image.fromarray(np.uint8(self.imageGrey())).save(path)
|
||||
print("Save successfully")
|
||||
|
||||
|
Reference in New Issue
Block a user