Please review and help me write better codes.
Combining Pictures We can use a conditional to copy just the non-white pixels from one picture to another picture. We can take this tiny image of the women and put her by the Eiffel tower in Paris, France. I have modified the code so that the code automatically swaps the image1 and image2; hence, always using the larger image as the canvas and the smaller picture as the insert.
from PIL import Image image1 = Image.open("lady_tiny.jpg") image2 = Image.open("eiffel.jpg") width = min(image1.size, image2.size) # (x) length = min(image1.size, image2.size) # (y) # check if smaller image then use it as insert if width == image2.size and length == image2.size: canvas = image1.load() # larger pic insert = image2.load() # smaller pic else: insert = image1.load() canvas = image2.load() for col in range(width): for row in range(length): r, g, b = insert[col, row] # read each pixel of insert(small) # Check if the pixel isn't white if r < 250 and g< 250 and b< 250: # Copy the color of insert (smaller) onto the canvas(larger) canvas[col, row + 130] = r, g, b if ( width == image2.size and length == image2.size ): # if image2 is insert then image 1 is canvas image1.show() else: image2.show()