8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「...この画像ってどのプログラムでつくったんだっけ」をいい加減やめる

8
Last updated at Posted at 2025-12-17

はじめに

卒修論を筆頭に,以前作成した画像を手直しして再度作成したい場合があります.しかし,画像を作成したプログラムがどれであるかを忘れてしまい,時間をかけてプログラムを探したり書き直したりする場合も多いです.そこで,使用したプログラムの名前を画像のメタデータに埋め込んでプログラム捜索をしなくて済むようにします.

方法

matplolibの savefig の引数である metadata を使います.

import matplotlib.pyplot as plt
import warnings

def savefig_with_program_name(fname, program_name):
    if "pdf" in str(fname):
        metadata = {"Creator": program_name}
    elif "png" in str(fname):
        metadata = {"Author": program_name}
    else:
        # pdf, png 以外は適宜実装してください
        warnings.warn(f"cannot save metadata", UserWarning)

    plt.savefig(fname, metadata=metadata)

メタデータは PIL で可視化できます.

plot_sinewave.py
from PIL import Image
import numpy as np

x = np.linspace(0, 4*np.pi)
y = np.sin(x)
plt.plot(x, y)
savefig_with_program_name("sine_wave.png", __file__)

img = Image.open("sine_wave.png")
print(img.info)
# {
#     'Software': 'Matplotlib version3.10.0, https://matplotlib.org/', 
#     'Author': '/home/.../plot_sinewave.py', 
#     'dpi': (99.9998, 99.9998)
# }

上記のようにメタデータにプログラム名を埋め込むことができました.

終わりに

本当はwindowsのプロパティで可視化したかったのですが,簡単にはいかなそうなので断念...

論文出版も含めて外部に画像を公開する場合は,意図しない情報流出を避けるため,メタデータを消すよう注意してください.

参考文献

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?