Example #1
0
def blend_image(img1, img2, alpha):
    try:
        with open_image(img1) as image:
            new_img1 = image.im.resize((200, 200))
        with open_image(img2) as image:
            new_img2 = image.im.resize((200, 200))
        blend_img = Image.blend(new_img1, new_img2, alpha)
        blend_img.show()
    except Exception as e:
        print e
Example #2
0
def composite_image(img1, img2):
    try:
        with open_image(img1) as image:
            new_img1 = Image.new('L', (200, 200))
        with open_image(img2) as image:
            new_img2 = image.im.resize((200, 200))
            m = new_img2.convert('L')
        comp_img = Image.composite(new_img1, new_img2, m)
        comp_img.show()
    except Exception as e:
        print e
def ehance_picture(img):
    try:
        with open_image(img) as image:
            new_img = ImageEnhance.Contrast(image.im)
            new_img.enhance(1.8).show("30% more contrast")
    except Exception as e:
        print e
def create_thumbnail(img, rate=None, resize=None):
    status = u'success'
    try:
        with open_image(img) as image:
            new_img = image.im
            new_size = new_img.size
            width, height = new_size
            if resize:
                if isinstance(resize, tuple):
                    new_size = resize
                else:
                    status = u'fail'
                    return status
            elif rate:
                if isinstance(rate, (int, float)):
                    _width = int(width * rate)
                    _height = int(height * rate)
                    new_size = _width, _height
                else:
                    status = u'fail'
                    return status
            else:
                status = u'fail'
                return status
            new_img.thumbnail(new_size)
            new_img.show()
        return status
    except Exception as e:
        print e
Example #5
0
def eval_image(img):
    try:
        with open_image(img) as image:
            new_img = image.im
            new_img = Image.eval(new_img, lambda x: x*2)
            new_img.show()
    except Exception as e:
        print e
def rotate_picture(img, angle):
    try:
        with open_image(img) as image:
            new_img = image.im
            new_img = new_img.rotate(angle)
            new_img.show()
    except Exception as e:
        print e
def point_operation(img):
    try:
        with open_image(img) as image:
            new_img = image.im
            # point(lambda x)
            new_img = new_img.point(lambda i: i * 2)
            new_img.show()
    except Exception as e:
        print e
def use_filter(img):
    try:
        with open_image(img) as image:
            new_img = image.im
            # 模糊
            new_img = new_img.filter(ImageFilter.BLUR)
            new_img.show()
    except Exception as e:
        print e
def transpose_picture(img):
    try:
        with open_image(img) as image:
            new_image = image.im
            new_image = new_image.transpose(Image.FLIP_LEFT_RIGHT)
            new_image = new_image.transpose(Image.ROTATE_90)
            new_image = new_image.transpose(Image.ROTATE_180)
            new_image.show()
    except Exception as e:
        print e
def check_picture_image(img):
    try:
        with open_image(img) as image:
            new_img = image.im
            return {
                u'mode': new_img.mode,
                u'size': new_img.size,
                u'format': new_img.format
            }
    except Exception as e:
        print e
def single_channel_operation(img):
    try:
        with open_image(img) as image:
            new_img_channels = image.im.split()
            r, g, b = range(3)
            red_channel = new_img_channels[r].point(lambda i: i < 100 and 255)
            green_channel = new_img_channels[g].point(lambda i: i * 0.7)
            green_channel.paste(green_channel, None, red_channel)
            new_img = Image.merge(image.im.mode, new_img_channels)
            new_img.show()
    except Exception as e:
        print e
def transition_picture_format(img, img_type):
    try:
        with open_image(img) as image:
            file_name = re.search(u'^\w+', img).group()
            file_dir = image.img_file_dir
            new_img = image.im
            new_img_dir = os.path.join(file_dir,
                                       u'%s.%s' % (file_name, img_type))
            new_img.save(new_img_dir)
            new_img.show()
    except Exception as e:
        print e
def convert_trans_pic(img):
    try:
        with open_image(img) as image:
            new_img = image.im
            if new_img.mode == 'RGB':
                r, g, b = new_img.split()
                a = get_main_image(new_img, 20)
                a_img = Image.merge('RGBA', (r, g, b, a))
                a_img.save('test.png')
            else:
                print 'not RGB mode'
    except Exception as e:
        print e
def resize_picture(img, size):
    status = u'success'
    try:
        with open_image(img) as image:
            new_img = image.im
            if isinstance(size, tuple):
                new_img = new_img.resize(size)
            else:
                status = u'fail'
            new_img.show()
            print status
    except Exception as e:
        print e
def operation_picture(img, r_size):
    try:
        with open_image(img) as image:
            new_img = image.im.convert('RGBA')
            m_width, m_height = [i // 2 for i in new_img.size]
            r_size //= 2
            box = (m_width - r_size, m_height - r_size, m_width + r_size,
                   m_height + r_size)
            region = new_img.crop(box)
            region = region.transpose(Image.ROTATE_180)
            new_img.paste(region, box)
            new_img.show()
    except Exception as e:
        print e
def operation_channel(img):
    try:
        with open_image(img) as image:
            new_img = image.im
            if new_img.mode == 'RGB':
                r, g, b = new_img.split()
                change_image = Image.merge("GRB", (g, b, r))
            elif new_img.mode == 'RGBA':
                r, g, b, a = new_img.split()
                change_image = Image.merge("GRBA", (g, b, r, a))
            else:
                change_image = new_img
            change_image.show()
    except Exception as e:
        print e