Python image combiner reading writing I am writing a simple script that will combine two images at a pixel level, selecting every other pixel from one image, and the other pixels from a second image.

from PIL import Image
import struct

print("Photo 1: ", end="")
photo1 = Image.open(input())  # First image, 1920 by 1080
photo1 = photo1.convert("RGB")

print("Photo 2: ", end="")
photo2 = Image.open(input()) # Second image, 1920 x 1080
photo2 = photo2.convert("RGB")

width = photo1.size[0]  # define W and H
height = photo1.size[1]

print("Output file: ", end="")
output = input()
data = str.encode("")
for y in range(0, height):  # counts to 1079
    row = ""
    for x in range(0, width):# counts to 1920
        if ((y + x) % 2) == 0: # makes every other pixel and alternates
                               # on each line, making a checker board pattern
            RGB = photo1.getpixel((x, y))
            print("Even", y) # how far am I?
        else:
            RGB = photo2.getpixel((x, y))
            print("Odd", y) # how far aim I?
        R, G, B = RGB  # now you can use the RGB value
        data += struct.pack("B", R) + struct.pack("B", G) + struct.pack("B",B)
                 # store RGB values in byte format in variable "data"

new = Image.frombytes("RGB", (1920, 1080), data)# change value if not 1920 x 1080
new.save(output, "PNG") # make new image


Sorry if the copy/paste ing messes with python indentation.
This script take a good 10 minutes to run on my laptop


Intel Core i5
8 GB ram
Running linux (Fedora 30)
Is this just going to take 10 minutes to grab all the individual pixel values, or is there any thing that is making this code inefficient?


Also, I am using python3.7 and 3 at the moment, but I also can use 2.7 if needed.

These are the two photos I am using:
Tuple unpacking

width = photo1.size[0]  # define W and H
height = photo1.size[1]


can be

width, height = photo1.size


Use input properly

Don't print here:

print("Output file: ", end="")
output = input()


Instead,

output = input('Output file: ')


Range default start

Don't include the 0 in these calls:

for y in range(0, height):  # counts to 1079
    for x in range(0, width):# counts to 1920


Only call pack once

    data += struct.pack("B", R) + struct.pack("B", G) + struct.pack("B",B)


should become, I think,

data += struct.pack('BBB', R, G, B)