Image rotation issue

kai.peters at gmail.com kai.peters at gmail.com
Mon Mar 30 17:22:05 EDT 2015


Last week some readers have kindly supplied ideas and code for a question I had asked around a form of image data compression required for specialized display hardware.

I was able to solve my issues for all but one:

The black & white only device (1024 (X) x 1280 (Y)) expects the compressed data based on portrait mode, i.e. 8 pixels combined into one bytes for 1280 rows of 128 bytes. 

This was working well until the desire came up to be able to tilt the display and use it in landscape mode - now I needed to rotate the text, as Pillow does not seem to support drawing at angles.

No big deal - or so I thought after discovering rotate:

from PIL import Image, ImageFont, ImageDraw

white = 1
black = 0
img   = Image.new('1', (1280, 1024), white)  # start in landscape mode since we need to calc. based on that
draw  = ImageDraw.Draw(img)
fontname = 'FreeSansBold.ttf'
    
# in real life, x and y are calculated to center or align text both vertically and horizontally
x = 10
y = 500

dfont = ImageFont.truetype(fontname, 96)

draw.text((x, y), 'Hallo world', black, font = dfont)
draw   = ImageDraw.Draw(img)
    
rotimg = img.rotate(270) # rotation is counterclockwise
    
# i can almost make it work by resizing rotimg here, but the aspect ratio is then screwed
#rotimg = rotimg.resize((1024, 1280))
    
rotimg.show()
imagedata = list(rotimg.getdata())
    
But grabbing data from the rotimg does not work as it does not seem to return an image with swapped dimensions...

What am I missing? 



More information about the Python-list mailing list