1

냥냥이 그림 클래스 속 sum함수와 그냥 sum함수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from skimage import novice,data
class MyPic():
    def __init__(self,filename):
        self.pic = novice.open(data.data_dir + "/" + filename)
    def threshold(self,p):
        if p > 255:
            return 255
        else:
            return int(p)
    def show(self):
        self.pic.show()
    def sum(self):
        (r,g,b,a) = (0,0,0,0)
        for p in self.pic:
            r += p.red
            g += p.green
            b += p.blue
            a += p.alpha
        return (r,g,b,a)
    def grey(self):
        for p in self.pic:
            (r,g,b) = (p.red, p.green, p.blue)
            grey = self.threshold((r + g + b)/3)
            (p.red,p.green,p.blue)=(grey,grey,grey)

skimage.novice skimage.data라는 모듈을 계속 부를때마다 이렇게 쓰는게 아니라 import 시키면 그냥 novice라고 사용할 수 있음

(a)

1
2
pic = MyPic("chelsea.png")
pic.show()

냥냥펀치!

(b)

1
2
pic = MyPic("chelsea.png")
print(pic.sum())

(19980169, 15078438, 11743750, 34501500)


(c)

1
2
pic = MyPic("chelsea.png")
print(sum(pic.sum()))

81303857


(d)

1
2
3
pic = MyPic("chelsea.png")
pic.grey()
pic.show()


(e)

1
2
3
pic = MyPic("chelsea.png")
pic.grey()
print(pic.sum())

(15554511, 15554511, 15554511, 34501500)


(f)

1
2
3
pic = MyPic("chelsea.png")
pic.grey()
print(sum(pic.sum()))

81165033


2

(a)

냥냥이를 반파랑 반빨강으로 만들 수 있도록 class MyPic을 수정해보자

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from skimage import novice,data
class MyPic():
    def __init__(self,filename):
        self.pic = novice.open(data.data_dir + "/" + filename)
    def threshold(self,p):
        if p > 255:
            return 255
        else:
            return int(p)
    def show(self):
        self.pic.show()
    def bluered(self):
        for y in range(self.pic.height):
            for x in range(self.pic.width//2):
                self.pic[x, y].blue=255
            for x in range(self.pic.width//2):
                self.pic[x + self.pic.width//2, y].red=255
1
2
3
pic = MyPic("chelsea.png")
pic.bluered()
pic.show()



(b)

세피아 효과를 줘보쟈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from skimage import novice,data
class MyPic():
    def __init__(self,filename):
        self.pic = novice.open(data.data_dir + "/" + filename)
    def threshold(self,p):
        if p > 255:
            return 255
        else:
            return int(p)
    def show(self):
        self.pic.show()
    def sepia(self):
        for p in self.pic:
            (r,g,b) = (p.red,p.green,p.blue)
            p.red = self.threshold(r * .393 + g * .769 + b * .189)
            p.green = self.threshold(r * .349 + g * .686 + b * .168)
            p.blue = self.threshold(r * .272 + g * .534 + b * .131)
1
2
3
pic = MyPic("chelsea.png")
pic.sepia()
pic.show()