0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

jpgからtxtへ変換する

Posted at

後輩から「jpgの画素値を書いたテキストファイルに変換したい」と相談を受けた.pyinstallerでexe化し,配布してあげることに.
以下,書いたコード.

JPGかPNGを変換する.py
from PIL import Image
from numpy import savetxt, array
from glob import glob
from os.path import dirname, splitext, basename

def jpg2txt(img_path):
    # tsvファイルで保存
    savetxt(f"{dirname(img_path)}/{splitext(basename(img_path))[0]}.txt",
               array(Image.open(img_path).convert("L")),
               fmt="%.0f",delimiter="\t",newline="\n",
               # encording="utf-8"
               )
    return

def main():
    path = input(r"""どのフォルダの画像をテキストに変換しますか? 

C:\Users\.... みたいな感じで入力してください。
中止したいときは、ctrl + C を押してください。

>>> """)
    
    pics = glob(path+r"\*jpg")
    pngs = glob(path+r"\*png")
    if len(pngs) ==0:
        pass
    else:
        for png in pngs:
            pics.append(png)
    
    for pic in pics:
        print(f"{pic} を変換しました")
        jpg2txt(pic)
    return
    
main()
input()

pyinstallerでexeにする.自分の引き出しのなさゆえ,np.savetxtを使ったばかりに exclude --numpyができず,数百メガバイトとなってしまった.

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?