如何拉伸和旋转图像的右半部分?

2024-06-08 05:07:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大小为(600, 300)的图像,使用以下代码制作:

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)

图像有三条不同颜色(红色、白色和蓝色)的水平条纹,如下所示:

rrrrrr 
wwwwww
bbbbbb

我想拍摄图像的后半部分,并将其顺时针旋转90度

rrrrwb
wwwrwb
bbbrwb

这可以用Python实现吗


Tags: 代码from图像imageimportnewpilrgb
2条回答

Crop图像的右侧部分,rotate将其旋转90度,然后paste将其返回到图像中。所有这些都可以在一行中完成:

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

希望有帮助

我在做荷兰/法国国旗的组合

在汉希尔的帮助下,我可以得到我想要的结果,即法国和荷兰国旗的统一

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

相关问题 更多 >

    热门问题