101 lines
3.8 KiB
Python
Executable File
101 lines
3.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding:utf-8 -*-
|
|
|
|
import sys
|
|
import subprocess
|
|
|
|
archIn = sys.argv[1]
|
|
lado = sys.argv[2]
|
|
tamFin = sys.argv[3]
|
|
res = sys.argv[4]
|
|
|
|
def AsignaShellOut(comando):
|
|
"""Ejecuta una cadena de caracteres como comando de Shell.
|
|
La salida se puede asignar a una variable"""
|
|
proc = subprocess.Popen(comando, shell = True, stdout = subprocess.PIPE)
|
|
salida = proc.stdout.read()
|
|
salida = salida.decode('ASCII')
|
|
salida = salida.strip()
|
|
return(salida)
|
|
|
|
# Tipo de imagen
|
|
verTipoImg = "identify " + archIn + " | awk '{print $2}'"
|
|
tipoIm = AsignaShellOut(verTipoImg)
|
|
|
|
# Nombre base de la imagen de entrada
|
|
if (tipoIm == 'PNG'):
|
|
nombreBaseIn = archIn.replace('.png', '')
|
|
elif (tipoIm == 'JPEG'):
|
|
nombreBaseIn = archIn.replace('.jpg', '')
|
|
|
|
# Tamanho de la imagen original en pixeles
|
|
WImgIn = "identify " + archIn + " | awk '{print $3}' | sed 's/x/ /' | awk '{print $1}'"
|
|
anchoImgIn = AsignaShellOut(WImgIn)
|
|
HImgIn = "identify " + archIn + " | awk '{print $3}' | sed 's/x/ /' | awk '{print $2}'"
|
|
altoImgIn = AsignaShellOut(HImgIn)
|
|
|
|
# Resolución de la imagen original en DPI
|
|
xImgIn = "identify -format '%x' " + archIn
|
|
resxImgIn = AsignaShellOut(xImgIn)
|
|
yImgIn = "identify -format '%y' " + archIn
|
|
resyImgIn = AsignaShellOut(yImgIn)
|
|
|
|
# Tamanho de la imagen original en centímetros
|
|
anchoImgInCm = float(anchoImgIn) * 2.54 / float(resxImgIn)
|
|
altoImgInCm = float(altoImgIn) * 2.54 / float(resyImgIn)
|
|
|
|
# Tamanho de la imagen original en cemtímetros si tuviera la resolución de la imagen final
|
|
anchoImgInCmRes = float(anchoImgIn) * 2.54 / float(res)
|
|
altoImgInCmRes = float(altoImgIn) * 2.54 / float(res)
|
|
|
|
# Cálculo de la escala y tamaño de la imagen final en pixeles
|
|
if (lado == 'H'):
|
|
altoImgOut = int(float(tamFin) * float(res) / 2.54)
|
|
escala = float(altoImgOut) / float(altoImgIn)
|
|
anchoImgOut = int(escala * int(anchoImgIn))
|
|
if (lado == 'W'):
|
|
anchoImgOut = int(float(tamFin) * float(res) / 2.54)
|
|
escala = float(anchoImgOut) / float(anchoImgIn)
|
|
altoImgOut = int(escala * int(altoImgIn))
|
|
|
|
# Tamanho de la imagen final en centímetros
|
|
anchoImgOutCm = anchoImgOut / float(res) * 2.54
|
|
altoImgOutCm = altoImgOut / float(res) * 2.54
|
|
|
|
# Nombre de la imagen de salida
|
|
ext = '-modificada'
|
|
nombreBaseOut = nombreBaseIn + ext
|
|
if (tipoIm == 'PNG'):
|
|
archOut = nombreBaseOut + '.png'
|
|
elif (tipoIm == 'JPEG'):
|
|
archOut = nombreBaseOut + '.jpg'
|
|
|
|
# Proceso de conversión y generación de la imagen final
|
|
resCm = round(float(res) / 2.54, 2)
|
|
convierte = 'convert ' + archIn \
|
|
+ ' -resize ' + str(anchoImgOut) + 'x' + str(altoImgOut) \
|
|
+ ' -density ' + str(resCm) + 'x' + str(resCm) \
|
|
+ ' -units pixelsPerCentimeter ' \
|
|
+ archOut
|
|
proceso = subprocess.Popen(convierte, shell = True, stdout = subprocess.PIPE)
|
|
|
|
# Impresión de la información
|
|
print('****** Imagen original ******')
|
|
print(' Nombre: {0}'.format(archIn))
|
|
print(' Tamaño digital (px): {0}x{1}'.format(anchoImgIn, altoImgIn))
|
|
print(' Resolución (dpi): {0}x{1}'.format(resxImgIn, resyImgIn))
|
|
print(' Tamaño físico (cm): {0:.2f}x{1:.2f}'.format(anchoImgInCm, altoImgInCm))
|
|
if not (resxImgIn == resyImgIn):
|
|
print("WARNING: La imagen no tiene la misma resolución en X y en Y.")
|
|
print(' Si la imagen original tuviera una resolución de {0} dpi (como la que se busca)'.format(res))
|
|
print(' su tamaño físico en cm sería de: {0:.2f}x{1:.2f}'.format(anchoImgInCmRes, altoImgInCmRes))
|
|
|
|
print()
|
|
print('****** Imagen final ******')
|
|
print(' Nombre: {0}'.format(archOut))
|
|
print(' Tamaño digital (px): {0}x{1}'.format(anchoImgOut, altoImgOut))
|
|
print(' Resolución (dpi): {0}x{1}'.format(res, res))
|
|
print(' Tamaño físico (cm): {0:.2f}x{1:.2f}'.format(anchoImgOutCm, altoImgOutCm))
|
|
|
|
print('\nLa imagen final está al {0:.2f} % del tamaño de la original.'.format(escala * 100.0))
|