Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # uf_pic_join.py
- #
- # Author:
- # Carlos Lamas
- # Date:
- # 2021-04-14
- # Version:
- # 1.1
- # Copyright:
- # This code is licensed as public domain and can be used
- # without any restriction
- # Description:
- # This script allows to join a set of splitted tiles of an
- # Ultra Fractal rendered image in a single image. Ultra
- # Fractal images size are limited to 2GiB so splitted images
- # may be required to render bigger images
- # Requirements
- # * libvips instaled and configured in system path
- # * pyvips binding installed
- # * python is required to run the script
- # Command syntax:
- # python uf_pic_join.py <corner_pic_file> <destination_file>
- # Command example:
- # python uf_pic_join.py 'my fractal E4.png' 'full fractal.png'
- # Notes:
- # * <corner_pic_file> is the name of the bottom right corner
- # tile (something like 'myfractal E4.png')
- # * <destination_file> is the name of the file to contain the
- # mosaic with all the picture parts joined. It has not to
- # be the same file format as the source image parts
- # * Tested only in Windows OS and Python 3. Some tweaking can
- # be necessary to run it in another OS
- # * Using older libvips version 8.9.2 due to some issues
- # related to pyvips with current versions 8.10.x
- #
- import os, os.path
- import sys, time
- import logging
- import pyvips
- # Set the following constants to configure the command execution
- LOGGING_LEVEL = logging.WARNING
- PIVIPS_CACHE_TRACE = False
- PIVIPS_REPORT_RAM_LEAKED = True
- # Initialize eval handler last_time "static" variable
- def preeval_handler(image, progress):
- eval_handler.last_time = 0
- preeval_handler.start_time = time.time()
- print('Joining images...')
- print('Percent complete: {0}%'.format(progress.percent), end = "")
- # Eval handler used to track the progress of the operation
- # Print the percent of operation done (only once every second)
- def eval_handler(image, progress):
- current_time = progress.run # time counter
- if current_time - eval_handler.last_time >= 1:
- print('\rPercent complete: {0}%'.format(progress.percent), end = "")
- eval_handler.last_time = current_time
- # Force printing the percent at the end
- def posteval_handler(image, progress):
- print('\rPercent complete: {0}%'.format(progress.percent))
- print('Total time spent: {0:.2f}s'.format(time.time() - preeval_handler.start_time))
- # Print the shell command syntax
- def print_command_syntax():
- print("Command syntax: python {0} <last_mosaic_file> <destination_file>".format(sys.argv[0]))
- def main():
- # Check 2 arguments are passed to the command
- if len(sys.argv) != 3:
- print("Invalid number of arguments ({0}).".format(len(sys.argv) - 1))
- print_command_syntax()
- return -1
- # Source files (2nd command argument)
- src_file_path, src_file_name = os.path.split(os.path.normpath(sys.argv[1].strip()))
- src_file_name, src_file_ext = os.path.splitext(src_file_name)
- # Destination file (3rd command argument)
- dst_file_path, dst_file_name = os.path.split(os.path.normpath(sys.argv[2].strip()))
- dst_file_name, dst_file_ext = os.path.splitext(dst_file_name)
- # Check source file names
- if len(src_file_name) <= 3 or src_file_name[-3] != ' ':
- print("Invalid source file: '{0}'".format(src_file_name))
- print_command_syntax()
- return -1
- # The row is the last character and the column is the second last
- rows = src_file_name[-1] # digit 0..9
- cols = src_file_name[-2].upper() # character A..I
- rows = "123456789".find(rows) + 1 # 1..9
- if rows == 0:
- print("Invalid source file name. Invalid row coordinate")
- return -1
- cols= "ABCDEFGHI".find(cols) + 1 # 1..9
- if cols == 0:
- print("Invalid source file name. Invalid column coordinate")
- return -1
- # pyvips configuration
- pyvips.cache_set_trace(PIVIPS_CACHE_TRACE)
- pyvips.base.leak_set(PIVIPS_REPORT_RAM_LEAKED) # show memory used
- # Create image list with sorted source files
- image_list = []
- src_file_name = src_file_name[:-2] # remove coordinates to get the base name
- for r in range(rows):
- for c in range(cols):
- suffix = chr(ord('A') + c) + str(r + 1)
- file_name = os.path.join(src_file_path, src_file_name + suffix + src_file_ext)
- image = pyvips.Image.new_from_file(file_name, access='sequential')
- image_list.append(image)
- # Abort when destination file already exists
- dst_file = os.path.join(dst_file_path, dst_file_name + dst_file_ext)
- if os.path.exists(dst_file):
- print('Invalid destination file. A file or folder with that name already exists.')
- return -1
- dst_image = pyvips.Image.arrayjoin(image_list, across = cols)
- # Configure tracking handlers
- dst_image.set_progress(True)
- dst_image.signal_connect('preeval', preeval_handler)
- dst_image.signal_connect('eval', eval_handler)
- dst_image.signal_connect('posteval', posteval_handler)
- dst_image.write_to_file(dst_file)
- return 0
- # Run main()
- if __name__ == "__main__":
- logging.basicConfig(level = LOGGING_LEVEL)
- result = main()
- exit(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement