2019년 2월 23일 토요일

[Python] Image Library Wrappers


pyimg.py

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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
######################################################################
# Library
######################################################################

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)

######################################################################
# Main
######################################################################
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")

######################################################################
# Reference
######################################################################
#https://pillow.readthedocs.io/en/3.1.x/reference/Image.html
#[*] PIL.Image.open(fp, mode='r')
#PIL.Image.alpha_composite(im1, im2) - Alpha composite im2 over im1.
#[*] PIL.Image.blend(im1, im2, alpha) - Creates a new image by interpolating between two input images, using a constant alpha.:
#PIL.Image.composite(image1, image2, mask) - Create composite image by blending images using a transparency mask
#PIL.Image.eval(image, *args) - Applies the function (which should take one argument) to each pixel in the given image
#PIL.Image.merge(mode, bands) - Merge a set of single band images into a new multiband image.
#                         
#[*] PIL.Image.new(mode, size, color=0) - Creates a new image with the given mode and size (w,h). 
#PIL.Image.fromarray(obj, mode=None) - Creates an image memory from an object exporting the array interface
#PIL.Image.frombytes(mode, size, data, decoder_name='raw', *args) - Creates a copy of an image memory from pixel data in a buffer.
#PIL.Image.fromstring(*args, **kw)
#PIL.Image.frombuffer(mode, size, data, decoder_name='raw', *args)
#
#Image.convert(mode=None, matrix=None, dither=None, palette=0, colors=256) - Returns a converted copy of this image
#[*] Image.copy() - Copies this image. Use this method if you wish to paste things into an image, but still retain the original.
#[*] Image.crop(box=None) Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
#Image.draft(mode, size) - Configures the image file loader so it returns a version of the image that as closely as possible matches the given mode and size.
#[*] Image.filter(filter) - Filters this image using the given filter. For a list of available filters, see the ImageFilter module.
#Image.getbands() - Returns a tuple containing the name of each band in this image. For example, getbands on an RGB image returns ('R','G','B').
#Image.getbbox() - Calculates the bounding box of the non-zero regions in the image.
#Image.getcolors(maxcolors=256) - Returns a list of colors used in this image.
#Image.getdata(band=None) - Returns the contents of this image as a sequence object containing pixel values.
#Image.getextrema() Gets the the minimum and maximum pixel values for each band in the image.
#Image.getpalette() Returns the image palette as a list.
#Image.getpixel(xy) Returns the pixel value at a given position.
#Image.histogram(mask=None, extrema=None) Returns a histogram for the image.
#Image.offset(xoffset, yoffset=None)
#[*] Image.paste(im, box=None, mask=None) Pastes another image into this image. box (x,y) or (x1,y1,x2,y2)
#Image.point(lut, mode=None) Maps this image through a lookup table or function.
#Image.putalpha(alpha) Adds or replaces the alpha layer in this image.
#Image.putdata(data, scale=1.0, offset=0.0) Copies pixel data to this image. 
#Image.putpalette(data, rawmode='RGB') Attaches a palette to this image. 
#[*] Image.putpixel(xy, value) Modifies the pixel at the given position.
#Image.quantize(colors=256, method=None, kmeans=0, palette=None) Convert the image to 'P' mode with the specified number of colors.
#[*] Image.resize(size, resample=0) Returns a resized copy of this image. (w,h)
#       resample - PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC, PIL.Image.LANCZOS
#[*] Image.rotate(angle, resample=0, expand=0) Returns a rotated copy of this image
#[*] Image.save(fp, format=None, **params) Saves this image under the given filename
#Image.seek(frame) Seeks to the given frame in this sequence file.
#[*] Image.show(title=None, command=None)
#Image.split() Split this image into individual bands
#Image.tell() Returns the current frame number. See seek().
#Image.thumbnail(size, resample=3) Make this image into a thumbnail. 
#Image.tobitmap(name='image') Returns the image converted to an X11 bitmap
#Image.tobytes(encoder_name='raw', *args) Return image as a bytes object. ("raw")
#Image.tostring(*args, **kw)
#[*] Image.transform(size, method, data=None, resample=0, fill=1) Transforms this image
#      method - PIL.Image.EXTENT PIL.Image.AFFINE PERSPECTIVE QUAD MESH 
#[*] Image.transpose(method) Transpose image (flip or rotate in 90 degree steps)
#      method - FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180, ROTATE_270 TRANSPOSE.
#[*] Image.verify() Verifies the contents of a file.
#Image.fromstring(*args, **kw)
#Image.load() Allocates storage for the image and loads the pixel data.
#[*] Image.close()
#PIL.Image.format()
#PIL.Image.mode()
#[*] PIL.Image.size()
#[*] PIL.Image.width()
#[*] PIL.Image.height()
#PIL.Image.palette()
#PIL.Image.info()

#class PIL.ImageDraw.Draw(im, mode=None)
#PIL.ImageDraw.Draw.arc(xy, start, end, fill=None)
#PIL.ImageDraw.Draw.bitmap(xy, bitmap, fill=None)
#PIL.ImageDraw.Draw.chord(xy, start, end, fill=None, outline=None) Same as arc(), but connects the end points with a straight line.
#PIL.ImageDraw.Draw.ellipse(xy, fill=None, outline=None)
#PIL.ImageDraw.Draw.line(xy, fill=None, width=0)
#PIL.ImageDraw.Draw.pieslice(xy, start, end, fill=None, outline=None)
#PIL.ImageDraw.Draw.point(xy, fill=None)
#PIL.ImageDraw.Draw.polygon(xy, fill=None, outline=None)
#PIL.ImageDraw.Draw.rectangle(xy, fill=None, outline=None)
#PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)
#PIL.ImageDraw.Draw.multiline_text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left")
#PIL.ImageDraw.Draw.textsize(text, font=None) Return the size of the given string, in pixels.
#PIL.ImageDraw.Draw.multiline_textsize(text, font=None, spacing=0)
#

'''
Modes

  - 1 (1-bit pixels, black and white, stored with one pixel per byte)
  - L (8-bit pixels, black and white)
  - P (8-bit pixels, mapped to any other mode using a color palette)
  - RGB (3x8-bit pixels, true color)
  - RGBA (4x8-bit pixels, true color with transparency mask)
  - CMYK (4x8-bit pixels, color separation)
  - YCbCr (3x8-bit pixels, color video format)
  - LAB (3x8-bit pixels, the L*a*b color space)
  - HSV (3x8-bit pixels, Hue, Saturation, Value color space)
  - I (32-bit signed integer pixels)
  - F (32-bit floating point pixels)
'''

'''
Filters

For geometry operations that may map multiple input pixels to a single output pixel, the Python Imaging Library provides four different resampling filters.

NEAREST Pick the nearest pixel from the input image. Ignore all other input pixels.
BILINEAR For resize calculate the output pixel value using linear interpolation 
  on all pixels that may contribute to the output value. For other transformations 
  linear interpolation over a 2x2 environment in the input image is used.
BICUBIC For resize calculate the output pixel value using cubic interpolation on 
  all pixels that may contribute to the output value. For other transformations 
  cubic interpolation over a 4x4 environment in the input image is used.
LANCZOS Calculate the output pixel value using a high-quality Lanczos filter 
  (a truncated sinc) on all pixels that may contribute to the output value. 
  In the current version of PIL, this filter can only be used with the resize 
  and thumbnail methods.
'''

댓글 없음:

댓글 쓰기