Python PIL.Image 模块,PERSPECTIVE 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用PIL.Image.PERSPECTIVE

项目:road_simulator    作者:vinzeebreak    | 项目源码 | 文件源码
def call(self, img):

        if img is None:
            raise ValueError('img is None')

        width, height = img.size
        sym = img.copy()

        symmetry = False
        if random() < self.proba:
            from_points = [(0, 0), (width-1, 0), (width-1, height-1), (0, height-1)]
            new_points = [(width-1, 0), (0, 0), (0, height-1), (width-1, height-1)]
            coeffs = find_coeffs(new_points, from_points)
            sym = sym.transform((width, height), Image.PERSPECTIVE, coeffs, Image.BICUBIC)
            symmetry = True
        return sym, symmetry
项目:captcha_project    作者:zhanghe06    | 项目源码 | 文件源码
def get(self):
        if self.draw_lines:
            self._create_lines()
        if self.draw_points:
            self._create_points()
        code_str = self._create_code_str()

        # ??????
        params = [1 - float(random.randint(1, 2)) / 100,
                  0,
                  0,
                  0,
                  1 - float(random.randint(1, 10)) / 100,
                  float(random.randint(1, 2)) / 500,
                  0.001,
                  float(random.randint(1, 2)) / 500
                  ]
        img = self.img.transform(self.size, Image.PERSPECTIVE, params)  # ????

        img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)  # ?????????????

        return img, code_str
项目:1-tk1-zener-learner    作者:mlennox    | 项目源码 | 文件源码
def create_perspective(img, factor):
    img_size = img.size
    w = img_size[0]
    h = img_size[1]
    shifts = generate_random_shifts(img_size, factor)
    coeffs = find_coeffs(
        [(shifts[0][0], shifts[0][1]),
            (w + shifts[1][0], shifts[1][1]),
            (w + shifts[2][0], h + shifts[2][1]),
            (shifts[3][0], h + shifts[3][1])], [(0, 0), (w, 0), (w, h), (0, h)])
    return img.transform((w, h), Image.PERSPECTIVE, coeffs, Image.BICUBIC)


# due to rotation and/or perspective we will need to fill in the background
项目:vsi_common    作者:VisionSystemsInc    | 项目源码 | 文件源码
def sample_patch_perspective(image, inv_xform_3x3, patch_size):
  """ return an Image of size patch_size """
  patch_size_tuple = (patch_size[0], patch_size[1])
  inv_xform_array = inv_xform_3x3.reshape(9,) / inv_xform_3x3[2,2]
  patch = image.transform(patch_size_tuple, Image.PERSPECTIVE, inv_xform_array, Image.NEAREST)

  ones_img = Image.new('L', image.size, 255)
  mask = ones_img.transform(patch_size_tuple, Image.PERSPECTIVE, inv_xform_array, Image.NEAREST)
  return patch, mask
项目:flask-maple    作者:honmaple    | 项目源码 | 文件源码
def create_validate_code(self,
                             size=(120, 30),
                             chars=init_chars,
                             img_type="GIF",
                             mode="RGB",
                             bg_color=(255, 255, 255),
                             fg_color=(0, 0, 255),
                             font_size=18,
                             font_type=fontType,
                             length=4,
                             draw_lines=True,
                             n_line=(1, 2),
                             draw_points=True,
                             point_chance=2):

        width, height = size
        img = Image.new(mode, size, bg_color)
        draw = ImageDraw.Draw(img)
        if draw_lines:
            self.create_lines(draw, n_line, width, height)
        if draw_points:
            self.create_points(draw, point_chance, width, height)
            strs = self.create_strs(draw, chars, length, font_type, font_size,
                                    width, height, fg_color)

        params = [1 - float(randint(1, 2)) / 100, 0, 0, 0,
                  1 - float(randint(1, 10)) / 100, float(randint(1, 2)) / 500,
                  0.001, float(randint(1, 2)) / 500]
        img = img.transform(size, Image.PERSPECTIVE, params)

        img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)

        return img, strs
项目:road_simulator    作者:vinzeebreak    | 项目源码 | 文件源码
def call(self, img):

        if img is None:
            raise ValueError('img is None')

        width, height = img.size
        from_points = [(0, 0), (width-1, 0), (width-1, height-1), (0, height-1)]
        new_points = [(self.new_width-1, 0),
                        (self.new_width+self.new_width-1, 0),
                        (self.new_width*2+self.new_width-1, self.new_height-1),
                        (0, self.new_height-1)]
        coeffs = find_coeffs(new_points, from_points)
        img = img.transform((self.new_width+self.new_width*2, self.new_height),
                                Image.PERSPECTIVE, coeffs, Image.BICUBIC)
        return img
项目:antitools    作者:bufubaoni    | 项目源码 | 文件源码
def code_img(code, size):
    r = Random()
    code = code
    len_code = len(code)

    font = ImageFont.truetype("Essence_Sans.ttf", size)

    font_width, font_height = font.getsize(code)
    font_width += size / 2
    print font_width, font_height
    img = Image.new("RGBA", (font_width, font_height), (255,) * 4)

    draw = ImageDraw.ImageDraw(img)

    draw.text((size/10, -size/10), code, font=font, fill=(0, 0, 0))

    params = [1,
              0,
              0,
              0,
              1 - float(r.randint(1, 10)) / 100,
              0,
              0.001,
              float(r.randint(1, 2)) / 500
              ]
    print params
    img = img.transform((font_width, font_height), Image.PERSPECTIVE, params)
    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)

    img.save("test.jpg")
项目:cosinus    作者:hdk5    | 项目源码 | 文件源码
def draw_picture(img_src):
    fap_path = os.path.join(os.path.dirname(__file__), "fap.png")

    # ?????????? ????????
    response = requests.get(img_src)
    response.raise_for_status()
    image_bytes = io.BytesIO(response.content)

    # ?????????
    image = Image.open(image_bytes)
    fap_pic = Image.open(fap_path)

    if image.mode != 'RGBA':
        image = image.convert('RGBA')

    image_width, image_height = image.size
    fap_width, fap_height = fap_pic.size

    def find_coeffs(pa, pb):
        """ https://stackoverflow.com/questions/14177744/
            ????? ????????? ????, ??? ??????? - ???????
            """
        matrix = []
        for p1, p2 in zip(pa, pb):
            matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
            matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
        A = numpy.matrix(matrix, dtype=numpy.float)
        B = numpy.array(pb).reshape(8)
        res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
        return numpy.array(res).reshape(8)

    trans_coeff = find_coeffs(
        [(217,111),(412,115),(222,372),(403,371)],
        [(0,0), (image_width-1,0), (0,image_height-1), (image_width-1, image_height-1)])

    resp_pic = image.transform(fap_pic.size, Image.PERSPECTIVE, trans_coeff, Image.BILINEAR)

    # ??????????? gesture ? image
    resp_bytes = io.BytesIO()
    Image.alpha_composite(resp_pic, fap_pic).save(resp_bytes, format='PNG')
    resp_bytes.seek(0)

    return resp_bytes
项目:music_recommend    作者:YeEmrick    | 项目源码 | 文件源码
def create_validate_code(size=(120, 30),
                         chars=init_chars,
                         img_type="GIF",
                         mode="RGB",
                         bg_color=(245, 245, 245),
                         fg_color=color_random,
                         font_size=24,
                         font_type=fontType,
                         length=4,
                         draw_lines=True,
                         n_line=(1, 2),
                         draw_points=True,
                         point_chance=2):
    '''
    @todo: ???????
    @param size: ?????????????????(120, 30)
    @param chars: ?????????????
    @param img_type: ???????????GIF?????GIF?JPEG?TIFF?PNG
    @param mode: ????????RGB
    @param bg_color: ??????????
    @param fg_color: ?????????????????#0000FF
    @param font_size: ???????
    @param font_type: ????????? ae_AlArabiya.ttf
    @param length: ???????
    @param draw_lines: ??????
    @param n_lines: ?????????????????(1, 2)???draw_lines?True???
    @param draw_points: ??????
    @param point_chance: ?????????????[0, 100]
    @return: [0]: PIL Image??
    @return: [1]: ??????????
    '''

    width, height = size  # ?? ?
    img = Image.new(mode, size, bg_color)  # ????
    draw = ImageDraw.Draw(img)  # ????
    if draw_lines:
        create_lines(draw, n_line, width, height)
    if draw_points:
        create_points(draw, point_chance, width, height)
    strs = create_strs(draw, chars, length, font_type, font_size, width, height, fg_color())

    # ??????
    params = [1 - float(random.randint(1, 2)) / 100,
              0,
              0,
              0,
              1 - float(random.randint(1, 10)) / 100,
              float(random.randint(1, 2)) / 500,
              0.001,
              float(random.randint(1, 2)) / 500
              ]
    img = img.transform(size, Image.PERSPECTIVE, params)  # ????

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)  # ?????????????

    return img, strs