Exemplo n.º 1
0
def img_style_transfer(filename, filename2):

    img = get_img(filename)
    filename_dir = '/Users/tanya/PycharmProjects/image6/static/images/'
    content_filename = filename_dir + filename
    content_image = load_image(content_filename, max_size=None)

    style_filename = filename_dir + filename2
    style_image = load_image(style_filename, max_size=300)

    content_layer_ids = [4]
    style_layer_ids = list(range(13))
    res = style_transfer(content_image=content_image,
                         style_image=style_image,
                         content_layer_ids=content_layer_ids,
                         style_layer_ids=style_layer_ids,
                         weight_content=5,
                         weight_style=10.0,
                         weight_denoise=0.3,
                         num_iterations=60,
                         step_size=10.0)
    # 滤波处理

    # 返回处理后文件名称和原始参数
    return save_img(res, filename)
Exemplo n.º 2
0
def rotate(filename: str, sp: list, size: int, degree: int):
    if degree == 90:
        angle = Image.ROTATE_90
    elif degree == 180:
        angle = Image.ROTATE_180
    else:
        angle = Image.ROTATE_270
    # 获取图像
    im = get_img(filename)
    width, height = im.size

    sp[0] = int(sp[0])
    sp[1] = int(sp[1])
    # 区域起点大于图像的尺寸
    if sp[0] > width or sp[1] > height:
        return filename
    # 设置窗口大小
    size_x = width - sp[0] if sp[0] + size > width else size
    size_y = height - sp[1] if sp[1] + size > height else size
    size = size_x if size_x < size_x else size_y
    # 创建矩形窗,左-上-右-下
    box = (sp[0], sp[1], sp[0] + size, sp[1] + size)
    # 裁剪窗口
    region = im.crop(box)
    region = region.transpose(angle)
    im.paste(region, box)
    return save_img(im, filename)
Exemplo n.º 3
0
def mosaic(filename: str, sp: list, ep: list, granularity: int) -> tuple:
    olddata = '#'.join(sp + ep) + '#' + str(granularity)
    # 获取图像
    im = get_img(filename)
    width, height = im.size
    # 创建Draw对象
    draw = ImageDraw.ImageDraw(im=im)
    sp[0] = int(sp[0])
    sp[1] = int(sp[1])
    # 区域起点大于图像的尺寸
    if sp[0] > width or sp[1] > height or granularity < 1:
        return filename, olddata
    # 处理终点
    ep[0] = width if int(ep[0]) > width else int(ep[0])
    ep[1] = height if int(ep[1]) > height else int(ep[1])

    # 正式打马赛克
    for x in range(int(sp[0]), ep[0], granularity):
        for y in range(int(sp[1]), ep[1], granularity):
            # 选取小方块中心的像素点填充整个小方块
            r, g, b = im.getpixel((x, y))
            draw.rectangle([(x, y),
                            (x + granularity - 1, y + granularity - 1)],
                           fill=(r, g, b))
    # 保存并返回文件名
    return save_img(im, filename), olddata
Exemplo n.º 4
0
def overlay(filename1, filename2, alpha: float):
    im1 = get_img(filename1)
    im2 = get_img(filename2)
    w = im1.size[0] if im1.size[0] > im2.size[0] else im2.size[0]
    h = im1.size[1] if im1.size[1] > im2.size[1] else im2.size[1]
    size = (w, h)
    base1 = Image.new('RGB', size, ImageColor.getrgb('white'))
    base2 = Image.new('RGB', size, ImageColor.getrgb('white'))
    base1.paste(im1, (0, 0))
    base2.paste(im2, (0, 0))
    base1.show()
    base2.show()

    res = Image.blend(base1, base2, alpha)
    res.show()
    return save_img(res, filename1)
Exemplo n.º 5
0
def water_mask(filename: str, mask_filename: str):
    # 获取图像
    im = get_img(filename)
    width, height = im.size

    # 获取水印图片
    mask_filename = convert_img(filename, mask_filename)
    mask_im = get_img(mask_filename)
    mw, mh = mask_im.size
    mw, mh = 250, int(mh * 250 / mw)
    mask_im = mask_im.resize((mw, mh))

    # 裁剪原来的图像区域并混叠
    region = im.crop((width - mw, height - mh, width, height))
    new = Image.blend(mask_im, region, 0.5)

    im.paste(new, (width - mw, height - mh))

    return save_img(im, filename)
Exemplo n.º 6
0
def img_filter(form, filename, **kwargs):
    mode = kwargs.get('mode', None)

    img = get_img(filename)

    # 读取滤波方式
    im_filter, olddata = get_filter(mode, form)

    # 滤波处理
    res = img.filter(im_filter)
    # 返回处理后文件名称和原始参数
    return save_img(res, filename), olddata
Exemplo n.º 7
0
def img_kernel(filename, size, kernel, scale=None, offset=None):
    im = get_img(filename)
    if scale is not None and scale.strip() != '':
        scale = float(scale)
    else:
        scale = None
    if offset is not None and offset.strip() != '':
        offset = float(offset)
    else:
        offset = 0
    im_filter = ImageFilter.Kernel(size, kernel, scale, offset)
    res = im.filter(im_filter)
    return save_img(res, filename)
Exemplo n.º 8
0
def img_enhance(filename, enhance_type, factor):
    im = get_img(filename)
    if enhance_type == 'Color':
        enhancer = ImageEnhance.Color(im)
    elif enhance_type == 'Contrast':
        enhancer = ImageEnhance.Contrast(im)
    elif enhance_type == 'Brightness':
        enhancer = ImageEnhance.Brightness(im)
    elif enhance_type == 'Sharpness':
        enhancer = ImageEnhance.Sharpness(im)
    else:
        raise NotExistEnhanceError()
    res = enhancer.enhance(factor)
    return save_img(res, filename)
Exemplo n.º 9
0
def text_mask(filename, text, color, font_size, start_point):
    olddata = text + '#' + color + '#' + font_size + '#' + start_point[
        0] + '#' + start_point[1]
    im = get_img(filename)
    draw = ImageDraw.ImageDraw(im)
    font = ImageFont.truetype(font=basedir +
                              '/static/font/STHeiti-Light-3.ttc',
                              size=int(font_size),
                              encoding='utf-8')
    if color in ImageColor.colormap:
        color = ImageColor.getrgb(color)
    else:
        color = ImageColor.getrgb('red')
    draw.text((int(start_point[0]), int(start_point[1])),
              text,
              fill=color,
              font=font)
    return save_img(im, filename), olddata
Exemplo n.º 10
0
    net_outp = loaded_model.get_layer(name="softmax").output
    test_func = K.function([net_inp], [net_outp])
    count = 0
    #preprocess data
    for index, img in enumerate(list_img):
        if K.image_data_format() == 'channels_first':
            img_dat = np.expand_dims(img, axis=3)
        else:
            img_dat = np.expand_dims(img, axis=0)
        img_dat = img_dat / 255
        c = np.expand_dims(np.array(img_dat).T, axis=0)
        top_pred_texts = decode_batch(test_func, c)[0]

        plt.subplot(5, 2, index + 1)
        plt.imshow(img, cmap="Greys_r")
        plt.xlabel('Truth = \'%s\'\nDecoded = \'%s\'' %
                   (label_list[index], top_pred_texts))

    fig = pylab.gcf()
    fig.set_size_inches(13, 16)
    plt.savefig(
        os.path.join(os.path.join(home_dir, "predict"),
                     "predict%02d.png" % (indx)))


list_img, label_list = get_img(test_data_dir, img_width, img_height)

run_name = "build"
predict(run_name, list_img, label_list, 201)
predict(run_name, list_img, label_list, 301, "gru_model.json",
        "gru_weights.h5")
Exemplo n.º 11
0
def convert_img(dst, src):
    dst_suffix = dst.split('.')[-1]
    new_filename = src.split('.')[0] + '.' + dst_suffix
    im = get_img(src)
    im = im.convert('RGB')
    return save_img(im, new_filename)