当前位置: 首页>>代码示例>>Python>>正文


Python Rectangle.to_png方法代码示例

本文整理汇总了Python中rectangle.Rectangle.to_png方法的典型用法代码示例。如果您正苦于以下问题:Python Rectangle.to_png方法的具体用法?Python Rectangle.to_png怎么用?Python Rectangle.to_png使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rectangle.Rectangle的用法示例。


在下文中一共展示了Rectangle.to_png方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Flattener

# 需要导入模块: from rectangle import Rectangle [as 别名]
# 或者: from rectangle.Rectangle import to_png [as 别名]
class Flattener(Field):
    def __init__(self, images, **kwargs):
        """Merge list of images.
    
        Insert i-th image on (i-1)-th on specified position,
        i = n, n-1, ..., 0.
        This field is usefull for views to join Images (packed in ImageWrapper)

        Args:
            images (tuple): (ImageWrapper, (x, y)) image and it's position
                relative to the background.
        """
        super(Flattener, self).__init__(**kwargs)
        self.images = images
        
        max_width = max([image.get_width() + width for image, (width, height) in images])
        max_height = max([image.get_height() + height for image, (width, height) in images])
        
        self.background = Rectangle((max_width, max_height),
                                    thickness=kwargs.get('thickness', 0))
 
    def to_png(self):
        
        def paste_pixels(pixel1, pixel2):
            """ paste pixel2 on pixel1 """
            r1, g1, b1, a1 = pixel1
            r2, g2, b2, a2 = pixel2
            if a1 and a2:
                sub_merge = lambda x1, x2: x1 + (x2 - x1) * a2 / 255
                r3 = sub_merge(r1, r2)
                g3 = sub_merge(g1, g2)
                b3 = sub_merge(b1, b2)
                a3 = max(a1, a2)
                return r3, g3, b3, a3
            else:
                if not a1:
                    return pixel2
                else:
                    return pixel1
                    
        
        def transparent_paste(background, image, position):
            width, height = image.size
            for x in range(width):
                for y in range(height):
                    pixel = image.getpixel((x, y))
                    background_position = position[0] + x, position[1] + y
                    try:
                        background_pixel = background.getpixel(background_position)
                        background.putpixel(background_position,
                                        paste_pixels(background_pixel, pixel))
                    except IndexError:
                        # end of background image
                        break
        
        background = self.background.to_png()
        background_image = background.get_image()
        for image, position in self.images:
            transparent_paste(background_image, image.get_image(), map(self.pt_to_px, position))
        background.set_image(background_image)
        return background
        
开发者ID:PP-TSD,项目名称:sdgen,代码行数:63,代码来源:flattener.py


注:本文中的rectangle.Rectangle.to_png方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。