61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
# @Time : 2022-7-26 0026 9:58
|
||
|
# @Author : Qing
|
||
|
# @Email : derighoid@gmail.com
|
||
|
# @File : Subtractive_Algorithm.py
|
||
|
# @Software: PyCharm
|
||
|
import os.path
|
||
|
|
||
|
import numpy as np
|
||
|
import matplotlib.pyplot as plt
|
||
|
from PIL import Image
|
||
|
from Picture_Reading import *
|
||
|
|
||
|
class ReduceColor(pictureRead):
|
||
|
def __init__(self,filePath):
|
||
|
self.filePath=filePath
|
||
|
|
||
|
def readImg(self):
|
||
|
face4=Image.open(self.filePath)
|
||
|
return face4
|
||
|
|
||
|
def imageMatrix(self):
|
||
|
im4 = np.array(self.readImg())
|
||
|
return im4
|
||
|
|
||
|
# 减色算法
|
||
|
def decrease_color(self):
|
||
|
out=self.imageMatrix().copy()
|
||
|
out=out//64*64+32
|
||
|
Image.fromarray((np.uint8(out))).save("E:\\桌面\\Python_Picture_Analysis\\data\\picture8.jpg")
|
||
|
return out
|
||
|
|
||
|
# 展示图片
|
||
|
def plt_Img(self):
|
||
|
plt.imshow(self.decrease_color())
|
||
|
plt.show()
|
||
|
|
||
|
|
||
|
#图像二值化(以128为阈值进行二值化)
|
||
|
def imgBinarization(self):
|
||
|
a=self.imageGrey()-np.array([[128]])
|
||
|
b=np.floor(a/np.array([[256]]))
|
||
|
c=b+np.array([[1]],dtype=np.int16)
|
||
|
bfilter=c.astype("uint8")
|
||
|
result=bfilter*np.array([[255]],dtype=np.uint8)
|
||
|
# result=np.array(self.readImg().convert("1"))
|
||
|
plt.imshow(result)
|
||
|
plt.show()
|
||
|
return result
|
||
|
|
||
|
#保存图片
|
||
|
def img_Save(self):
|
||
|
root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
|
||
|
if not os.path.exists(root):
|
||
|
os.mkdir(root)
|
||
|
else:
|
||
|
Image.fromarray(np.uint8(self.decrease_color())).save(root + 'picture8'+'.jpg')
|
||
|
Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9'+'.jpg')
|
||
|
print("Picture Save successfully")
|
||
|
|