1、用Python玩转图片处理
class ImageUtils:
""" 图片处理工具 """
def __init__(self, source_dir, target_dir):
self.source_dir = source_dir
self.target_dir = target_dir
def thumbnail(self, filename, percent=0.5):
'缩略图'
im = Image.open(os.path.join(self.source_dir, filename))
w, h = im.size
print('Original image size: %sx%s' % (w, h))
im.thumbnail((int(w*percent), int(h*percent)))
print('Thumbnail image to: %sx%s' % (int(w*percent), int(h*percent)))
output = os.path.join(self.target_dir, filename.split('.')[0]+'-thumbnail.jpg')
im.save(output, 'jpeg')
def resize(self, filename, horizontal_ratio=0.5, vertical_ratio=0.5):
'调整大小'
im = Image.open(os.path.join(self.source_dir, filename))
w, h = im.size
print('Original image size: %sx%s' % (w, h))
im_size = im.resize((int(w*horizontal_ratio), int(h*vertical_ratio)))
print('Resize image to: %sx%s' % (int(w*horizontal_ratio), int(h*vertical_ratio)))
output = os.path.join(self.target_dir, filename.split('.')[0]+'-resize.jpg')
im_size.save(output, 'jpeg')
def enhance(self, filename, enhance_ratio=1.3):
'图片对比度