2
1

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 3 years have passed since last update.

OpenCVで日本語の画像ファイルを読み書きする方法

Last updated at Posted at 2020-08-23

概要

OpenCVで画像ファイルを読み込む場合、imread、imwriteをよく使用するが、
imread、imwriteはASCII文字のみ使用可能なため、UNICODE文字の日本語があるとエラーになってしまう。
今回は、imread、imwriteするときに日本語ファイル名でも読み書き出来る方法を記載する。

前提条件

  • Python3.7.3
  • OpenCV 4.2

サンプルプログラム

cv2_unicode_readwrite.py

import cv2
import os
import numpy as np

def main():

    READ_FILE = "./テスト.jpg"
    WRITE_FILE = "./テスト2.jpg"

    # 読込
    f = cv2.imdecode(np.fromfile(READ_FILE, dtype=np.uint8),
                    cv2.IMREAD_UNCHANGED)

    # 64x64にリサイズを実施
    resize_face = cv2.resize(f, (64, 64))

    #保存
    is_success, im_buf_arr = cv2.imencode(".jpg", resize_face)
    im_buf_arr.tofile(WRITE_FILE)

if __name__ == "__main__":
    main()

参考ページ

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?