0import os
1from pathlib import Path
3from PIL import Image
6class Stich:
7 def __init__(self):
8 self.input_folder = Path('pngs')
9 self.output_fn = 'no_ads'
10 self.width = 160
11 self.height = 50
13 def run(self):
14 images = self.get_images()
15 self.stich(images)
17 def get_images(self):
18 image_files = sorted([f for f in os.listdir(self.input_folder) if f.lower().endswith('.png')])
19 images = [Image.open(os.path.join(self.input_folder, fname)) for fname in image_files]
20 return images
22 def stich(self, images):
23 for img in images:
24 if img.size != (self.width, self.height):
25 raise ValueError(f'Image {img.filename} is not {self.width}x{self.height}px')
27 total_width = self.width * len(images)
28 stitched_image = Image.new('RGB', (total_width, self.height))
30 for i, img in enumerate(images):
31 stitched_image.paste(img, (i * self.width, 0))
33 output_path = Path(self.output_fn + '.jpg')
34 stitched_image.save(output_path, 'JPEG', quality=100)
36 print(f'Saved stitched image to {output_path}')
39if __name__ == '__main__':
40 Stich().run()
index : link-collection
---