Rotating PIL image doesn't seem to rotate the canvas (TKinter canvas is not added)


right eye

I have a problem with rotating the created image. Because the code is scattered across many methods, I've listed below the commands I think are relevant.

The problem is, when the image is created successfully, when I rotate the image using img.rotate(-90)... the image rotates, but it looks like the tray/background/canvas is not rotated (see attached image).

How can I correct it. Do I need to create a larger transparent background? Is it possible to make the background/canvas also rotate...or can I rotate and then resize the background/canvas?

First example image (QRCODE)

img = Image.new('RGB', (x,y), 'white')
qr = qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=1,)
qr.add_data('QRB.NO/AbCd1')
qr.make(fit=True)
QRimg = qr.make_image()
img = img.paste(QRimg, (x,y))
img.show() #333
raw_input('(Above is unrotated QRcode image) Press enter...') #333
img = img.rotate(-90)
print img, type(img)
img.show() #333
raw_input('Above is the rotated -90 QRcode image. Press enter...') #333

second example image

font_name      = 'Arial.ttf'
font_size      = 16 
font = ImageFont.truetype(font_name, font_size)
img = Image.new('RGB', (x,y), color=background_color)
# Place text 
draw = ImageDraw.Draw(img)
draw.text( (corner_X,corner_Y), 'QRB.NO/AbCd1', font=font, fill='#000000' ) 
draw.rectangle((0,0,x-1,y-1), outline = "black")
del draw
print img, type(img)
img.show() #333
raw_input('(Above is the unrotated test image). Press enter...') #333
img = img.rotate(90)
print img, type(img)
img.show() #333
raw_input('(Above is the ROTATED 90 text image). Press enter...') #333

output

<PIL.Image.Image image mode=RGB size=71x57 at 0x10E9B8C10> <class 'PIL.Image.Image'>
(Above is unrotated QRcode image) Press enter...

<PIL.Image.Image image mode=RGB size=71x57 at 0x10E9B8F90> <class 'PIL.Image.Image'>
Above is the rotated -90 QRcode image. Press enter...

<PIL.Image.Image image mode=RGB size=57x9 at 0x10EA6CB90> <class 'PIL.Image.Image'>
(Above is the unrotated test image). Press enter...

<PIL.Image.Image image mode=RGB size=57x9 at 0x10E9B8C10> <class 'PIL.Image.Image'>
(Above is the ROTATED 90 text image). Press enter...


bad rotation

edit:

x,y = img.size
img = img.resize( (y, x),  Image.ANTIALIAS )
img = img.rotate(-90)

...or...

x,y = img.size
img = img.rotate(-90)
img = img.resize( (y, x),  Image.ANTIALIAS )

... doesn't seem to help.

try resize and rotate

right eye

Figured it out. I'm not going to help others because it seems like a subtle but important distinction.

img = img.transpose(Image.ROTATE_270) 

...or...

img = img.transpose(Image.ROTATE_90) 

document

Related


Rotate square on Tkinter canvas

Robin Andrews I'm close, but have two problems. One is the final position of the rotated square (no longer centered). The second is that my original vertex list changed, although I copied it using list(..). As usual any help is greatly appreciated. from Tkinte

Rotate square on Tkinter canvas

Robin Andrews I'm close, but have two problems. One is the final position of the rotated square (no longer centered). The second is that my original vertex list changed, although I copied it using list(..). As usual any help is greatly appreciated. from Tkinte

Rotate line in Tkinter canvas

vega2015 I have a line on a canvas and I want to rotate it by x angle while keeping the starting point in the middle of the canvas, but I would like to know what is the formula to calculate the coordinates of the ending point? degrees=xnumberofdegrees lineEndP

Rotate square on Tkinter canvas

Robin Andrews I'm close, but have two problems. One is the final position of the rotated square (no longer centered). The second is that my original vertex list changed, although I copied it using list(..). As usual any help is greatly appreciated. from Tkinte

Rotate line in Tkinter canvas

vega2015 I have a line on a canvas and I want to rotate it by x angle while keeping the starting point in the middle of the canvas, but I would like to know what is the formula to calculate the coordinates of the ending point? degrees=xnumberofdegrees lineEndP

Tkinter canvas doesn't show whole photo image

Hello B I created a short script to display photos in a Tkinter canvas element. I resize the canvas element to be the same size as the photo, but when launching the window, no matter how much I enlarge the window, I usually only see a small portion of the imag

Tkinter canvas doesn't show whole photo image

Hello B I created a short script to display photos in a Tkinter canvas element. I resize the canvas element to be the same size as the photo, but when launching the window, no matter how much I enlarge the window, I usually only see a small portion of the imag

Tkinter canvas doesn't show whole photo image

Hello B I created a short script to display photos in a Tkinter canvas element. I resize the canvas element to be the same size as the photo, but when launching the window, no matter how much I enlarge the window, I usually only see a small portion of the imag

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Rotate the image but not the canvas

Pujan Srivastava I am using a background image on the canvas. I just want to rotate the background image by 90 degrees, but the context should not be rotated. If I use CSS transitions then the whole canvas rotates. What can I do? var can = document.getElementB

Rotate an existing image on the canvas

Alex Saidani I am trying to rotate an image already drawn on an HTML canvas like this: var canvas = document.getElementById("editorCanvas"); var ctx = canvas.getContext("2d"); var canvasOffset = $("#editorCanvas").offset(); var offsetX = canvasOffset.left; va

Rotate an image in the canvas

Yaroslav Shabanov I am trying to create some rotated images. So I created an array of canvases and put in each picture. But for some reason they don't show up. please help me. var canvas = [], ctx = [], particles = []; for (var i = 0; i < 10; i++){ sym.$( sy

Rotate the image but not the canvas

Pujan Srivastava I am using a background image on the canvas. I just want to rotate the background image by 90 degrees, but the context should not be rotated. If I use CSS transitions then the whole canvas rotates. What can I do? var can = document.getElementB

Rotate an image in the canvas

Yaroslav Shabanov I am trying to create some rotated images. So I created an array of canvases and put in each picture. But for some reason they don't show up. please help me. var canvas = [], ctx = [], particles = []; for (var i = 0; i < 10; i++){ sym.$( sy

Rotate an existing image on the canvas

Alex Saidani I am trying to rotate an image already drawn on an HTML canvas like this: var canvas = document.getElementById("editorCanvas"); var ctx = canvas.getContext("2d"); var canvasOffset = $("#editorCanvas").offset(); var offsetX = canvasOffset.left; va

Rotate the image but not the canvas

Pujan Srivastava I am using a background image on the canvas. I just want to rotate the background image by 90 degrees, but the context should not be rotated. If I use CSS transitions then the whole canvas rotates. What can I do? var can = document.getElementB