當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。