You can do this with Pillow:

from PIL import Image
im1 = Image.open("background.jpg")
im2 = Image.open("bird.jpg")

newimg = Image.blend(im1, im2, alpha=0.5)
newimg.save("blended.jpg")

I get this result:

Answer from silver on Stack Overflow
🌐
Educative
educative.io › answers › how-to-superimpose-two-images-using-pil-in-python
How to superimpose two images using PIL in Python
The Image module offers a variety of factory operations, including tools for generating new pictures and loading images from files. Two images can be superimposed on another using the paste() function from the Image module.
Discussions

python - Superimpose scatter plots - Stack Overflow
I am using Python matplotlib. i want to superimpose scatter plots. More on stackoverflow.com
🌐 stackoverflow.com
superimpose two protein by python script
Superimposing two protein structures is a challenging problem. There are some structure alignment programs available; I would not try to write one. Aligning two protein sequences is much easier, and there are biopython functions to do that. For structure alignments and a printed figure, I would look at structure viewing programs. I believe that some of them will superimpose two structures, but you would be doing the structural alignment by eye. More on reddit.com
🌐 r/bioinformatics
5
2
June 23, 2022
superimpose an image over another image with python - Stack Overflow
Is there a built in function that allows an image to be added over another image in python? Thanks a lot More on stackoverflow.com
🌐 stackoverflow.com
Overlay two same sized images in Python - Stack Overflow
I've got two images that are exactly the same dimensions, all I'm trying to do is take one, make it 50% transparent and place it directly on top of the other, like so: import Image background = I... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com › lessons › python-pillow-superimpose-images
Superimposition of Images (Video) – Real Python
Superimposition of Images Using .paste(). Now everything is in place. You can paste the segmented image of the cat into the image of the cloister. Open a new REPL session and start by importing the images that you’ll need. Here you use .paste() to…
Published: August 15, 2023
🌐
AskPython
askpython.com › python-modules › matplotlib › superimpose-scatter-plots-matplotlib
How to Superimpose Scatter Plots Using Matplotlib? - AskPython
July 27, 2023 - Superimposing multiple scatter plots on the same grid using matplotlib is nothing but plotting a few datasets on the graph, which we can say collide with each other at some point.
🌐
Moonbooks
moonbooks.org › Articles › How-to-overlay--superimpose-two-images-using-python-and-pillow-
How to overlay / superimpose two images using python and pillow ?
August 24, 2022 - from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) background.save('how_to_superimpose_two_images_01.png',"PNG")
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › overlay-an-image-on-another-image-in-python
Overlay an image on another image in Python - GeeksforGeeks
January 3, 2021 - Pillow: Python Imaging Library (expansion of PIL) is the de facto image processing package for the Python language.
Top answer
1 of 1
2
The short explanation is you need to really be careful with syntax and build off examples in the documentation and found on the web. For example, already your load command doesn't follow syntax. From the documentation on the load command: · User Comments/Examples · Load xyz.pdb using the PyMOL API: · cmd.load("xyz.pdb") · So you'll note that your current version, pymol.cmd.load(6UFO), doesn't match having quotes, case, extension, etc.. (pymol.cmd part is okay. You'll see the documentation and my examples use a variation that makes for less typing by assigning to cmd. I'll try to use the way you are doing it in my example below so that you get a sense of the variations and how to adapt.) · Full explanation by way of example: · I put together an example using your code at the bottom of a notebook that demonstrates the use of PyMOL's super command first using the supplied demonstration in the documentation and combines it with making a figure. This is a later entry in a series of demonstrations. To best understand some of the basics used in the demonstration, you should work through the first available notebook listed in the series. While it doesn't involve a superposition, it does involve getting structures using PyMOL loading them into PyMOL, and generating images, all on a remote server. And so it gives a good place to build from to understand what is going on in the superposition demonstration. · To get started go here and press launch binder. When the session spins up in your browser, work through the first notebook listed under available notebooks. Then check out the one listed as 'Demo of Superimposing Two Structures via PyMOL Super Command'. (Here is the direct link to the static version of that specific Jupyter notebook for when you simply need to review it for quick reference.)
🌐
Real Python
realpython.com › lessons › python-pillow-segment-superimpose
Segmentation and Superimposition (Video) – Real Python
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas. ... 00:00 Image Segmentation and Superimposition. In this section of the course, you’ll use the JPEG files, cat and monastery, which you can find in the course materials.
Published: August 15, 2023
🌐
Bioinformatics Answers
biostars.org › p › 486865
Python function to superimpose protein on each other
January 27, 2021 - # In this case we use CA atoms whose index is in the specified range list_res_1 = [] list_res_2 = [] ref_atoms = [] sample_atoms = [] # Iterate of all chains in the model in order to find all residues for ref_chain in ref_model: # Iterate of all residues in each model in order to find proper atoms for ref_res in ref_chain: # Check if residue number ( .get_id() ) is in the list if ref_res.get_id()[1] in atoms_to_be_aligned: # Append CA atom to list ref_atoms.append(ref_res['O']) # Do the same for the sample structure for sample_chain in sample_model: for sample_res in sample_chain: if sample_res.get_id()[1] in atoms_to_be_aligned: sample_atoms.append(sample_res['O']) # Now we initiate the superimposer: #super_imposer = Bio.PDB.Superimposer() sup(ref_atoms, sample_atoms) sup(sample_model.get_atoms()) # Print RMSD: print (list_res_1,list_res2) superimpose()
🌐
Ansys Innovation Space
innovationspace.ansys.com › home › how to apply ‘superimpose’ structure using python?
How to apply ‘superimpose’ structure using python? | Ansys Learning Forum
September 13, 2024 - I want to see how the field looks inside the structure, similar to how the movie monitor shows data with the structure outline superimposed.My questions are: Is it possible to impose the structure outline on the field intensity plot using only Python script, without relying on the GUI?
🌐
GitHub
github.com › biopython › biopython › blob › master › Bio › PDB › Superimposer.py
biopython/Bio/PDB/Superimposer.py at master · biopython/biopython
"""Superimpose two structures.""" · import numpy as np · · from Bio.PDB.PDBExceptions import PDBException · from Bio.SVDSuperimposer import SVDSuperimposer · · · class Superimposer: """Rotate/translate one set of atoms on top of another to minimize RMSD.""" ·
Author: biopython
Top answer
1 of 2
2

There might be better ways of applying a colorizing mask to an image, but if you want to do it the way you suggest, then this simple clipping will do what you want:

import numpy as np

image[:, :, 0] = np.clip(image[:, :, 0] + color_delta[0] * (mask[:, :, 0] / 255), 0, 255)
image[:, :, 1] = np.clip(image[:, :, 1] + color_delta[1] * (mask[:, :, 0] / 255), 0, 255)
image[:, :, 2] = np.clip(image[:, :, 2] + color_delta[2] * (mask[:, :, 0] / 255), 0, 255)

The result is:

Another way would be to simply modify the hue/saturation if your goal is to apply a color to a region. For instance:

mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.bool)
mask[100:200, 100:500] = True

image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
image[mask, 0] = 80
image[mask, 1] = 255
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
2 of 2
0

One approach using np.clip & np.einsum -

import numpy as np

# Get clipped values after broadcasted summing of image and color_delta
clipvals = np.clip(image + color_delta,0,255)

# Mask of image elements to be changed
mask1 = mask[:,:,0]>0

# Extract clipped values for TRUE values in mask1, otherwise keep image 
out = np.einsum('ijk,ij->ijk',clipvals,mask1) + np.einsum('ijk,ij->ijk',image,~mask1)

Runtime tests

In [282]: # Setup inputs
     ...: M = 1000; N = 1000
     ...: image = np.random.randint(-255,255,(M,N,3))
     ...: imagecp = image.copy()
     ...: mask = np.random.randint(0,10,(M,N,3))
     ...: color_delta = np.random.randint(-255,255,(3))
     ...: 

In [283]: def clip_einsum(image,color_delta,mask):
     ...:     clipvals = np.clip(imagecp + color_delta,0,255)
     ...:     mask1 = mask[:,:,0]>0
     ...:     return np.einsum('ijk,ij->ijk',clipvals,mask1) +
                           np.einsum('ijk,ij->ijk',image,~mask1)
     ...: 

In [284]: def org_approach(image,color_delta,mask):
     ...:     rows, cols = image.shape[:2]
     ...:     #out = image.copy()
     ...:     for row in range(rows):
     ...:         for col in range(cols):
     ...:             if mask[row, col, 0] > 0:
     ...:                 image[row, col, 0] = min(255, max(0, 
                                 image[row, col, 0] + color_delta[0]))
     ...:                 image[row, col, 1] = min(255, max(0,
                                 image[row, col, 1] + color_delta[1]))
     ...:                 image[row, col, 2] = min(255, max(0,
                                 image[row, col, 2] + color_delta[2]))
     ...:                 

In [285]: %timeit clip_einsum(image,color_delta,mask)
10 loops, best of 3: 147 ms per loop

In [286]: %timeit org_approach(image,color_delta,mask)
1 loops, best of 3: 5.95 s per loop
🌐
University of Warwick
warwick.ac.uk › fac › sci › moac › people › students › peter_cock › python › protein_superposition
Protein Superposition using Biopython
This PDB file contains twenty-one models of a dimer structure (identical chains A and B). Each model is apparently stored at a random orientation (shown below left). The following python script will do twenty pairwise alignments in order to superimpose the later models onto the first model - and output a new PDB with the new co-ordinates (shown below right).
🌐
Bioinformatics Answers
biostars.org › p › 486544
Implementation of python function
import NumPy as np import Bio from Bio.PDB import * def superimpose(pdb_code_1, chain_id_1, list_res_1, pdb_code_2, chain_id2, list_res2): # Select what residues numbers you wish to align # and put them in a list start_id = 1 end_id = 70 atoms_to_be_aligned = range(start_id, end_id + 1) pdb_code_1= "1d3z" pdb_filename_1 = "%s.pdb" % pdb_code_1 pdb_out_filename = "%s_aligned.pdb" % pdb_code_1 pdb_code_2= "1ubq" pdb_filename_2 = "%s.pdb" % pdb_code_2 # Start the parser pdb_parser = Bio.PDB.PDBParser(QUIET = True) # Get the structures ref_structure = pdb_parser.get_structure("reference", pdb_file