Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Merge Images Vertically #597

Merged
merged 3 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Scripts/Miscellaneous/Merge_Images_Vertically/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Merge Images Vertically
Merge images inside a subfolder vertically using Pillow.
Useful for comics and webtoons.

### Pre-requisites:
You will need to install python on your machine. You can download python from the python.org and install it.
Run `pip install -r requirements.txt` to install modules used.

### How to run the script
Run `python merge_images_vertically.py`.
Type in the subfolder name in which the images are saved.

### Screenshot of the console interaction
![Screenshot](usage.png)

## *Author Name*

[João Camelo](https://github.com/jrcamelo)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import numpy as np
import PIL
from PIL import Image
import os
from os import listdir
from os.path import isfile, join

def merge_pics_vertically(images_list, name):
# Opens all files and stores them in a list
imgs = [Image.open(i) for i in images_list]
min_img_width = min(i.width for i in imgs)

# Sums up the total height
total_height = 0
for i, img in enumerate(imgs):
if img.width > min_img_width:
imgs[i] = img.resize((min_img_width, int(img.height / img.width * min_img_width)), Image.ANTIALIAS)
total_height += imgs[i].height

# Pastes all of them together
img_merge = Image.new(imgs[0].mode, (min_img_width, total_height))
y = 0
for img in imgs:
img_merge.paste(img, (0, y))
y += img.height

# Then saves the final image
img_merge.save(name + '.jpg')

def get_files(directory, ext = None):
if ext is None:
ext = [".jpg"]
files = []
# Scans the folder and gets all files with the extension
for f in os.scandir(directory):
if f.is_file():
if os.path.splitext(f.name)[1].lower() in ext:
files.append(f.path)
return files

name = input("Sub-folder with images to be merged: ")
path = os.getcwd() + "/" + name
pictures = get_files(path)
merge_pics_vertically(pictures, path)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.19.1
Pillow==7.2.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.