Add Gaussian noise to images with python2.7

【environment】

windows8.1 Anaconda(python2.7) You need to have opencv3 installed in advance.

【Overview】

Creates image data with random Gaussian noise added to multiple image data in the specified folder and saves it in another specified folder.

Folder structure


|---gaussian
    |---before_images(Image folder before adding noise)
    |---before_images(Image folder after adding noise)
    |---gaussian.py

gaussian.py


# -*- coding: utf-8 -*-

import cv2
import numpy as np
import sys
import os
import glob

#Gaussian noise
def addGaussianNoise(src):
    row,col,ch= src.shape
    mean = 0
    var = 0.1
    sigma = 15
    gauss = np.random.normal(mean,sigma,(row,col,ch))
    gauss = gauss.reshape(row,col,ch)
    noisy = src + gauss
    
    return noisy

#Substitution of the directory where the program resides
current_dir = os.getcwd()
#Substitution of the directory where the image exists
before_images = glob.glob(current_dir + "\\before_images\\*") 

i = 0

for image in before_images:
    if image == current_dir + "\\before_images\\Thumbs.db":
        continue
    else:
        #Loading images
        img = cv2.imread(image)
        
        #Add noise
        after_image = addGaussianNoise(img)
        
        #Save image
        cv2.imwrite(current_dir + '\\after_images\\' + str(i) + '.jpg', after_image) 
        i += 1

[Reference URL]

https://github.com/bohemian916/deeplearning_tool/blob/master/increase_picture.py Install opencv3

Recommended Posts

Add Gaussian noise to images with python2.7
Add images to iOS photos with Pythonista
Upload images to Google Drive with Python
Convert PDFs to images in bulk with Python
Connect to BigQuery with Python
How to add help to HDA (with Python script bonus)
Connect to Wikipedia with Python
Switch python to 2.7 with alternatives
Write to csv with Python
Add fake tilt shift effect to video with OpenCV + Python
Send experiment results (text and images) to slack with Python
Convert images to sepia toning with PIL (Python Imaging Library)
Save images on the web to Drive with Python (Colab)
Number recognition in images with Python
Python: How to use async with
Link to get started with python
Add fields to features with ArcPy
[Python] Write to csv file with Python
How to collect images in Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Add Python 2.7 Japanese documentation to Dash.app
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
What to do with PYTHON release?
Post images from Python to Tumblr
Animate multiple still images with Python
Unable to install Python with pyenv
How to use FTP with Python
Load gif images with Python + OpenCV
How to calculate date with python
[Python] Collect images easily with icrawler!
Easily post to twitter with Python 3
Add TRACE log level to Python ...?
I want to debug with Python
Working with DICOM images in Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
The fastest way to get camera images regularly with python opencv
[Python] Try to recognize characters from images with OpenCV and pyocr
Try to reproduce color film with Python
Try logging in to qiita with Python
Convert memo at once with Python 2to3
Learn to colorize monochrome images with Chainer
Memo to ask for KPI with python
How to add a package with PyCharm
Add a Python data source with Redash
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Capturing images with Pupil, python and OpenCV
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
[Introduction to Python] Let's use foreach with Python
Single pixel camera to experience with Python
Function to save images by date [python3]