Spaces:
Sleeping
Sleeping
File size: 2,600 Bytes
3b2d5b2 a1622dd 49acc9d 093fe97 f8f9bae 093fe97 a1622dd 093fe97 49acc9d 093fe97 fe2e131 093fe97 49acc9d 093fe97 5b36d3d 093fe97 f8f9bae 093fe97 5b36d3d 49acc9d 093fe97 49acc9d 093fe97 3b2d5b2 093fe97 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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()
|