|
| 1 | +# Python Modules |
| 2 | + |
| 3 | + |
| 4 | +# Os Module |
| 5 | +# we can do file move, delet, check extension using this module |
| 6 | + |
| 7 | + |
| 8 | +# import os |
| 9 | +# print(os.getcwd()) # o/p: F:\Python_prac\Chapter 20 |
| 10 | + |
| 11 | +# create folder |
| 12 | + |
| 13 | +# path=(r'F:\Python_prac\Chapter 20\movies' |
| 14 | +# os.mkdir("movies") # when i ran first time, folder created, but when i ran second time i got error(FileExistsError: [WinError 183] Cannot create a folder when that folder already exists: 'movies') |
| 15 | +# print(os.path.exists('movies')) # o/p: True # it will check file exists or not and we have to enter path of the folder instead of folder name only. |
| 16 | +# if os.path.exists('movies'): |
| 17 | +# print("already exists") |
| 18 | +# else: # o/p: already exists |
| 19 | +# os.mkdir('movies') |
| 20 | + |
| 21 | + |
| 22 | +# path=(r'F:\Python_prac\Chapter 20\movies') |
| 23 | +# --OR--- |
| 24 | +# os.chdir(r'F:\Python_prac\Chapter 20') and then use mkdir('movies') method |
| 25 | + |
| 26 | +# create file |
| 27 | +# open('file.txt','a').close() # it will create file, and it will not give error in case of file existing |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +# print(os.listdir()) # o/p: ['file.txt', 'movies', 'p20.py'] # it will return the list of files and folders of current working directory |
| 32 | +# print(os.listdir(r'F:\Courses\python hindi')) # o/p: ['harshit vashisth', 'test.py'] |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +# # if we want file path # o/p: F:\Python_prac\Chapter 20\file.txt |
| 37 | +# for item in os.listdir(): # F:\Python_prac\Chapter 20\movies |
| 38 | +# # print(os.getcwd()+'\\'+item) # F:\Python_prac\Chapter 20\p20.py |
| 39 | +# # or |
| 40 | +# path=os.path.join(os.getcwd(),item) |
| 41 | +# print(path) |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +# for item in os.listdir(r'F:\Courses\python hindi'): |
| 47 | +# # print(r'F:\Courses\python hindi'+'\\'+item) |
| 48 | +# # or # o/p: F:\Courses\python hindi\harshit vashisth |
| 49 | +# path=os.path.join(r'F:\Courses\python hindi',item) # F:\Courses\python hindi\test.py |
| 50 | +# print(path) |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +# os.chdir(r'F:\Courses') |
| 57 | +# print(os.listdir()) |
| 58 | + |
| 59 | +# # o/p: ['AWS', 'python 2', 'python hindi', '[FreeCourseLab.com] Udemy - Build Responsive Website Using HTML5, CSS3, JS And Bootstrap', '[FreeCourseLab.com] Udemy - Git for Windows Step-By-Step Mastery using Commands and GUI', '[FreeCourseLab.com] Udemy - Python 3 Complete Masterclass - Make Your Job Tasks Easier!', '[FreeCourseSite.com] Udemy - Docker Mastery The Complete Toolset From a Docker Captain', '[FreeTutorials.Eu] data-structures-algorithms-in-python', '[FreeTutorials.Eu] Udemy - Adobe Photoshop CC – Essentials Training Course', '[FreeTutorials.Eu] Udemy - sql-mysql-for-data-analytics-and-business-intelligence'] |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +# os.chdir(r'F:\Python_prac\Chapter 20') |
| 67 | +# # print(os.walk(r'F:\Python_prac\Chapter 20')) # o/p: <generator object walk at 0x00A0EED0> |
| 68 | +# file_iter=os.walk(r'F:\Python_prac\Chapter 20') |
| 69 | +# for current_path, folder_names, file_names in file_iter: |
| 70 | +# print(f"current_path : {current_path}") |
| 71 | +# print(f"folder_names : {folder_names}") |
| 72 | +# print(f"file_names : {file_names}") |
| 73 | + |
| 74 | +# # o/p: current_path : F:\Python_prac\Chapter 20 |
| 75 | +# # folder_names : ['movies'] |
| 76 | +# # file_names : ['file.txt', 'p20.py'] |
| 77 | +# # current_path : F:\Python_prac\Chapter 20\movies |
| 78 | +# # folder_names : ['Ravin'] |
| 79 | +# # file_names : ['New Microsoft Excel Worksheet.xlsx'] |
| 80 | +# # current_path : F:\Python_prac\Chapter 20\movies\Ravin |
| 81 | +# # folder_names : [] |
| 82 | +# # file_names : ['New Bitmap Image.bmp'] |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +# create folder inside folder |
| 87 | +# os.makedirs('new/Ravin') # it will create Ravin folder inside new folder(here new and Ravin both are new folder) |
| 88 | + |
| 89 | +# i want to delete folder |
| 90 | + |
| 91 | +# os.rmdir('new') # o/p: OSError: [WinError 145] The directory is not empty: 'new' this function delete only empty folder |
| 92 | +# we can delete not empty folder using shutil module |
| 93 | +# import shutil |
| 94 | + |
| 95 | +# delete not empty folder |
| 96 | +# shutil.rmtree('new') # rmtree function delete not empty folder permenantly, means folder will not move to the recycle bin |
| 97 | + |
| 98 | +# copy not empty folder in another folder |
| 99 | +# shutil.copytree('new','movies/new123') # copy new folder in movies and where new folder copied with new name like new123. we have to give path insted of folder name |
| 100 | + |
| 101 | +# copy file in anoter folder |
| 102 | +# shutil.copy(r'F:\Python_prac\Chapter 20\movies\a.xlsx',r'F:/Python_prac/Chapter 20/movies/new123/a1.xlsx') |
| 103 | + |
| 104 | +# move one folder in another folder |
| 105 | +# shutil.move(r'F:\Python_prac\Chapter 20\movies\new123',r'F:/Python_prac/Chapter 20/movies/Ravin/new1234') |
| 106 | + |
| 107 | +# move file in another folder |
| 108 | +# shutil.move(r'F:\Python_prac\Chapter 20\movies\a.xlsx',r'F:/Python_prac/Chapter 20/movies/new123/a2.xlsx') |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +# Edit Imagies using python ---> we can do that using pillow library of python, but it is not standard library of python |
| 125 | + |
| 126 | +# Installation of pillow library |
| 127 | +# |
| 128 | +# pip install Pillow |
| 129 | + |
| 130 | + |
| 131 | +# how to change the extension |
| 132 | +# resize image files |
| 133 | +# resize multiple images using for loop |
| 134 | +# Sharpness |
| 135 | +# Brightness |
| 136 | +# Color |
| 137 | +# Contrast |
| 138 | +# Image blur, Gaussianblur |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +from PIL import Image |
| 143 | +import os |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +# # how to change the extension |
| 149 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin\r.png') |
| 150 | +# # if img1.mode == 'RGBA': |
| 151 | +# # img1 = img1.convert('RGB') |
| 152 | +# # img1.save(r'F:\Python_prac\Chapter 20\Ravin\r.jpg') |
| 153 | +# # img1.show() |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +# # resize image files |
| 160 | +# max_size=(250,250) |
| 161 | +# img1.thumbnail(max_size) |
| 162 | +# img1.save(r'F:\Python_prac\Chapter 20\Ravin\rsmall.png') |
| 163 | + |
| 164 | + |
| 165 | +# resize multiple images using for loop |
| 166 | + |
| 167 | +# How to deal with all files |
| 168 | + |
| 169 | +# for item in os.listdir(r'F:\Python_prac\Chapter 20\Ravin'): |
| 170 | +# if item.endswith('.jpg'): |
| 171 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin'+'\\'+item) |
| 172 | +# file_name, extension=os.path.splitext(item) |
| 173 | +# img1.save(r'F:\Python_prac\Chapter 20\Ravin'+'\\'+f'{file_name}.png') |
| 174 | + |
| 175 | + |
| 176 | +# for item in os.listdir(r'F:\Python_prac\Chapter 20\Ravin'): |
| 177 | +# if item.endswith('.jpg'): |
| 178 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin'+'\\'+item) |
| 179 | +# max_size=(250,250) |
| 180 | +# img1.thumbnail(max_size) |
| 181 | +# file_name, extension=os.path.splitext(item) |
| 182 | +# img1.save(r'F:\Python_prac\Chapter 20\Ravin'+'\\'+f'{file_name}r.png') |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +# sharpness, brightnessm color, contrast we have to import ImageEnhance module |
| 190 | + |
| 191 | +# Sharpness |
| 192 | +# from PIL import ImageEnhance |
| 193 | + |
| 194 | + |
| 195 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin\r2.jpg') |
| 196 | +# enhancer=ImageEnhance.Sharpness(img1) # Sharpness is a class, ImageEnhance is a module |
| 197 | +# enhancer.enhance(10).save(r'F:\Python_prac\Chapter 20\Ravin\r2edited1.jpg') |
| 198 | +# # 0 ---> blurry image |
| 199 | +# # 1 ---> original image |
| 200 | +# # 2 ---> image with increased sharpness |
| 201 | +# # 3,4,5... ---> sharpness will incress |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | +# # # Color |
| 208 | + |
| 209 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin\r2edited1.jpg') |
| 210 | +# enhancer=ImageEnhance.Color(img1) # Color is a class, ImageEnhance is a module |
| 211 | +# enhancer.enhance(2).save(r'F:\Python_prac\Chapter 20\Ravin\r2editedc.png') |
| 212 | + |
| 213 | +# # 0 ---> black and white image |
| 214 | +# # 1 ---> original image |
| 215 | +# # 2,3,4...----> increase colour |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | +# # # Brightness |
| 224 | + |
| 225 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin\r2edited1.jpg') |
| 226 | +# enhancer=ImageEnhance.Brightness(img1) # Brightness is a class, ImageEnhance is a module |
| 227 | +# enhancer.enhance(1.5).save(r'F:\Python_prac\Chapter 20\Ravin\r2editedb.png') |
| 228 | + |
| 229 | +# # 0 ---> black image |
| 230 | +# # 1 ---> original image |
| 231 | +# # 2,3,4...----> increase brightness |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | + |
| 237 | +# # # Contrast |
| 238 | + |
| 239 | +# img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin\r2edited1.jpg') |
| 240 | +# enhancer=ImageEnhance.Contrast(img1) # Contrast is a class, ImageEnhance is a module |
| 241 | +# enhancer.enhance(1.5).save(r'F:\Python_prac\Chapter 20\Ravin\r2editedco.png') |
| 242 | + |
| 243 | +# # 0 ---> black and white image |
| 244 | +# # 1 ---> original image |
| 245 | +# # 2,3,4...----> increase contrast |
| 246 | + |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | +# Image blur ----> for it we have to import filter |
| 254 | +from PIL import ImageFilter |
| 255 | + |
| 256 | +img1=Image.open(r'F:\Python_prac\Chapter 20\Ravin\r2edited1.jpg') |
| 257 | +img1.filter(ImageFilter.Gaussianblur(radius=4)).save(r'F:\Python_prac\Chapter 20\Ravin\r2blur.png') |
| 258 | + |
| 259 | +# bydefault radius is 2 |
0 commit comments