Skip to content

Instantly share code, notes, and snippets.

@portableant
Created October 12, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save portableant/0ee496233f79f7da5caf705b2c94563b to your computer and use it in GitHub Desktop.
Save portableant/0ee496233f79f7da5caf705b2c94563b to your computer and use it in GitHub Desktop.
Lenborough hoard stitch images together
Install the PIL library on windows at the powershell prompt by typing the command below
pip3 install PIL
1. Now save your images into a folder on your computer and make a note of the path (don't work on your master images, use a copied folder)
2. Save the stitch.py file from here in the same directory
3. Remove all the images with the scale bar included as you don't need these
4. Now edit the stitch.py file and replace line 14 with the path to the directory where you have your lenborough images
5. Now go back to powershell and type:
cd path_to_folder
This could look like:
C:\Users\wscott\lenborough_images_stitch\
6. Now in powershell type
python3 stitch.py
7. This should create the merged images in a folder called new in the folder you have all your images.
from PIL import Image
import os
import re
#Tokeniser function
digits = re.compile(r'(\d+)')
def tokenize(filename):
return tuple(int(token) if match else token
for token, match in
((fragment, digits.search(fragment))
for fragment in digits.split(filename)))
root = '/Users/danielpett/Documents/research/micropasts/lenborough/'
directory = root + ''
extension = '.jpg'
#Are the folders arranged with front image as odd numbers?
frontfirst = True
#Rotation required
reqrot = 0
# A new directory to put merged images into
resdir = directory + '/new/'
#Create directory if not there
if not os.path.exists(resdir):
os.makedirs(resdir)
#Sort the file list
files = [file for file in os.listdir(directory) if file.lower().endswith(extension)]
files.sort(key=tokenize)
#Create the arrays
if frontfirst:
cardfronts = files[::2]
cardbacks = files[1::2]
else:
cardfronts = files[1::2]
cardbacks = files[::2]
#Loop through both arrays
for f, b in zip(cardfronts, cardbacks):
imf = Image.open(os.path.join(directory, f))
imb = Image.open(os.path.join(directory, b))
#rotate the images
imf = imf.rotate(reqrot)
imb = imb.rotate(reqrot)
#Work out maximum width
maxwd = imf.size[0] + imb.size[0]
#Add heights together
maxht = max(imf.size[1],(imb.size[1]))
offset = imb.size[0]
y = 0
#Create new image
imnew = Image.new('RGB', (maxwd, maxht), 'black')
#Paste them back together
imnew.paste(imf, (0,0))
imnew.paste(imb, (offset,0))
#Split file names up
var1, var2 = f.split(".")
var3, var4 = b.split(".")
#Save new image
imnew.save(resdir + var1 + '_' + var3 + '_obv_rev_merged' + extension)
print(var1 + '_' + var3 + '_obv_rev_merged' + extension + ' has been made')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment