Last active
August 8, 2018 06:40
-
-
Save ti-nspire/e54bcbd8b8c0e021d21c58cd9019376d to your computer and use it in GitHub Desktop.
PIL.Imageの使いかた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import matplotlib.pyplot as plt | |
import numpy as np | |
class IMAGE: | |
def __init__(self, imgPath): | |
self.srcImg = Image.open(imgPath) | |
self.__initialize() | |
def __initialize(self): | |
self.srcArr = np.asarray(self.srcImg) | |
self.size = self.srcArr.shape | |
self.width = self.size[1] | |
self.height = self.size[0] | |
self.numOfPixels = self.width * self.height | |
self.AllRGB = self.srcArr.reshape(self.numOfPixels, 3) | |
self.AllBrightness = np.mean(self.AllRGB, axis=1, dtype="uint16") # rgb の平均値を輝度とすることにする。 | |
#self.AllBrightness = self.AllRGB[:,0]*0.299 + self.AllRGB[:,1]*0.587 + self.AllRGB[:,2]*0.114 | |
self.AllBrightness = self.AllBrightness.astype(np.uint8) | |
# 画像を表示するための函数 | |
def showImg(self): | |
plt.imshow(self.srcImg) | |
#plt.imshow(self.srcArr) | |
plt.show() | |
return self | |
# ヒストグラムを表示するための函数 | |
def showHist(self): | |
plt.figure() | |
plt.xlabel("brightness") | |
plt.ylabel("freq") | |
plt.hist(self.AllBrightness, range=(-0.5, 255.5), bins=256) | |
plt.show() | |
return self | |
# 輝度を変えるための函数 | |
def changeBrightness(self, shift): | |
rgb = self.AllRGB.astype(np.int16) + shift | |
rgb = np.clip(rgb, 0, 255) | |
rgb = rgb.reshape(self.height, self.width, 3) | |
self.srcImg = Image.fromarray(rgb.astype(np.uint8)) | |
self.__initialize() | |
return self | |
# 画像を2値化するための函数 | |
def conv2binary(self, threshold=100): | |
self.AllRGB.flags.writeable = True | |
mask = self.AllBrightness > threshold | |
self.AllRGB[mask] = [255, 255, 255] | |
self.AllRGB[~mask] = [0, 0, 0] | |
self.AllRGB = self.AllRGB.reshape(self.height, self.width, 3) | |
self.srcImg = Image.fromarray(self.AllRGB.astype(np.uint8)) | |
self.__initialize() | |
return self | |
# 画像をグレースケールにするための函数 | |
def conv2gray(self): | |
brt = np.repeat(self.AllBrightness, 3) | |
brt = brt.reshape(self.height, self.width, 3) | |
self.srcImg = Image.fromarray(brt.astype(np.uint8)) | |
self.__initialize() | |
return self | |
# 画像をファイルに書き出すための函数 | |
def export(self, filepath): | |
self.srcImg.save(filepath) | |
return self | |
############ | |
# 使いかた # | |
############ | |
if __name__ == "__main__": | |
pathFrom = "H:\\img\\riho.png" # このファイルを読み込む。 | |
pathTo = "H:\\img\\sute.png" # このファイルに書き出す。 | |
# 1. 画像ファイルを読み込んで、Imageオブジェクトを表示して、輝度のヒストグラムを表示する。 | |
img = IMAGE(pathFrom).showImg().showHist() | |
# 2. 輝度を高くして、Imageオブジェクトを表示して、輝度のヒストグラムを表示する。 | |
img.changeBrightness(100).showImg().showHist() | |
# 3. 画像ファイルを読み込んで、輝度を低くして、Imageオブジェクトを表示して、輝度のヒストグラムを表示する。 | |
IMAGE(pathFrom).changeBrightness(-100).showImg().showHist() | |
# 4. 画像を読み込んで、グレースケールにして、Imageオブジェクトを表示して、輝度のストグラムを表示する。 | |
IMAGE(pathFrom).conv2gray().showImg().showHist() | |
# 5. 画像を読み込んで、閾値を境に輝度を2値化して、Imageオブジェクトを表示して、輝度のヒストグラムを表示する。 | |
img = IMAGE(pathFrom).conv2binary(77).showImg().showHist() | |
# 6. Imageオブジェクトを画像ファイルとして書き出す。 | |
img.export(pathTo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment