python - 列表迭代器中的项目值未更新

标签 python image python-3.x scope iterator

我有一些代码循环遍历 Logo 的属性,并且这些属性将转换为 Logo 本身。

问题是,我有一个“AntiAliasing/SuperSampling”功能,可以检查properties["antialias"] == True

如果启用 AA,它会暂时将某些大小调整属性覆盖为三倍大小、进行渲染,然后缩小到原始大小(除以三)。

这是重要的代码的简短部分:

if properties["antialias"]:
    properties["font_size"] *= 3
    properties["base_size"] = (properties["base_size"][0] * 3,
                               properties["base_size"][1] * 3)
    properties["padding"] = (properties["padding"][0] * 3,
                             properties["padding"][1] * 3)

在那里您可以看到 font_size(tuple) base_size(tuple) padding 都乘以三。这就是已加载的属性中的所有大小信息。

因此,在此之后,图像由 PIL(Pillow)生成,然后按以下方式缩小尺寸:

if properties["antialias"]:
    logo_image = logo_image.resize((round(logo_image.width  / 3),
                                    round(logo_image.height / 3)), Image.ANTIALIAS)

问题是,第一张图像效果很好。然后,该循环生成的其他图像都以三的呈指数增长。这显然不是指数代码。

我得出的结论是,调用的每个“属性”(每个循环使用的项目)字典都不会改变(可能只是修改后的值),而是重复乘以三。

(基本上我认为更改后的值在每个循环中保持不变。)

这就是您所期望的循环:

for properties in logo_list:

如果您需要阅读,这里是完整的代码。它从外部作用域变量和其他函数调用,但我认为这对这个问题并不重要。

for properties in logo_list:
    if weight in properties["weights"]:
        logo_name = "".join([region["text"] for region in properties["regions"]])
        logo_file = logo_name + "-" + file[:-4] + "." + properties["type"].lower()
        out_dir = os.path.join("out", logo_name)

        try:
            os.makedirs(out_dir)
        except FileExistsError:
            pass

        if properties["antialias"]:
            properties["font_size"] *= 3
            properties["base_size"] = (properties["base_size"][0] * 3,
                                       properties["base_size"][1] * 3)
            properties["padding"] = (properties["padding"][0] * 3,
                                     properties["padding"][1] * 3)

        logo_image = generate(properties, font_path)

        if properties["shrink"]:
            logo_image = logo_image.crop(logo_image.getbbox())

        if properties["padding"]:
            logo_image = padding(properties, logo_image)

        if properties["antialias"]:
            logo_image = logo_image.resize((round(logo_image.width  / 3),
                                            round(logo_image.height / 3)), Image.ANTIALIAS)

        if properties["background"]:
            logo_image = background(properties, logo_image)

        logo_image.save(os.path.join(out_dir, logo_file), properties["type"])

Since people asked, here is the absolute full code.

最佳答案

已编辑:您正在循环遍历文件,然后对于每个文件,循环遍历 logo_list。然后修改 logo_list 的项目。因此第二个文件将使用 logo_list 及其修改的项目。这意味着每次乘以三,从而产生指数增长。

关于python - 列表迭代器中的项目值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44836975/

相关文章:

python-3.x - 分割掩码的反转像素坐标

python - OpenCV 会覆盖我之前保存的视频。 python 3

python - 使用 pynput 在随机位置自动单击鼠标(对于网页浏览器游戏)

python - django cms 和 zinnia 骨架覆盖

c++ - VK_FILTER_NEAREST 在采样期间不起作用

c# - 将来自网络的图像保存到 Windows Phone 8.1 RT C# 中的已保存图片文件夹

CSS:禁用图像大小调整/'cropping' 图像?

python - 如何将具有嵌套序列的字典展平为具有所有值和键的单个序列?

Python - 在多处理环境中使用 Streamhandler

python - 如何在 sqlalchemy 中为表设置别名?