이미지 그리드 자동 생성

이미지 그리드 자동 생성

으로 생성한 PDF 이미지가 여러 개 있습니다 matplotlib. 나는 그것들을 임의의 그리드에 배열할 수 있기를 원합니다. 이를 위해 나는 라텍스를 작성하고 실행하는 Python 스크립트를 작성했습니다.

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--images', nargs='+', help='List of images to be processed')
    parser.add_argument('--output', help='Output file name', default='output_grid.pdf')
    parser.add_argument('--grid', nargs=2, type=int, help='Grid dimensions (rows, columns)')
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    assert len(args.images) == args.grid[0]*args.grid[1], "Number of images does not match grid dimensions"
    tex_outfile = "_{}_.tex".format(random.randint(0,100000))
    with open(tex_outfile, 'w') as f:
        f.write("\\documentclass[varwidth]{standalone}\n")
        f.write("\\usepackage[pdftex]{graphicx, color}\n")
        f.write("\\begin{document}\n")
        f.write("\\begin{{tabular}}{{{}}}\n".format(" c "*args.grid[1]))
        for k,v in enumerate(args.images):
            f.write("\\includegraphics[width={:.9f}\\linewidth,keepaspectratio]{{{}}}".format(1.0/(args.grid[1] + 1), v))
            if (k+1) % args.grid[1] != 0:
                f.write(" & ")
            else: 
                f.write(" \\\\\n")
        f.write("\\end{tabular}\n")
        f.write("\\end{document}\n")
    os.system("pdflatex {}".format(tex_outfile))
    os.system("mv {}.pdf {}".format(tex_outfile[:-4], args.output))
    os.system("rm {}*".format(tex_outfile[:-4]))

위의 Python 코드는 아래의 Latex를 생성한 다음 pdflatexPython 코드 줄에서 실행됩니다.os.system("pdflatex {}".format(tex_outfile))

\documentclass[varwidth]{standalone}
\usepackage[pdftex]{graphicx, color}
\begin{document}
\begin{tabular}{ c  c }
\includegraphics[width=0.333333333\linewidth,keepaspectratio]{image1.pdf} & \includegraphics[width=0.333333333\linewidth,keepaspectratio]{image2.pdf} \\
\includegraphics[width=0.333333333\linewidth,keepaspectratio]{image3.pdf} & \includegraphics[width=0.333333333\linewidth,keepaspectratio]{image4.pdf} \\
\end{tabular}
\end{document}

내가 가진 문제는 이미지가 완벽하게 정렬되지 않았다는 것입니다. 이 이미지를 참조하세요(스택 교환을 위해 png로 변환됨).

여기에 이미지 설명을 입력하세요

정렬 문제는 y-틱의 길이가 다르기 때문이라고 생각합니다. 제가 제공한 코드에서 이 문제를 해결할 수 있는 방법이 있나요? 또는 더 나은 방법이 될 수 있는 대안이 있습니다( subplots개별 이미지가 이미 PDF로 제공되므로 를 사용하면 작동하지 않습니다).

답변1

왼쪽 라벨 길이가 사물에 영향을 주지 않도록 오른쪽 정렬하고 너비가 아닌 배율을 사용하여 이미지 크기가 같은 양만큼 조정되도록 하여 플롯이 원래 동일한 크기라면 동일한 크기가 되도록 합니다.

관련 정보