def test_size_property(self):
     img = Image((500, 300), 'red')
     self.assertEqual(img.width, 500)
     self.assertEqual(img.height, 300)
     img.scale(0.5)
     self.assertEqual(img.width, 250)
     self.assertEqual(img.height, 150)
 def test_size_property(self):
     im = Image((500, 300), 'red')
     self.assertEqual(im.width, 500)
     self.assertEqual(im.height, 300)
     im.scale(0.5)
     self.assertEqual(im.width, 250)
     self.assertEqual(im.height, 150)
 def test_unicodefilename(self):
     self.img.write('unicode.png')
     if sys.version_info >= (3, ):
         img = Image('unicode.png')
     else:
         img = Image(unicode('unicode.png'))
     img.scale(0.5)
     if sys.version_info >= (3, ):
         img.write('unicode.jpg')
     else:
         img.write(unicode('unicode.jpg'))
 def test_unicodefilename(self):
     self.img.write('unicode.png')
     if sys.version_info >= (3, ):
         img = Image('unicode.png')
     else:
         img = Image(unicode('unicode.png'))
     img.scale(0.5)
     if sys.version_info >= (3, ):
         img.write('unicode.jpg')
     else:
         img.write(unicode('unicode.jpg'))
Exemple #5
0
def _ensure(id_, folderer, getter):
    folder = folderer(id_)
    # Create folder if it does not exists
    if not os.path.exists(folder):
        os.makedirs(folder)
    # Download image
    original = os.path.join(folder, ORIGINAL_FILENAME)
    if not os.path.exists(original):
        getter(original)
    # Create thumbnail
    thumb = os.path.join(folder, THUMBNAIL_FILENAME)
    if os.path.exists(original) and not os.path.exists(thumb):
        img = Image(original)
        img.scale((THUMBNAIL_SIZE, THUMBNAIL_SIZE))
        img.write(thumb)
Exemple #6
0
    def test_scale_with_filtertype(self):
        testset = {"Catrom": None, "Cubic": None, "None": None}
        for k, v in testset.items():
            img = Image((600, 400), 'gradient:#ffffff-#000000')
            if sys.platform.lower() == 'darwin':
                img.font("/Library/Fonts/Arial.ttf")
            img.annotate("hello", (100, 100))
            if k != "None":
                img.scale(0.6, k)
            else:
                img.scale(0.6)
            img.write('t.jpg')
            m = hashlib.md5()
            with open('t.jpg', 'rb') as fp:
                m.update(fp.read())
                testset[k] = m.hexdigest()

        self.assertNotEqual(testset["Catrom"], testset["None"])
        self.assertNotEqual(testset["Cubic"], testset["None"])
        self.assertNotEqual(testset["Catrom"], testset["Cubic"])
 def test_scale_with_filtertype(self):
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6, 'Catrom')
     img.write('t.jpg')
     m = hashlib.md5()
     with open('t.jpg', 'rb') as fp:
         m.update(fp.read())
         scale_with_filtertype_catrom_digest = m.hexdigest()
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6, 'Cubic')
     img.write('t.jpg')
     m = hashlib.md5()
     with open('t.jpg', 'rb') as fp:
         m.update(fp.read())
         scale_with_filtertype_cubic_digest = m.hexdigest()
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6)
     img.write('t.jpg')
     m = hashlib.md5()
     with open('t.jpg', 'rb') as fp:
         m.update(fp.read())
         scale_digest = m.hexdigest()
     self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_digest)
     self.assertNotEqual(scale_with_filtertype_catrom_digest,
                         scale_with_filtertype_cubic_digest)
Exemple #8
0
 def test_scale_with_filtertype(self):
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6, 'Catrom')
     img.write('t.jpg')
     m = hashlib.md5()
     with open('t.jpg', 'rb') as fp:
         m.update(fp.read())
         scale_with_filtertype_catrom_digest = m.hexdigest()
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6, 'Cubic')
     img.write('t.jpg')
     m = hashlib.md5()
     with open('t.jpg', 'rb') as fp:
         m.update(fp.read())
         scale_with_filtertype_cubic_digest = m.hexdigest()
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6)
     img.write('t.jpg')
     m = hashlib.md5()
     with open('t.jpg', 'rb') as fp:
         m.update(fp.read())
         scale_digest = m.hexdigest()
     self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_digest)
     self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_with_filtertype_cubic_digest)
 def test_scale(self):
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6)
     img.write('t.jpg')
 def test_unicodefilename(self):
     self.img.write('unicode.png')
     img = Image(u'unicode.png')
     img.scale(0.5)
     img.write(u'unicode.jpg')
 def test_scale(self):
     im = Image((600, 400), 'red')
     im.scale(0.6)
     im.write('t.jpg')
Exemple #12
0
 def test_scale(self):
     im = Image((600, 400), 'red')
     im.scale(0.6)
     im.write('t.jpg')
Exemple #13
0
class Image( object ):
    """
    This is pgmagick.api.Image adapter.And add some util tools.
    """

    def __init__( self, filename = None, color = None, *args, **kargs ):
        """
        Init a adapter.

        :Args:
         - filename - This param is a file name or Image of pgmagick.
         - Color - This is color of pgmagick.api.
        """

        if isinstance( filename, OrginallyImage ):

            self.__apiImage = ApiImage()
            self.__apiImage.img = filename
        else:

            self.__apiImage = ApiImage( filename, color, args, kargs )

    def border( self, size, color_str = "white" ):
        """
        Set a border and color.

        :Args:
         - size - This is dict. (example: {"width":2,"height":2} )
         - color_str - The default value is white. ( example: "#ffeeff" )
        """

        img = self.get_pgmagick_image()

        img.borderColor( Color( color_str ) )

        img.border( Geometry( size.get("width"), size.get("height") ) );


    def crop( self, bounds ):
        """
        Crop this image.

        :Args:
         - bounds - This is bounds dict. ( example:{"width":40,"height":40,"x":0,"y":0} )
        """

        img = self.get_pgmagick_image()

        img.crop( Geometry( bounds.get("width"), bounds.get("height"), bounds.get("x"), bounds.get("y") ) )


    def resize( self, size, is_force = False ):
        """
        Reset a size.
        
        :Args:
         - size - This is dict. (example: {"width":2,"height":2} )
         - isForce - This is boolean.The image width or height will be possible less to set size,when isForce is true.
        """

        if is_force:

            self.__apiImage.scale( ( size.get("width"), size.get("height") ) )

        else:

            height = self.__apiImage.height

            width = self.__apiImage.width

            ratio_height = float( size.get("height") ) / float( height )

            ratio_width = float( size.get("width") ) / float( width )

            ratio = 1

            if ratio_height < ratio_width:

                ratio = ratio_width

            else:

                ratio = ratio_height

            self.__apiImage.scale( ( ratio * width, ratio * height ) )


    def get_blob( self ):
        """
        Get a blob object.
        """

        blob = Blob()

        img = self.get_pgmagick_image()
        img.write( blob, "png" )

        return blob


    def get_pgmagick_image( self ):
        """
        Get a pgmagick image.
        """

        return self.__apiImage.img


    def get_api_image( self ):
        """
        Get a pgmagick.api image.
        """

        return self.__apiImage


    def __create_arrow_bg( self, points, color ):
        """
        Create a draw of arrow background.
        """

        draw = Draw()
        draw.stroke_antialias( False )
        draw.polygon( points )
        draw.fill_color( color )

        return draw

    def __create_arrow_border( self, start_x, end_x , y, arrow_height, color ):
        """
        Create a draw of arrow border.
        """

        center_x = ( start_x + end_x ) /2
        center_y = arrow_height + y

        arrow_border_draw = Draw()
        arrow_border_draw.stroke_color( color )
        arrow_border_draw.stroke_antialias( False )
        arrow_border_draw.line( start_x, y, center_x, center_y )
        arrow_border_draw.line( center_x, center_y, end_x, y )
        arrow_border_draw.stroke_width( 2 )

        return arrow_border_draw


    def arrow( self, background_color = "#FFFFFF", border_color = "#BBBBBB" ):
        """
        Draw a arrow at icon bottom.
        """
        img = self.get_api_image()

        width = img.width
        height = img.height
        center_x = width / 2
        arrow_half_width = int( center_x / 3 )

        arrow_height = int( height / 7 )

        bg_img = ApiImage(( width, height + arrow_height + 1 ),'transparent')

        #
        arrow_start_x = center_x - arrow_half_width
        arrow_end_x = center_x + arrow_half_width
        center_y = height + arrow_height

        arrow_bg_start_point = ( arrow_start_x, height -1 )
        arrow_bg_end_point = ( arrow_end_x, height - 1 )
        arrow_bg_center_point = ( center_x, height + arrow_height )
        
        draw = Draw()
        draw.composite( 0, 0, width, height, img.img )

        bg_img.draw( draw )

        points = ( arrow_bg_start_point, arrow_bg_end_point, arrow_bg_center_point, arrow_bg_start_point )

        bg_img.draw( self.__create_arrow_bg( points, background_color ) )

        bg_img.draw( self.__create_arrow_border( arrow_start_x, arrow_end_x, height, arrow_height, border_color ) )

        self.__apiImage = bg_img
Exemple #14
0
 def test_scale(self):
     img = Image((600, 400), 'gradient:#ffffff-#000000')
     img.scale(0.6)
     img.write('t.jpg')
Exemple #15
0
 def test_unicodefilename(self):
     self.img.write('unicode.png')
     img = Image(u'unicode.png')
     img.scale(0.5)
     img.write(u'unicode.jpg')