Sunday, February 19, 2017

Edit photo - Picture frame - Image effects in Python

from skimage import data, io
import sys
from skimage.color import gray2rgb

try:
    rgb_image = data.imread(sys.argv[1])
    height = rgb_image.shape[0]
    width = rgb_image.shape[1]

    if(len(rgb_image.shape) < 3):
        rgb_image = gray2rgb(rgb_image)
   
    for i,j in zip(range(int(width/8)), range(int(width/8),0,-1)):
        rgb_image[i, 0:j, :] = 0
   
    for i,j in zip(range(int(7*(height/8)),height), range(0,int(width/8))):
        rgb_image[i, 0:j, :] = 0
   

Edit photo - Square crop - Image resizing in Python

from skimage import data, io
import sys
from skimage.color import gray2rgb

try:
    rgb_image = data.imread(sys.argv[1])
    height = rgb_image.shape[0]
    width = rgb_image.shape[1]

    if height == width:
        print("already square image")
   
    else:
        if(len(rgb_image.shape) < 3):
            rgb_image = gray2rgb(rgb_image)
           
        right_image = rgb_image.copy()
        left_image = rgb_image.copy()
        center_image = rgb_image.copy()
       

Edit photo - Oil painting effect - Image manipulation in Python

from skimage import data, io
import random
import sys
from skimage.color import gray2rgb

try:
    rgb_image = data.imread(sys.argv[1])
    height = rgb_image.shape[0]
    width = rgb_image.shape[1]

    if(len(rgb_image.shape) < 3):
        rgb_image = gray2rgb(rgb_image)
   
    for i in range(height):
        for j in range(width):
            for k in range(rgb_image.shape[2]):

Edit photo - Pencil sketch effect - Image processing in Python

Code:
from skimage import data, io, util
import random
import sys
from skimage.color import rgb2gray

try:
    rgb_image = data.imread(sys.argv[1])
   
    height = rgb_image.shape[0]
    width = rgb_image.shape[1]
   
    if(len(rgb_image.shape) == 3):
        rgb_image = util.img_as_ubyte(rgb2gray(rgb_image))

Python: Count occurences in List

1. Histogram

>>> mylist = [1,3,2,5,4,4,2,2,4,2,4,2,4,6,4,5,2]

>>> import matplotlib.pyplot as plt

>>> plt.hist(mylist)
(array([ 1.,  0.,  6.,  0.,  1.,  0.,  6.,  0.,  2.,  1.]), array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ,  5.5,  6. ]), <a list of 10 Patch objects>)

>>> plt.show()


Edit photo - Change color effects - Image filtering in Python

Code:-

from skimage import data, io
import random

try:
    rgb_image = data.imread("Collage.jpg")
    #print(rgb_image.shape)
   
    red_image = rgb_image.copy()
    green_image = rgb_image.copy()
    blue_image = rgb_image.copy()
   
    red_image[:, :, (1, 2)] = 0
    io.imsave("red_output_image.jpg", red_image)
   
    green_image[:, :, (0, 2)] = 0
    io.imsave("green_output_image.jpg", green_image)
   
    blue_image[:, :, (0, 1)] = 0
    io.imsave("blue_output_image.jpg", blue_image)
   

Saturday, February 18, 2017

Bar plot in Python using Bokeh


import pandas as pd
from collections import Counter
from datetime import datetime, timedelta
from bokeh.charts import Bar, output_file, show
from bokeh.models.layouts import Column
import numpy as np

try:
    data = pd.read_csv('new.csv')
    scan_col = Counter(data['Scan'])
    values = list()
    keys = list()
   
    for s in scan_col.keys():
        scan_time_total = datetime.strptime("00:00:00","%H:%M:%S")
        scan_time_total = timedelta(hours=scan_time_total.hour, minutes=scan_time_total.minute, seconds=scan_time_total.second)

Bar plot in Python using Matplotlib


import pandas as pd
from collections import Counter
from datetime import datetime, timedelta
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

try:
    data = pd.read_csv('new.csv')

    srn_col = Counter(data['SRN'])
    month_col = Counter(data['Month'])
    scan_col = Counter(data['Scan'])

    scan_dict = {}

Bar plot in Python using Plotly

Step 1: Install plotly package if you haven't.

Follow this article: http://com.puter.tips/2017/02/install-plotly-package-in-python.html

Step 2: Set up your online credentials for plotly account.

2.1: Go to https://plot.ly/settings/api
2.2: Sign in/sign up.
2.3: Open https://plot.ly/settings/api (API Settings page) and click on Regenerate Key button. Copy the API key.

Install Plotly package in Python

You need pip package manager to install Plotly package in Python.

Use this command to install plotly with python:
pip install plotly


Ref.: https://plot.ly/python/getting-started/