Mister Verleg
Mister Verleg

Reputation: 4303

How to strech and rotate right half of an image?

I have an image of size (600, 300) made with the following code:

from PIL import Image, ImageDraw

im = Image.new('RGB', (600, 300), (255,255,255))
draw = ImageDraw.Draw(im)
draw.rectangle((0, 0, 600, 300/3), fill=(174,28,40)) #rood
draw.rectangle((0, 200, 600, 400), fill=(33,70,139)) #rood
im.save('result.jpg', quality=95)

The image has three horizontal stripes with different colors (red, white and blue) like this:

rrrrrr 
wwwwww
bbbbbb

I want to take the second half of the image and rotate it clockwise by 90 degrees.

rrrrwb
wwwrwb
bbbrwb

Can this be done in Python?

Upvotes: 0

Views: 46

Answers (2)

Mister Verleg
Mister Verleg

Reputation: 4303

I was making the dutch/french flag combination

Using the help of HansHirse i could get my desired result, Namely a unison of the french and dutch flags.

from PIL import Image, ImageDraw

    im = Image.new('RGB', (600, 300), (255,255,255))
    draw = ImageDraw.Draw(im)
    draw.rectangle((0, 0, 600, 300/3), fill=(174,28,40)) #red
    draw.rectangle((0, 200, 600, 400), fill=(33,70,139)) #blue

    # the  rotation needs to be the other way around
    sub_image = im.crop(box=(300,0,600,300)).rotate(-90) # can use negative value
    im.paste(sub_image, box=(300,0)) # box=(0,300) to paste in front

    im.save('dutchFrench.jpg', quality=95)

enter image description here

Upvotes: 0

HansHirse
HansHirse

Reputation: 18905

Crop the right part of the image, rotate it by 90 degrees, and paste it back into the image. That all can be done in just one line:

from PIL import Image, ImageDraw, ImageOps

im = Image.new('RGB', (600, 300), (255, 255, 255))
draw = ImageDraw.Draw(im)
draw.rectangle((0, 0, 600, 300/3), fill=(174, 28, 40))
draw.rectangle((0, 200, 600, 400), fill=(33, 70, 139))

# Crop right part of image, rotate by 90 degrees, and paste back into image
im.paste(im.crop((300, 0, 600, 300)).rotate(90), (300, 0))

im.save('result.jpg', quality=95)

Result

Hope that helps!

Upvotes: 1

Related Questions