From 8066028cc9c3a7e2775415738488e80182cac6c3 Mon Sep 17 00:00:00 2001 From: Sandip Samal Date: Thu, 29 Aug 2024 13:03:04 -0400 Subject: [PATCH] maintain resolution while saving op image --- markimg/markimg.py | 42 ++++++++++++++++++++++++++++++------------ setup.py | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/markimg/markimg.py b/markimg/markimg.py index 0d2fdc9..59fdb11 100644 --- a/markimg/markimg.py +++ b/markimg/markimg.py @@ -13,8 +13,8 @@ import math import os import sys -from PIL import Image import cv2 +from PIL import Image import matplotlib import matplotlib.pyplot as plt from chrisapp.base import ChrisApp @@ -423,14 +423,15 @@ def run(self, options): LOG(f"Reading input image from {file_path[0]}") image = cv2.imread(file_path[0]) + #image = Image.open(file_path[0]) plt.style.use('dark_background') plt.axis('off') - max_y, max_x, max_z = image.shape + max_y, max_x, RGB = image.shape + #max_x, max_y = image.size plt.figure(figsize=(max_x / 100, max_y / 100)) - plt.imshow(image, aspect='auto') - + plt.imshow(image) img_XY_plane: ImageCanvas = ImageCanvas(max_y, max_x) height = data[row]["origHeight"] ht_scale = height / max_x @@ -610,10 +611,6 @@ def run(self, options): offset_x = int(offset[1]) x_pos, y_pos = img_XY_plane.add_offset(-offset_x, -offset_y) - # y_pos = y_pos - line_gap - - # plt.text(x_pos, y_pos, options.addText, color=options.addTextColor, fontsize=options.addTextSize, - # rotation=rotation) # Clean up all matplotlib stuff and save as PNG plt.tick_params(left=False, right=False, labelleft=False, @@ -621,12 +618,33 @@ def run(self, options): plt.savefig(os.path.join("/tmp", row + "img.jpg"), bbox_inches='tight', pad_inches=0.0) plt.clf() - png = cv2.imread(os.path.join("/tmp", row + "img.jpg")) + # Open an existing image + tmpimg = Image.open(os.path.join("/tmp", row + "img.jpg")) + x,y = tmpimg.size + # Calculate the aspect ratio + aspect_ratio = max_x /x + + # Define the target width + target_width = int(x * aspect_ratio) + target_height = int(y * aspect_ratio) + + # Resize the image + resized_image = tmpimg.resize((target_width, target_height)) + + # Rotate the image by 90 degrees + rotated_image = resized_image.rotate(-90) - inverted_png = cv2.rotate(png, cv2.ROTATE_90_CLOCKWISE) + # Save the resized image + rotated_image.save(os.path.join(options.outputdir, row + f".{options.outputImageExtension}")) LOG(f"Input image dimensions {image.shape}") - LOG(f"Output image dimensions {inverted_png.shape}") - cv2.imwrite(os.path.join(options.outputdir, row + f".{options.outputImageExtension}"), inverted_png) + LOG(f"Output image dimensions {rotated_image.size}") + + # png = cv2.imread(os.path.join("/tmp", row + "img.jpg")) + # + # inverted_png = cv2.rotate(png, cv2.ROTATE_90_CLOCKWISE) + # LOG(f"Input image dimensions {image.shape}") + # LOG(f"Output image dimensions {inverted_png.shape}") + # cv2.imwrite(os.path.join(options.outputdir, row + f".{options.outputImageExtension}"), inverted_png) d_json[row] = {'info': d_info, 'femur': d_femur, 'tibia': d_tibia, 'total': d_total, 'pixel_distance': d_pixel, 'details': details} diff --git a/setup.py b/setup.py index e1255c1..6a51c20 100755 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='markimg', - version='1.5.5', + version='1.5.6', description='An app to mark landmark points and lines on an input image', long_description=readme, author='FNNDSC',