Python PIL 尝试在两个图像之间交换方形像素组

问题描述 投票:0回答:1

我已经尝试了很多次这段代码,但找不到错误所在(请参阅下面的代码。)

这个想法是拍摄两张相同大小的照片并交换它们之间的正方形像素组,从而创建两个棋盘。

此处的代码使用 400x400 像素的图像,分别为红色和绿色。

生成的图像 ___OUTPUT_IM_100_POS.png 是应有的样子。然而,___OUTPUT_IM_100_NEG.png 是其原始形式的副本。

我确信这是一个非常新手的错误,但是,请...

from PIL import Image
# The import below is to be able to always have the right line number when printing messages...
from inspect import currentframe, getframeinfo


# The function below is used for debugging. It allows always having the line when the message is written.
def pprint(line_number, message):
    # print(end)
    print('L_' + str(line_number) + ': ' + message)


# declare paths
images_path = r'C:\_D_Drive\_pictures\for_asking'
# Declare input images names
input_images_names = [images_path + r'\4_squares_pos.png', images_path + r'\4_squares_neg.png']
# Declare output images and check_size to put in their name
check_size = 100
width = 400
height = 400
output_images_names = [images_path + r"\___OUTPUT_IM_" + str(check_size) + r"_POS.png",
                      images_path + r"\___OUTPUT_IM_" + str(check_size) + r'_NEG.png']
# create two black images of the same size and save them as output_images_names[0] and [1]
adjustable_woven_images = [Image.new(mode="RGB", size=(width, height), color="black"), Image.new(mode="RGB", size=(width, height), color="black")]
adjustable_woven_images[0].save(output_images_names[0], format="png")
adjustable_woven_images[1].save(output_images_names[1], format="png")

red_400x400 = Image.new(mode="RGB", size=(width * 1, height * 1), color="red")
red_400x400_pixel_map = red_400x400.load()
green_400x400 = Image.new(mode="RGB", size=(width * 1, height * 1), color="green")
green_400x400_pixel_map = green_400x400.load()

# Open the two input images into the output images
output_images = [red_400x400, green_400x400]


# Get pixel maps:
pixel_maps_for_output_images = [output_images[0].load(), output_images[1].load()]
# Declare pixel maps for swap arrays
pixel_maps_for_swap = [pixel_maps_for_output_images[0], pixel_maps_for_output_images[1]]

# Announce start...
pprint(getframeinfo(currentframe()).lineno, f"Opening\n{input_images_names[0]} and\n{input_images_names[1]}...\n")


# Core function swaps squares...
def swap_square(run_number_given, odd_or_even, i_given, j_given):
    global pixel_maps_for_output_images
    global check_size
    global width
    global height
    global pixel_maps_for_swap
    sq_pos_neg_string = ["POS", "NEG"]

    # Make temp squares if asked for to show process...
    make_temp_squares = False
    if make_temp_squares:
        temp_square_image = Image.new(mode="RGB", size=(width, height), color="lightgray")
        temp_map = temp_square_image.load()
        for u in range(width):
            for v in range(height):
                temp_map[u, v] = pixel_maps_for_output_images[0][u, v]
                temp_map[u, v] = (255, 0, 255)
        for u in range(check_size):
            for v in range(check_size):
                temp_map[u + i_given, v + j_given] = pixel_maps_for_swap[0][u + i_given, v + j_given]
        temp_square_image.save(
            images_path + r"\_temp_square_" + str(run_number_given) + "_" + str(check_size) + "_" + sq_pos_neg_string[odd_or_even] + "_" + str(i_given) + "_" + str(j_given) + ".png",
            format="png")

    for k in range(check_size):
        for l in range(check_size):
            if k + i_given < width and l + j_given < height:
                if odd_or_even == 1:
                    pixel_maps_for_swap[0][k + i_given * 1, l + j_given * 1] = pixel_maps_for_output_images[0][k + i_given, l + j_given]
                    pixel_maps_for_swap[1][k + i_given * 1, l + j_given * 1] = pixel_maps_for_output_images[1][k + i_given, l + j_given]
                    pixel_maps_for_output_images[0][k + i_given, l + j_given] = pixel_maps_for_swap[1][k + i_given * 1, l + j_given * 1]
                    pixel_maps_for_output_images[1][k + i_given, l + j_given] = pixel_maps_for_swap[0][k + i_given, l + j_given]
    pprint(getframeinfo(currentframe()).lineno, f"odd_or_even: {odd_or_even} i:j {i_given}:{j_given} Saved {images_path}\_temp_square_{check_size}_{sq_pos_neg_string[odd_or_even]}_{i_given}_{j_given}.png")
    output_images[0].save(images_path + "\\__run#" + str(run_number_given) + "_" + sq_pos_neg_string[odd_or_even] + "_oe_0.png", format="png")
    output_images[1].save(images_path + "\\__run#" + str(run_number_given) + "_" + sq_pos_neg_string[odd_or_even] + "_oe_1.png", format="png")


run_number = 0
for j in range(0, height, check_size):
    if height - j < check_size:
        pprint(getframeinfo(currentframe()).lineno, f"WATCH IT: I have a VERTICAL edge here, watch it...")
    for i in range(0, width, check_size):
        if width - i < check_size:
            pprint(getframeinfo(currentframe()).lineno, f"WATCH IT: I have a HORIZONTAL edge here, watch it...")
        odd_even = ((int(i / check_size) % check_size) % 2 + (int(j / check_size) % check_size) % 2) % 2
        run_number += 1
        swap_square(run_number, odd_even, i, j)

output_images[0].save(output_images_names[0], format="png")
output_images[1].save(output_images_names[1], format="png")
python pycharm python-imaging-library
1个回答
0
投票

我将 this 答案与 this 答案合并,得出这个:

#!/usr/bin/env python3

from PIL import Image, ImageOps

# Size of finished images
size = (500,500)

# Create new black image of chess board to use as mask
w, h = 8, 8
mask = Image.new("L", (w,h))
pixels = mask.load()

# Make pixels white where (row+col) is odd
for i in range(w):
    for j in range(h):
        if (i+j)%2:
            pixels[i,j] = 255

# Scale up to 500x500
mask = mask.resize(size, Image.NEAREST)
mask.save('DEBUG-mask.png')

# Create two images to merge "chunkily"
lime = Image.new('RGB', size, 'lime')
blue = Image.new('RGB', size, 'blue')

# Paste blue onto lime with first mask
res1 = lime.copy()
res1.paste(blue, mask)
res1.save('res1.png')

# Paste blue onto lime with inverted mask
res2 = lime.copy()
res2.paste(blue, ImageOps.invert(mask))
res2.save('res2.png')

这使得这两个:

© www.soinside.com 2019 - 2024. All rights reserved.