python图形化编程更改内部参数,在使用PIL在python中旋转时,指定图像填充颜色,并将展开参数设置为true...

I'm trying to rotate an image in Python using PIL and having the expand argument to true. It seems that when the background of my image is black, the resulting image saved as a bmp will be a lot smaller than if I have a white background for my image, and then I replace the black due to expand with white. In either case, my original image is always of two colors, and right now i need the file size to be small, since I'm putting these images on an embedded device.

Any ideas if i can force rotate to fill in another color when expanding or if there is another way to rotate my picture in order to make it small?

解决方案

If your original image has no alpha layer, you can use an alpha layer as a mask to convert the background to white. When rotate creates the "background", it makes it fully transparent.

# original image

img = Image.open('test.png')

# converted to have an alpha layer

im2 = img.convert('RGBA')

# rotated image

rot = im2.rotate(22.2, expand=1)

# a white image same size as rotated image

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

# create a composite image using the alpha layer of rot as a mask

out = Image.composite(rot, fff, rot)

# save your work (converting back to mode='1' or whatever..)

out.convert(img.mode).save('test2.bmp')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值