Can I use use areaAverage() to analyze an actual image?



  • For an upcoming course—and for my book on Jurriaan Schrofer—I'm trying to recreate the process some of Schrofer's former colleagues at Total Design used in 1987 to rasterize a photo of him with various weights of one of his script designs.

    schrofer_head.jpg

    It seems like I should be able to use areaAverage() to scan a grid of areas in a given image, get the average color of each area, and then use that information to choose an appropriate weight of the glyph that will act as a pixel for that area in the generated image. That is, I'll get a 1px image with that average color—which I can presumably then extract the color information from to use further?

    But: the output of my code suggests that areaAverage() isn't seeing the image in the ImageObject. I'm saving the images in an array to analyze them one at a time; here's the code:

    path = '/Users/meilleur/Desktop/maurice-meilleur-web-600p_hi.png'
    
    w, h = imageSize(path)
    
    newPage(w, h)
    im = ImageObject()
    with im:
        size(w, h)
        image(path, (0, 0))
    image(im, (0, 0))
    
    res = 60
    s = w/res
    areas = []
    startX = 0; startY = 0
    for row in range(res):
        y = startY + row * s
        temp = []
        for col in range(res):
            x = startX + col * s
            temp.append(im.areaAverage((x, y, s, s)))
        areas.append(temp)
    print(imagePixelColor(areas[0][0], (0, 0)))
    

    And this throws an error:

    Traceback (most recent call last):
    File "schrofer_illegible_raster.py", line 23, in <module>
    File "/Applications/DrawBot.app/Contents/Resources/lib/python3.9/drawBot/drawBotDrawingTools.py", line 2020, in imagePixelColor
    AttributeError: 'NoneType' object has no attribute 'startswith'

    Obviously I'm missing something here …



  • areaAverage() does not return anything, but changes the imageObject.

    p = 'schrofer_head.jpg'
    
    im = ImageObject(p)
    print(im.size()) # (1365.0, 1365.0)
    
    im.areaAverage()
    print(im.size()) # (1.0, 1.0)
    
    print(imagePixelColor(im, (0, 0))) # (0.6, 0.6039215686274509, 0.6, 1.0)
    
    

  • admin

    thanks @monomonnik that is correct



  • Thanks—I guess this makes sense, but the docs suggest that it's possible to add arguments to specify an 'area of interest' (https://www.drawbot.com/content/image/imageObject.html#drawBot.context.tools.imageObject.ImageObject.areaAverage). In any case I found a workaround by getting creative with sampling pixels in the image.



  • The thing that tripped me up initially was ‘Returns a single-pixel image…’. I was (mis)reading that and assumed that ‘returns’ meant ‘returns a new image’ instead of altering the imageObject.

    I’m not sure if the documentation is a little confusing, or that the behaviour of areaAverage is unexpected.


Log in to reply