Skip to content

Commit

Permalink
Merge pull request #71 from markmac99/AnnotateJpg
Browse files Browse the repository at this point in the history
Optionally, add title to jpegs
  • Loading branch information
dvida authored May 3, 2021
2 parents 07ac287 + ff70c6c commit 97616e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
24 changes: 21 additions & 3 deletions RMS/Routines/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import numpy as np
import scipy.misc

from PIL import Image, ImageFont, ImageDraw
import datetime

# Check which imread funtion to use
try:
imread = scipy.misc.imread
Expand Down Expand Up @@ -48,18 +51,33 @@ def loadImage(img_path, flatten=-1):
return img


def saveImage(img_path, img):
def saveImage(img_path, img, add_timestamp=False):
""" Save image to disk.
Arguments:
img_path: [str] Image path.
img: [ndarray] Image as numpy array.
add_timestamp: [boolean] optionally add a timestamp title to the image
"""

imsave(img_path, img)


if add_timestamp is True:
my_image = Image.open(img_path)
try:
_, height = my_image.size
image_editable = ImageDraw.Draw(my_image)
_, fname = os.path.split(img_path)
splits = fname.split('_')
dtstr = splits[2] + '_' + splits[3] + '.' + splits[4]
imgdt = datetime.datetime.strptime(dtstr, '%Y%m%d_%H%M%S.%f')
title = splits[1] + ' ' + imgdt.strftime('%Y-%m-%d %H:%M:%S UTC')
#fnt = ImageFont.truetype("arial.ttf", 15)
fnt = ImageFont.load_default()
image_editable.text((15,height-20), title, font=fnt, fill=(255))
except:
print('unable to add title')
my_image.save(img_path)


def binImage(img, bin_factor, method='avg'):
Expand Down
18 changes: 11 additions & 7 deletions Utils/BatchFFtoImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from RMS.Formats.FFfile import validFFName
from RMS.Routines.Image import saveImage

def batchFFtoImage(dir_path, fmt):

def batchFFtoImage(dir_path, fmt, add_timestamp=False):
# Go through all files in the given folder
for file_name in os.listdir(dir_path):

Expand All @@ -27,29 +28,32 @@ def batchFFtoImage(dir_path, fmt):
print('Saving: ', img_file_name)

# Save the maxpixel to disk
saveImage(os.path.join(dir_path, img_file_name), ff.maxpixel)
saveImage(os.path.join(dir_path, img_file_name), ff.maxpixel, add_timestamp)


if __name__ == "__main__":

### COMMAND LINE ARGUMENTS
# COMMAND LINE ARGUMENTS

# Init the command line arguments parser
arg_parser = argparse.ArgumentParser(description="Convert all FF files in a folder to images.")

arg_parser.add_argument('dir_path', nargs=1, metavar='DIR_PATH', type=str, \
arg_parser.add_argument('dir_path', nargs=1, metavar='DIR_PATH', type=str,
help='Path to directory with FF files.')

arg_parser.add_argument('file_format', nargs=1, metavar='FILE_FORMAT', type=str, \
arg_parser.add_argument('file_format', nargs=1, metavar='FILE_FORMAT', type=str,
help='File format of the image, e.g. jpg or png.')

arg_parser.add_argument('-t', '--add_timestamp', action="store_true",
help="""Add a title to the image. """)

# Parse the command line arguments
cml_args = arg_parser.parse_args()

#########################


dir_path = cml_args.dir_path[0]
add_timestamp = cml_args.add_timestamp

batchFFtoImage(dir_path, cml_args.file_format[0])

batchFFtoImage(dir_path, cml_args.file_format[0], add_timestamp)

0 comments on commit 97616e8

Please sign in to comment.