python怎么弄成黑色背景图片,如何使用PIL(Python图像库)旋转图像并使黑色背景透明...

I want to rotate a gray "test" image and paste it onto a blue background image. Now I just can remove the black color after rotate my gray "test" image, but their is now a white color section. How can I use Python to change the "white" color section to blue?

Here is my code, can someone help me? I'd appreciate it.

dst_im = Image.new("RGBA", (196,283), "blue" )

im = src_im.convert('RGBA')

rot = im.rotate( angle, expand=1 ).resize(size)

f = Image.new( 'RGBA', rot.size, (255,)*4 )

im2 = Image.composite( rot, f, rot )

im2.convert(src_im.mode)

im2_width, im2_height = im2.size

cut_box = (0, 0, im2_width, im2_height )

paste_box = ( left, top, im2_width+left, im2_height+top )

region = im2.crop( cut_box )

dst_im.paste( region, paste_box )

dst_im.save("test.gif")

oZoHR.gif

解决方案

I have the impression that your code could be simplified as follows:

from PIL import Image

src_im = Image.open("winter3.jpg")

angle = 45

size = 100, 100

dst_im = Image.new("RGBA", (196,283), "blue" )

im = src_im.convert('RGBA')

rot = im.rotate( angle, expand=1 ).resize(size)

dst_im.paste( rot, (50, 50), rot )

dst_im.save("test.png")

This gives the following result:

aUeUK.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值