2019년 2월 22일 금요일

[Python] Image Processing Example


1. Image Processing Library

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import PIL
from PIL import Image

class pyimg():  
    
    def __init__(self):
        pass

    def new(self):
        self.image = PIL.Image.new()
    
    def open(self, filename):
        self.image = PIL.Image.open(filename) 

    def close(self):
        self.image.close()
    
    def copy(self):
        new_image = pyimg()
        new_image.image = self.image.copy() #copy.deepcopy(self.image)
        return new_image
    
    def save(self,filename):
        self.image.save(filename)
        
    def show(self):
        self.image.show()

    def quantize(self,colors):
        #Image.quantize(colors=256, method=None, kmeans=0, palette=None)
        #method: 0 = median cut 1 = maximum coverage 2 = fast octree
        self.image = self.image.quantize(colors) 

    def convertToGray(self):
        #Image.convert(mode=None, matrix=None, dither=None, palette=0, colors=256)
        #mode: 1, L, P, RGB, RGBA, CMYK, YCbCr, LAB, HSV, I, F
        self.image = self.image.convert('L')

    def convertToBW(self):
        self.image = self.image.convert('1')
    
    def crop(self,box):
        self.image = self.image.crop(box) #left,upper,right,lower

    def cropWH(self,box): #left,upper,width,height
        box[2] = box[0] + box[2]
        box[3] = box[1] + box[3] 
        self.image = self.image.crop(box) #left,upper,right,lower

    def cropBorder(self,box): #left,top,right,bottom
        box[2] = self.image.width - box[2]
        box[3] = self.image.height - box[3]
        self.image = self.image.crop(box) #left,upper,right,lower

    def resize(self,size,resample=0):
        #Image.resize(size, resample=0)
        #resample: PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC, PIL.Image.LANCZOS
        self.image = self.image.resize(size, resample=resample)

    def resizeWidth(self,width,resample=0):
        radio = width / self.image.width
        height = int(radio * self.image.height)
        self.image = self.image.resize((width,height), resample=resample)

    def resizeHeight(self,height,resample=0):
        radio = height / self.image.height
        width = int(radio * self.image.width)
        self.image = self.image.resize((width,height), resample=resample)

    def resizeBox(self,size,resample=0):
        width = size[0]
        radio = width / self.image.width
        height = int(radio * self.image.height)
        if height > size[1]: 
            height = size[1]
            radio = height / self.image.height
            width = int(radio * self.image.width)
        self.image = self.image.resize((width,height), resample=resample)
        
    def rotate(self,degree,expand=0):
        #Image.rotate(angle, resample=0, expand=0)
        self.image = self.image.rotate(degree,expand)

    def flipLR(self):
        self.image = self.image.transpose(PIL.Image.FLIP_LEFT_RIGHT)

    def flipTB(self):
        self.image = self.image.transpose(PIL.Image.FLIP_TOP_BOTTOM)

    def flip90(self):
        self.image = self.image.transpose(PIL.Image.ROTATE_90)

    def flip180(self):
        self.image = self.image.transpose(PIL.Image.ROTATE_180)

    def flip270(self):
        self.image = self.image.transpose(PIL.Image.ROTATE_270)



2. Image Processing Library Test Example

1
2
3
4
5
6
7
if __name__ == "__main__":
    img = pyimg()
    img.open('D:/line.png')
    img2 = img.copy()
    img2.resizeBox([800,200])
    img2.show()
    img2.save("D:/Temp_c.png")


댓글 없음:

댓글 쓰기