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

Python+OpenCVで品質を指定してJPEG画像生成

Posted at

参考文献

本記事は以下のページを参考にしています。

1. データ準備

BSDS500というデータセットを使用するため、以下のサイトからダウンロード。

https://github.com/BIDS/BSDS500
P. Arbelaez, M. Maire, C. Fowlkes and J. Malik, "Contour Detection and Hierarchical Image Segmentation," IEEE TPAMI, Vol. 33, No. 5, pp. 898-916, May 2011.

今回使用するのは、BSDS500/data/imagesだけなので、必要なもののみ取り出す。
test内のファイルをtrainに移動する。

./data
.
└── BSDS500
    └── images
        ├── train   # train + test = 400 images
        └── val     # val          = 100 images

2. OpenCVによるJPEG画像の生成(保存)

OpenCVのimwrite関数を使用する。

cv2.imwrite(<save_path>, <img>, [int(cv2.IMWRITE_JPEG_QUALITY), <jpeg_quality>])

save_path: 保存先のパス、img: 画像、jpeg_quality: JPEG画像の品質

3. サンプル

.
├── create_jpeg_image.py
└── data
    └── BSDS500
        └── images
            ├── train
            │   └── gnd
            └── val
                └── gnd
create_jpeg_image.py
import os
import cv2
import argparse


def save_jpeg_images(src_path, dst_path, q=100):
    file_list = os.listdir(src_path)
    dst_path  = os.path.join(dst_path, 'q{}'.format(q))
    os.makedirs(dst_path, exist_ok=True)

    for file_name in file_list:
        im = cv2.imread(os.path.join(src_path, file_name))
        cv2.imwrite(os.path.join(dst_path, file_name), im, [int(cv2.IMWRITE_JPEG_QUALITY), q])

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--src_path', default='./data/BSDS500/images/val/gnd')
    parser.add_argument('--dst_path', default='./data/BSDS500/images/val')
    parser.add_argument('--q', type=int, default=10)
    args = parser.parse_args()

    save_jpeg_images(args.src_path, args.dst_path, args.q)

以上

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