Variable portrait



  • Hi, I’m playing with variable fonts and images, I have two questions, how can I use just some characters, and which could be a better way to write the code, because it takes so long to render the gif and it always appears a firs blank page. Thank you all!

    import string
    import random
    
    # path to the image
    path = u"https://scontent.fmex6-1.fna.fbcdn.net/v/t1.0-9/42516210_1195851967229695_2595517735622410240_n.png?_nc_cat=103&_nc_oc=AQlcHYpbwrz7YCN-WPr0rUxI4-Glqu4ug8Oua-UKqAe-oQXGVyYMunKNOWrxKscDWMk&_nc_ht=scontent.fmex6-1.fna&oh=6bb9b894653113271e6cac9b723e4d81&oe=5E63F271"
    
    # get the size of the image
    print(imageSize("https://scontent.fmex6-1.fna.fbcdn.net/v/t1.0-9/42516210_1195851967229695_2595517735622410240_n.png?_nc_cat=103&_nc_oc=AQlcHYpbwrz7YCN-WPr0rUxI4-Glqu4ug8Oua-UKqAe-oQXGVyYMunKNOWrxKscDWMk&_nc_ht=scontent.fmex6-1.fna&oh=6bb9b894653113271e6cac9b723e4d81&oe=5E63F271"))
    
    # w, h = imageSize(path)
    w = 1656
    h = 1656
    
    # shift it up a bit
    translate(0, 2)
    
    min_val = listFontVariations('League Spartan Variable')['wght']['minValue']
    max_val = listFontVariations('League Spartan Variable')['wght']['maxValue']
    
    # setup a variable for the font size as for the steps
    s = 10
    w = 560    
    h = 560
    
    def ranLetters():
        # loop over the width of the image    
        newPage(w, h)
        fill(randint(0, 1))
        rect(0, 0, w, h)
        for x in range(0, w, s):
            # loop of the height of the image
            for y in range(0, h, s):
                # get the color            
                color = imagePixelColor(path, (x, y))
                if color:
                    font("League Spartan Variable")
                    fontSize(s)
                    lineHeight(s * 0.75)
                    txt = random.choice(string.ascii_letters)
                
                    fontVariations(wght = randint(min_val, max_val))
                
                    r, g, b, a = color
                    # set the color
                    fill(r, g, b, a)
                    # draw some text
                    text(txt, (x, y))
                    
    for i in range(10):
        ranLetters()
                
    saveImage("~/Desktop/ee.gif")
    


  • hi @eduairet
    you can submit a string of characters to the random choice function:

    chars = 'aeiou'
    print (random.choice(chars))
    

    any call of a ‘drawing function’ will make a new Page. Remove the translate(0, 2) to not have the blank page.

    Finally getting the image pixel color takes some time. Try to move stuff outside of the loops if they do not change. Eg try a loop outside the ranLetters() function to get and store the pixel colors, so you do not have to do that for every page.

    pix_cols = {}
    
    for x in range(0, w, s):
        for y in range(0, h, s):
            pix_cols[(x, y)] = imagePixelColor(path, (x, y))
    

    then inside the loop just ask for the stored value:

    color = pix_cols[(x, y)]
    

    also these three lines do not change so they could be outside of the loop:

    font("League Spartan Variable")
    fontSize(s)
    lineHeight(s * 0.75)
    

    good luck!



  • @jo Thank you very much for the help! I think I don't really know how the translate function works, I'll take a look on it later, by the way, you solved my problem, the only thing that never worked is the font outside the loop, I don't know why. Thanks!!! ☺



  • import string
    import random
    
    # path to the image
    path = u"https://yourimage.jpg"
    
    # get the size of the image
    print(imageSize("https://yourimage.jpg"))
    
    # w, h = imageSize(path) s = letter size
    w = 400
    h = 400
    s = 10
    
    # Set the characters you need
    chars = 'HOLAhola'
    print (random.choice(chars))
    
    # Get colors
    pix_cols = {}
    for x in range(0, w, s):
        for y in range(0, h, s):
            pix_cols[(x, y)] = imagePixelColor(path, (x, y))
            
    # Variable font to use       
    """
    FontName
    wdth {'name': 'Width', 'minValue': 100.0, 'maxValue': 800.0, 'defaultValue': 100.0}
    wght {'name': 'Weight', 'minValue': 150.0, 'maxValue': 800.0, 'defaultValue': 150.0}
    slnt {'name': 'Slant', 'minValue': 0.0, 'maxValue': 14.0, 'defaultValue': 0.0}
    """
    
    min_val_wdth = listFontVariations('FontName')['wdth']['minValue']
    max_val_wdth = listFontVariations('FontName')['wdth']['maxValue']
    min_val_wght = listFontVariations('FontName')['wght']['minValue']
    max_val_wght = listFontVariations('FontName')['wght']['maxValue']
    min_val_slnt = listFontVariations('FontName')['slnt']['minValue']
    max_val_slnt = listFontVariations('FontName')['slnt']['maxValue']
    
    def ranLetters():
        # loop over the width of the image    
        newPage(w, h)
        fill(1)
        rect(0, 0, w, h)
        
        for x in range(0, w, s):
            # loop of the height of the image
            for y in range(0, h, s):
                color = pix_cols[(x, y)]
                if color:
                    txt = random.choice(chars)
                    font("FontName")
                    fontSize(s)
                    
                    fontVariations(wdth = randint(min_val_wdth, max_val_wdth), wght = randint(min_val_wght, max_val_wght))
    
                    r, g, b, a = color
                    # set the color
                    fill(r, g, b, a)
                    # draw some text
                    text(txt, (x, y), align='center')
                    
    for i in range(5):
        ranLetters()
                
    saveImage("~/Desktop/name.gif", imageResolution=144)