JPEG image generation by specifying quality with Python + OpenCV

References

This article refers to the following pages.

1. Data preparation

Download from the following site to use the data set called 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.

Only BSDS500 / data / images is used this time, so only the necessary ones are taken out. Move the files in test to train.

./data


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

2. Generation (save) of JPEG image by OpenCV

Use the OpenCV imwrite function.

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

** save_path **: destination path, ** img **: image, ** jpeg_quality **: JPEG image quality

3. Sample

.
├── 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)

that's all

Recommended Posts

JPEG image generation by specifying quality with Python + OpenCV
[Small story] Test image generation with Python / OpenCV
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Find image similarity with Python + OpenCV
Gradation image generation with Python [1] | np.linspace
Save video frame by frame with Python OpenCV
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
Light image processing with Python x OpenCV
I tried "smoothing" the image with Python + OpenCV
Convert PDF to image (JPEG / PNG) with Python
I tried "differentiating" the image with Python + OpenCV
How to crop an image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
Binarization with OpenCV / Python
Image processing with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
[Python] Delete by specifying a tag with Beautiful Soup
Image processing with Python (Part 2)
[Python] Using OpenCV with Python (Basic)
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Image processing by python (Pillow)
Image caption generation with Chainer
Get image features with OpenCV
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
[Python] Image processing with scikit-image
[Python] Send gmail with python: Send one by one with multiple image files attached
I tried "gamma correction" of the image with Python + OpenCV
Improve detection accuracy quickly by specifying parameters with openCV face detection
Paste png with alpha channel as transparent image with Python / OpenCV
Mass generation of QR code with character display by Python
Cut out frames from video by 1 second with Python + OpenCV
[Python] Easy reading of serial number image files with OpenCV
Shining life with Python and OpenCV
Cut out an image with python
Real-time image processing basics with opencv
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Edge Detection)
Prime number generation program by Python
Easy Python + OpenCV programming with Canopy
Let's do image scraping with Python
Try face recognition with python + OpenCV
Accelerate read by specifying OpenCV frame
Load gif images with Python + OpenCV
Object extraction in images by pattern matching using OpenCV with Python
Password generation in texto with python
CSRF countermeasure token generation with Python
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
Image processing with Python 100 knocks # 2 Grayscale
Draw an illustration with Python + OpenCV
Introduction to image analysis opencv python