Exemple #1
0
def load_images(path):
    f = open(path, 'rb')
    profile_images = pickle.load(f)
    f.close()
    ids = []
    images = []
    target_width = 48
    target_height = 48

    for uid, image in profile_images.items():
        image = BytesIO(image)
        image = Image.open(image)
        image = image.convert("RGB")
        if image.size != (target_width, target_height):
            left = (image.width - target_width) / 2
            top = (image.height - target_height) / 2
            right = (image.width + target_width) / 2
            bottom = (image.height + target_height) / 2

            image.crop((left, top, right, bottom))
            image.show()

        image_np = np.array(image, dtype=np.float32) / 255
        ids.append(uid)
        images.append(image_np)

    ids = np.array(ids)
    images = np.stack(images)
    return ids, images
Exemple #2
0
    def get_complete_img(self, image):
        url = re.findall(r'url\("(.*?)"\);', image[0].get_attribute('style'))[0]
        print(url)

        img_position_list = [i.get_attribute('style') for i in image]
        img_position_list = [re.findall(r'background-position: -(.*?)px -?(.*?)px;', i)[0] for i in img_position_list]

        img = requests.get(url).content

        img = BytesIO(img)

        img = Image.open(img)

        new_img = Image.new('RGB', (260, 116))

        count_up = 0
        count_down = 0
        for i in img_position_list[:26]:
            img_crop = img.crop((int(i[0]), 58, int(i[0]) + 10, 116))
            new_img.paste(img_crop, (count_up, 0))
            count_up += 10

        for i in img_position_list[26:]:
            img_crop = img.crop((int(i[0]), 0, int(i[0]) + 10, 58))
            new_img.paste(img_crop, (count_down, 58))
            count_down += 10
        return new_img
Exemple #3
0
    def get_complete_image(self, image):
        # 用正则获取元素中的图片url链接
        image_url = re.search(r'url\("(.*?)"\);',
                              image[0].get_attribute('style')).group(1)
        # 获取列表中每张小图的位置偏移信息
        image_position_list = [i.get_attribute('style') for i in image]
        image_position_list = [
            re.search(r'position: -(.*?)px -?(.*?)px;', i).groups()
            for i in image_position_list
        ]

        # 访问图片链接,获取图片的二进制数据
        im = requests.get(image_url).content
        # PIL要从二进制数据读取一个图片的话,需要把其转化为BytesIO
        im = BytesIO(im)
        im = Image.open(im)
        # 生成一个新的相同大小的空白图片
        new_im = Image.new('RGB', (260, 116))

        # 设置一个粘贴坐标,坐标每次循环加10,则从左到右依次粘贴
        count_up = 0
        count_low = 0
        # 图片主要分为上下两个部分,所以分成两个循环分别粘贴
        # image_list前26个为上半部分,后26个为下半部分
        # 每个小图片大小为10,58
        for i in image_position_list[:26]:
            croped = im.crop((int(i[0]), 58, int(i[0]) + 10, 116))
            new_im.paste(croped, (count_up, 0))
            count_up += 10
        for i in image_position_list[26:]:
            croped = im.crop((int(i[0]), 0, int(i[0]) + 10, 58))
            new_im.paste(croped, (count_low, 58))
            count_low += 10
        print(new_im)
        return new_im
Exemple #4
0
def up_image():
    if request.method == 'POST' and request.files.get('image_file'):
        timec = str(time.time()).replace(".", "")
        file = request.files.get('image_file')
        img = file.read()
        img = BytesIO(img)
        img = Image.open(img, mode="r")
        img = img.crop((0, 0, 110, 20))
        # username = request.form.get("name")
        print("接收图片尺寸: {}".format(img.size))
        s = time.time()
        value = R.rec_image(img)
        e = time.time()
        print("识别结果: {}".format(value))
        # 保存图片
        print("保存图片: {}{}_{}.{}".format(api_image_dir, value, timec,
                                        image_suffix))
        file_name = "{}_{}.{}".format(value, timec, image_suffix)
        file_path = os.path.join(api_image_dir + file_name)
        img.save(file_path)
        result = {
            'time': timec,  # 时间戳
            'value': value,  # 预测的结果
            'speed_time(ms)': int((e - s) * 1000)  # 识别耗费的时间
        }
        img.close()
        return jsonify(result)
    else:
        content = json.dumps({"error_code": "1001"})
        resp = response_headers(content)
        return resp
Exemple #5
0
def load_cursor_from_svg(file: BinaryIO) -> AnimatedCursor:
    """
    Load a cursor from an SVG. SVG is expected to be square. If it is not square, this method will slide
    horizontally across the SVG using a height * height square, loading each following square as next frame of the
    cursor with a delay of 100 and hotspots of (0, 0). Note this method uses CairoSVG library to load and render
    SVGs of various sizes to bitmaps. For info on supported SVG features, look at the docs for the CairoSVG library.

    :param file: The file handler pointing to the SVG data.
    :return: An AnimatedCursor object, representing an animated cursor. Static cursors will only have 1 frame.
    """
    # Convert SVG in memory to PNG, and read that in with PIL to get the default size of the SVG...
    mem_png = BytesIO()
    cairosvg.svg2png(file_obj=file, write_to=mem_png)
    file.seek(0)
    size_ref_img = Image.open(mem_png)

    # Compute height to width ratio an the number of frame(Assumes they are stored horizontally)...
    h_to_w_multiplier = size_ref_img.size[0] / size_ref_img.size[1]
    num_frames = int(h_to_w_multiplier)

    if (num_frames == 0):
        raise ValueError(
            "Image width is smaller then height so this will load as a 0 frame cursor!!!"
        )

    # Build empty animated cursor to start stashing frames in...
    ani_cur = AnimatedCursor([Cursor() for i in range(num_frames)],
                             [100] * num_frames)

    for sizes in DEFAULT_SIZES:
        # For each default size, resize svg to it and add all frames to the AnimatedCursor object...
        image = BytesIO()
        cairosvg.svg2png(file_obj=file,
                         write_to=image,
                         output_height=sizes[1],
                         output_width=int(sizes[1] * h_to_w_multiplier))
        file.seek(0)
        image = Image.open(image)

        height = image.size[1]
        for i in range(num_frames):
            ani_cur[i][0].add(
                CursorIcon(
                    image.crop((i * height, 0, i * height + height, height)),
                    0, 0))

    return ani_cur