Python图片转换bug|私教学员答疑

你好,我是悦创。

原本代码:

# install in terminal before use it
# pip install pillow
from PIL import Image

# Load the original image
original_image = Image.open("hier/photo.png")

# Define the sizes and filenames
sizes_and_filenames = [
    (50, 50, "StoreLogo.png"),
    (32, 32, "32x32.png"),
    (128, 128, "128x128.png"),
    (256, 256, "icon.ico"),
    (512, 512, "icon.png"),
    (30, 30, "Square30x30Logo.png"),
    (44, 44, "Square44x44Logo.png"),
    (71, 71, "Square71x71Logo.png"),
    (89, 89, "Square89x89Logo.png"),
    (107, 107, "Square107x107Logo.png"),
    (142, 142, "Square142x142Logo.png"),
    (150, 150, "Square150x150Logo.png"),
    (284, 284, "Square284x284Logo.png"),
    (310, 310, "Square310x310Logo.png")
]

# Resize and save images
for size, size_name in sizes_and_filenames:
    resized_image = original_image.resize((size, size), Image.ANTIALIAS)
    resized_image.save(size_name)

# Special handling for icon.icns and 128x128@2x.png
original_image.resize((512, 512), Image.ANTIALIAS).save("icon.icns")
original_image.resize((256, 256), Image.ANTIALIAS).save("128x128@2x.png")

print("Images have been resized and saved.")

修改后代码:

from PIL import Image

# Load the original image
original_image = Image.open("hier/photo.png")

# Define the sizes and filenames
sizes_and_filenames = [
    (50, 50, "StoreLogo.png"),
    (32, 32, "32x32.png"),
    (128, 128, "128x128.png"),
    (256, 256, "icon.ico"),
    (512, 512, "icon.png"),
    (30, 30, "Square30x30Logo.png"),
    (44, 44, "Square44x44Logo.png"),
    (71, 71, "Square71x71Logo.png"),
    (89, 89, "Square89x89Logo.png"),
    (107, 107, "Square107x107Logo.png"),
    (142, 142, "Square142x142Logo.png"),
    (150, 150, "Square150x150Logo.png"),
    (284, 284, "Square284x284Logo.png"),
    (310, 310, "Square310x310Logo.png")
]

# Resize and save images
for width, height, filename in sizes_and_filenames:
    resized_image = original_image.resize((width, height), Image.Resampling.LANCZOS)
    resized_image.save(filename)

# Special handling for icon.icns and 128x128@2x.png
original_image.resize((512, 512), Image.Resampling.LANCZOS).save("icon.icns")
original_image.resize((256, 256), Image.Resampling.LANCZOS).save("128x128@2x.png")

print("Images have been resized and saved.")
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI悦创|编程1v1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值