Skip to content

Commit

Permalink
erjojju
Browse files Browse the repository at this point in the history
  • Loading branch information
tiritibambix committed Jan 12, 2025
1 parent beb9a9c commit f6b9d85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 48 deletions.
69 changes: 22 additions & 47 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,55 +96,30 @@ def allowed_file(filename):
return '.' in filename

def get_image_dimensions(filepath):
"""Get image dimensions using appropriate tool based on file type."""
"""Get image dimensions using ImageMagick."""
try:
if filepath.lower().endswith('.arw'):
app.logger.info(f"Getting dimensions for ARW file: {filepath}")
# Utiliser exiftool pour obtenir les dimensions de l'aperçu JPEG
cmd = ['exiftool', '-s', '-s', '-s', '-PreviewImageLength', '-PreviewImageWidth', filepath]
app.logger.info(f"Running exiftool command: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True)

if result.returncode == 0 and result.stdout.strip():
app.logger.info(f"Exiftool output: {result.stdout}")
# Essayer de parser les dimensions
dimensions = result.stdout.strip().split('\n')
if len(dimensions) == 2:
try:
height = int(dimensions[0])
width = int(dimensions[1])
app.logger.info(f"Successfully parsed dimensions: {width}x{height}")
return width, height
except ValueError:
app.logger.warning("Could not parse dimensions from exiftool output")
pass
else:
app.logger.warning(f"Exiftool failed or no output: {result.stderr}")

# Si on n'a pas pu obtenir les dimensions de l'aperçu, utiliser les dimensions connues
app.logger.info("Using known dimensions for Sony A7 IV")
return 7008, 4672 # Dimensions connues pour Sony A7 IV
app.logger.info(f"Getting dimensions for non-ARW file: {filepath}")
command = ['magick', 'identify', filepath]
app.logger.info(f"Running ImageMagick command: {' '.join(command)}")
result = subprocess.run(command, capture_output=True, text=True, check=True)

# Parse dimensions from output
output = result.stdout.strip()
match = re.search(r'\s(\d+)x(\d+)\s', output)
if match:
width, height = map(int, match.groups())
return width, height
else:
app.logger.info(f"Getting dimensions for non-ARW file: {filepath}")
cmd = ['convert', 'identify', filepath]
app.logger.info(f"Running ImageMagick command: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise Exception(f"Error getting image dimensions: {result.stderr}")
raise ValueError(f"Could not parse dimensions from output: {output}")

app.logger.info(f"ImageMagick output: {result.stdout}")
# Parse the output to get dimensions
match = re.search(r'\s(\d+)x(\d+)\s', result.stdout)
if match:
width = int(match.group(1))
height = int(match.group(2))
app.logger.info(f"Successfully parsed dimensions: {width}x{height}")
return width, height
else:
raise Exception("Could not parse image dimensions")
except subprocess.CalledProcessError as e:
error_msg = f"Error getting image dimensions: {e.stderr}"
app.logger.error(error_msg)
raise ValueError(error_msg)
except Exception as e:
app.logger.error(f"Error getting image dimensions: {str(e)}")
return None, None
error_msg = f"Error getting image dimensions: {str(e)}"
app.logger.error(error_msg)
raise ValueError(error_msg)

def get_available_formats():
"""Get all formats supported by ImageMagick."""
Expand Down Expand Up @@ -311,7 +286,7 @@ def build_imagemagick_command(filepath, output_path, width, height, percentage,
raise Exception("No preview image found in RAW file")

# Commande ImageMagick pour redimensionner le JPEG extrait
magick_cmd = ['convert', temp_jpeg]
magick_cmd = ['magick', temp_jpeg]

if width.isdigit() and height.isdigit():
resize_value = f"{width}x{height}" if keep_ratio else f"{width}x{height}!"
Expand All @@ -331,7 +306,7 @@ def build_imagemagick_command(filepath, output_path, width, height, percentage,
else:
app.logger.info(f"Processing non-ARW file: {filepath}")
# Pour les autres formats, utiliser directement ImageMagick
command = ['convert', filepath]
command = ['magick', filepath]

if width.isdigit() and height.isdigit():
resize_value = f"{width}x{height}" if keep_ratio else f"{width}x{height}!"
Expand Down
2 changes: 1 addition & 1 deletion templates/resize.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
<body>
<div class="container">
<h4>{{ _('Resize Image: %(filename)s', filename=filename) }}</h4>
<form action="{{ url_for_with_lang('resize', filename=filename) }}" method="post">
<form action="{{ url_for_with_lang('resize_image', filename=filename) }}" method="post">
<fieldset>
<legend>{{ _('Resize by Pixels') }}</legend>
<div class="form-group">
Expand Down

0 comments on commit f6b9d85

Please sign in to comment.