PDF-resizer / app.py
soiz1's picture
Update app.py
093fe97 verified
raw
history blame
2.6 kB
import gradio as gr
import tempfile
from pdf2image import convert_from_path
import fitz # PyMuPDF
def analyze_pdf(pdf_file):
results = []
overall_rating = "OK"
with tempfile.TemporaryDirectory() as tmpdir:
# pdf2imageで画像を取得
try:
images = convert_from_path(pdf_file.name, dpi=300, output_folder=tmpdir)
except Exception as e:
return f"PDF変換エラー: {str(e)}", ""
# 画像サイズ確認
for i, img in enumerate(images):
width, height = img.size
page_rating = "OK"
if width < 1000 or height < 1000:
page_rating = "非推奨(解像度低)"
elif width < 1500 or height < 1500:
page_rating = "注意(やや低め)"
results.append(
f"ページ{i+1}: {width}x{height}px → {page_rating}"
)
if page_rating.startswith("非推奨"):
overall_rating = "非推奨"
elif page_rating.startswith("注意") and overall_rating == "OK":
overall_rating = "注意"
# 追加で、PDFメタ情報のDPIも参考に
doc = fitz.open(pdf_file.name)
dpi_infos = []
for i, page in enumerate(doc):
try:
# MediaBoxや解像度情報から概算
rect = page.rect
width_pt = rect.width
height_pt = rect.height
dpi_x = width / (width_pt / 72)
dpi_y = height / (height_pt / 72)
dpi_infos.append(f"ページ{i+1}推定DPI: {dpi_x:.1f}x{dpi_y:.1f}")
except Exception:
dpi_infos.append(f"ページ{i+1}推定DPI: 取得失敗")
# まとめ
result_text = "\n".join(results + [""] + dpi_infos)
result_text += f"\n\n総合評価: {overall_rating}"
return result_text, overall_rating
with gr.Blocks() as demo:
gr.Markdown("# Audiveris適性チェック(非公式・推定)")
gr.Markdown("PDFをアップロードすると、ページ画像サイズからAudiverisで使えそうかを推定します。")
with gr.Row():
pdf_input = gr.File(label="PDFファイル")
analyze_button = gr.Button("判定")
result_output = gr.Textbox(label="詳細結果", lines=20)
rating_output = gr.Textbox(label="総合評価")
analyze_button.click(analyze_pdf, inputs=pdf_input, outputs=[result_output, rating_output])
if __name__ == "__main__":
demo.launch()