| 
									
										
										
										
											2020-05-18 18:34:19 +02:00
										 |  |  | from PIL import Image | 
					
						
							|  |  |  | import numpy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 11:08:31 +01:00
										 |  |  | image1_path = "/home/pi/Desktop/cal.png" | 
					
						
							|  |  |  | image2_path = "/home/pi/Desktop/cal2.png" | 
					
						
							|  |  |  | output_file = "/home/pi/Desktop/merged.png" | 
					
						
							| 
									
										
										
										
											2020-05-18 18:34:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 11:08:31 +01:00
										 |  |  | def merge(image1, image2, out_filename): | 
					
						
							| 
									
										
										
										
											2020-05-18 18:34:19 +02:00
										 |  |  |   """Merge black pixels from image2 into image 1
 | 
					
						
							|  |  |  |   module_name = name of the module generating the image | 
					
						
							|  |  |  |   out_filename = what name to give to the finished file | 
					
						
							|  |  |  |   """
 | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2020-11-11 11:08:31 +01:00
										 |  |  |   im1_name, im2_name = image1_path, image2_path | 
					
						
							| 
									
										
										
										
											2020-05-18 18:34:19 +02:00
										 |  |  |   im1 = Image.open(im1_name).convert('RGBA') | 
					
						
							|  |  |  |   im2 = Image.open(im2_name).convert('RGBA') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def clear_white(img): | 
					
						
							|  |  |  |     """Replace all white pixels from image with transparent pixels
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     x = numpy.asarray(img.convert('RGBA')).copy() | 
					
						
							|  |  |  |     x[:, :, 3] = (255 * (x[:, :, :3] != 255).any(axis=2)).astype(numpy.uint8) | 
					
						
							|  |  |  |     return Image.fromarray(x) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   im2 = clear_white(im2) | 
					
						
							|  |  |  |   im1.paste(im2, (0,0), im2) | 
					
						
							|  |  |  |   im1.save(out_filename+'.png', 'PNG') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 11:08:31 +01:00
										 |  |  | merge(image1_path, image2_path, output_file) | 
					
						
							| 
									
										
										
										
											2020-05-18 18:34:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print('Done') |