Fix goalimage scale

This commit is contained in:
2024-11-10 01:35:44 +09:00
parent 02f483aa68
commit efbce943b6
3 changed files with 19 additions and 16 deletions

View File

@ -357,9 +357,24 @@ class SumasenExcel:
elif re.search(r'\.(jpg|jpeg|png|gif|bmp|mpo)$', value, re.I): elif re.search(r'\.(jpg|jpeg|png|gif|bmp|mpo)$', value, re.I):
return os.path.join("/app/media/compressed", value) return os.path.join("/app/media/compressed", value)
return None return None
def get_image_scale(value):
if value.startswith('https://'):
scale=1.0
return scale
elif value.startswith('checkin/'):
scale=1.0
return scale
elif value.startswith('goals/'):
scale=4.0
return scale
else:
scale=1.0
return scale
# 画像パスを取得 # 画像パスを取得
image_path = get_image_path(field_value) image_path = get_image_path(field_value)
scale = get_image_scale(field_value)
if image_path and os.path.exists(image_path): if image_path and os.path.exists(image_path):
try: try:
@ -379,7 +394,6 @@ class SumasenExcel:
# ワークシートを取得 # ワークシートを取得
worksheet = cell.parent worksheet = cell.parent
logging.info('step-1')
try: try:
# 列の幅を取得(文字単位からピクセルに変換) # 列の幅を取得(文字単位からピクセルに変換)
column_letter = get_column_letter(cell.column) column_letter = get_column_letter(cell.column)
@ -388,7 +402,6 @@ class SumasenExcel:
except Exception: except Exception:
cell_width = 100 # デフォルト値 cell_width = 100 # デフォルト値
logging.info('step-2')
try: try:
# 行の高さを取得(ポイント単位からピクセルに変換) # 行の高さを取得(ポイント単位からピクセルに変換)
row_height = worksheet.row_dimensions[cell.row].height row_height = worksheet.row_dimensions[cell.row].height
@ -396,7 +409,6 @@ class SumasenExcel:
except Exception: except Exception:
cell_height = 20 # デフォルト値 cell_height = 20 # デフォルト値
logging.info('step-3')
# マージンの設定(ピクセル単位) # マージンの設定(ピクセル単位)
margin_horizontal = 4 # 左右のマージン margin_horizontal = 4 # 左右のマージン
margin_vertical = 2 # 上下のマージン margin_vertical = 2 # 上下のマージン
@ -405,7 +417,6 @@ class SumasenExcel:
effective_cell_width = cell_width - (margin_horizontal * 2) effective_cell_width = cell_width - (margin_horizontal * 2)
effective_cell_height = cell_height - (margin_vertical * 2) effective_cell_height = cell_height - (margin_vertical * 2)
logging.info('step-4')
# 最小サイズを設定(マージンを考慮) # 最小サイズを設定(マージンを考慮)
effective_cell_width = max(effective_cell_width, 92) # 100 - (4 * 2) effective_cell_width = max(effective_cell_width, 92) # 100 - (4 * 2)
effective_cell_height = max(effective_cell_height, 16) # 20 - (2 * 2) effective_cell_height = max(effective_cell_height, 16) # 20 - (2 * 2)
@ -416,23 +427,21 @@ class SumasenExcel:
effective_cell_width = min(effective_cell_width, max_width) effective_cell_width = min(effective_cell_width, max_width)
effective_cell_height = min(effective_cell_height, max_height) effective_cell_height = min(effective_cell_height, max_height)
logging.info('step-5')
# アスペクト比を保持しながらリサイズ # アスペクト比を保持しながらリサイズ
img_width, img_height = img.size img_width, img_height = img.size
img_aspect = img_width / img_height img_aspect = img_width / img_height
cell_aspect = effective_cell_width / effective_cell_height cell_aspect = effective_cell_width / effective_cell_height
if img_aspect > cell_aspect: if img_aspect > cell_aspect:
width = effective_cell_width width = effective_cell_width*scale
height = int(width / img_aspect) height = int(width / img_aspect)*scale
else: else:
height = effective_cell_height height = effective_cell_height*scale
width = int(height * img_aspect) width = int(height * img_aspect)*scale
# 画像処理部分の修正 # 画像処理部分の修正
#from openpyxl.utils.units import pixels_to_EMU #from openpyxl.utils.units import pixels_to_EMU
logging.info('step-6')
# 画像をリサイズ # 画像をリサイズ
img_resized = img.resize((width, height), Image.BICUBIC) img_resized = img.resize((width, height), Image.BICUBIC)
@ -441,8 +450,6 @@ class SumasenExcel:
img_resized.save(img_byte_arr, format='JPEG', quality=85, optimize=True) img_resized.save(img_byte_arr, format='JPEG', quality=85, optimize=True)
img_byte_arr.seek(0) img_byte_arr.seek(0)
logging.info('step-7')
# OpenPyXLのImageオブジェクトを作成 # OpenPyXLのImageオブジェクトを作成
excel_image = XLImage(img_byte_arr) excel_image = XLImage(img_byte_arr)
@ -454,7 +461,6 @@ class SumasenExcel:
from openpyxl.drawing.spreadsheet_drawing import OneCellAnchor, AnchorMarker from openpyxl.drawing.spreadsheet_drawing import OneCellAnchor, AnchorMarker
from openpyxl.drawing.xdr import XDRPositiveSize2D from openpyxl.drawing.xdr import XDRPositiveSize2D
logging.info('step-8')
marker = AnchorMarker( marker = AnchorMarker(
col=cell.column - 1, col=cell.column - 1,
colOff=pixels_to_EMU(margin_horizontal), colOff=pixels_to_EMU(margin_horizontal),
@ -471,13 +477,10 @@ class SumasenExcel:
anchor = OneCellAnchor(_from=marker, ext=size) anchor = OneCellAnchor(_from=marker, ext=size)
excel_image.anchor = anchor excel_image.anchor = anchor
logging.info('step-9')
# 画像をワークシートに追加 # 画像をワークシートに追加
worksheet.add_image(excel_image) worksheet.add_image(excel_image)
#cell.parent.add_image(excel_image) #cell.parent.add_image(excel_image)
logging.info('step-A')
# メモリ解放 # メモリ解放
#img_byte_arr.close() #img_byte_arr.close()

Binary file not shown.

BIN
rog/.DS_Store vendored

Binary file not shown.