<jupyter_start><jupyter_text>1806554 <NAME> Python Assignment 4  1<jupyter_code># 1 method
import matplotlib.pyplot as plt
import cv2
i = cv2.imread('demo.png')
plt.imshow(i)
# 2 method
out = cv2.imshow('Image Output',i)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 3 method
from PIL import Image  
im = Image.open(r"demo.png")  
im.show()<jupyter_output><empty_output><jupyter_text>2<jupyter_code>i.shape<jupyter_output><empty_output><jupyter_text>3<jupyter_code>gi = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) 
plt.imshow(gi)<jupyter_output><empty_output><jupyter_text>4<jupyter_code>import imutils as im
ih = im.rotate(i,90)
plt.imshow(ih)
# vertical flip
iv = cv2.flip(i,0)
plt.imshow(iv)
# horizontal flip
iv = cv2.flip(i,1)
plt.imshow(iv)<jupyter_output><empty_output><jupyter_text>5<jupyter_code># resize
new_size = 100
ds = (i.shape[1],new_size)
output = cv2.resize(i, ds, interpolation = cv2.INTER_AREA)
plt.imshow(output)<jupyter_output><empty_output><jupyter_text>6<jupyter_code># new window
out = cv2.imshow('Image Output',i)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>7<jupyter_code># show in jupyter
plt.imshow(i)<jupyter_output><empty_output><jupyter_text>8<jupyter_code># concat side by side horizontal
im_h = cv2.hconcat([i,i])
cv2.imwrite('opencv_hconcat.jpg', im_h)
j = cv2.imread('opencv_hconcat.jpg')
plt.imshow(j)<jupyter_output><empty_output><jupyter_text>9<jupyter_code># crop image
#i.shape
val = input()
(hs,he,ws,we) = map(int,val.strip().split(' ')) 
cropped = i[hs:he, ws:we]
plt.imshow(cropped)<jupyter_output>70 170 230 290<jupyter_text>10<jupyter_code># Binarized Image
import numpy as np
img = cv2.imread('demo.png')
height,width,channels = img.shape
img_binary = np.zeros((height,width,1))
img_grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, img_binary) = cv2.threshold(img_grayscale, 128, 255, cv2.THRESH_BINARY)
cv2.imwrite('image_binary.jpg',img_binary)
cv2.imshow('image',img_binary)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>11<jupyter_code># binary image
matz = np.random.randint(2,size = (5,5))
print(matz)
plt.imshow(matz, cmap="gray") 
plt.show()<jupyter_output>[[0 1 1 1 0]
 [1 1 0 0 1]
 [0 1 0 1 0]
 [1 0 0 0 1]
 [0 1 0 1 0]]<jupyter_text>12<jupyter_code># RGB Channels
rgbImage = cv2.imread('demo.png')
# redChannel = rgbImage[:,:,1]
# greenChannel = rgbImage[:,:,2]
# blueChannel = rgbImage[:,:,2]
def channelShift(i):
    return rgbImage[:,:,i]
x = (0,1,2)
r,g,b = map(channelShift,x)
plt.imshow(r)
plt.imshow(g)
plt.imshow(b)<jupyter_output><empty_output><jupyter_text>13<jupyter_code># Text on Image
img = cv2.imread('demo.png')
font = cv2.FONT_HERSHEY_DUPLEX 
org = (25,35) 
fontScale = 1
color = (255,0,0) 
thickness = 2
sentence = input()
image = cv2.putText(img,sentence,org,font,fontScale,color,thickness,cv2.LINE_AA) 
cv2.imwrite('opencv_T.jpg',image)
j = cv2.imread('opencv_T.jpg')
plt.imshow(j)<jupyter_output>1806554<jupyter_text>14<jupyter_code># images in folder
import cv2
import os

def printImageNames(folder):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename))
        if img is not None:
            images.append(filename)
    images.sort()
    return images
folder="C:/Users/KIIT/Documents/College-Stuff/T&T/python scripts/"
printImgNames(folder)<jupyter_output><empty_output><jupyter_text>15<jupyter_code># count Images
def cntImages(path):
    cnt = 0
    for filename in os.listdir(path):
        img = cv2.imread(os.path.join(path,filename))
        if img is not None:
            cnt+=1
    return cnt
path="C:/Users/KIIT/Documents/College-Stuff/T&T/python scripts/"
cntImages(path)<jupyter_output><empty_output><jupyter_text>16<jupyter_code># copy and Name change
import shutil
import os
src_dir = "C:/Users/KIIT/Documents/College-Stuff/T&T/python scripts/"
dst_dir = "C:/Users/KIIT/Documents/College-Stuff/T&T/python scripts/work/"
for filename in glob.glob(os.path.join(src_dir, '*.jpg')):
    shutil.copy(filename, dst_dir)
for cnt,filename in enumerate(os.listdir("work")):
    new_name = str(filename)
    dst = new_name[:-4] + "_" + str(cnt) + ".jpg"
    src = 'work/' + filename
    dst = 'work/' + dst
    os.rename(src,dst)<jupyter_output><empty_output><jupyter_text>17<jupyter_code># Specifications
def Info(folder):
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename))
        if img is not None:
            with open("images_info.txt","a") as f:
                f.write("filename : "+str(filename))
                f.write(" Info -> \n")
                f.write("height : "+str(img.shape[0])+" "
                        "width : "+str(img.shape[1])+" "
                        "channels : "+str(img.shape[2]))
                f.write("\n")
    with open("images_info.txt") as f:
        for line in f:
            print(line,end = "") 
folder="C:/Users/KIIT/Documents/College-Stuff/T&T/python scripts/"
Info(folder)<jupyter_output>filename : demo.png Info -> 
height : 172 width : 292 channels : 3
filename : image_binary.jpg Info -> 
height : 172 width : 292 channels : 3
filename : opencv_hconcat.jpg Info -> 
height : 172 width : 584 channels : 3
filename : opencv_T.jpg Info -> 
height : 172 width : 292 channels : 3
{'fraction_non_alphanumeric': 0.15327915327915329, 'fraction_numerical': 0.03591003591003591, 'mean_word_length': 4.752173913043478, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '506002', 'license': 'MIT', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2113, 'n_words': 368, 'path': 'T&T/python scripts/4_1806554.ipynb', 'repo_name': 'ganeshbhandarkar/College-Labs-And-Projects', 'size': '5302'}