import os from pathlib import Path from PIL import Image class Stich: def __init__(self): self.input_folder = Path('pngs') self.output_fn = 'no_ads' self.width = 160 self.height = 50 def run(self): images = self.get_images() self.stich(images) def get_images(self): image_files = sorted([f for f in os.listdir(self.input_folder) if f.lower().endswith('.png')]) images = [Image.open(os.path.join(self.input_folder, fname)) for fname in image_files] return images def stich(self, images): for img in images: if img.size != (self.width, self.height): raise ValueError(f'Image {img.filename} is not {self.width}x{self.height}px') total_width = self.width * len(images) stitched_image = Image.new('RGB', (total_width, self.height)) for i, img in enumerate(images): stitched_image.paste(img, (i * self.width, 0)) output_path = Path(self.output_fn + '.jpg') stitched_image.save(output_path, 'JPEG', quality=100) print(f'Saved stitched image to {output_path}') if __name__ == '__main__': Stich().run()