Package: keygim
keygim Package DocumentationThis library provides various image utilities and a function to download images from Google search.There are two modules in this package.keygim-used to download Google imagesimageUtils-image and basic utilitiesTo install this package, type in command prompt:pip install keygim[1] Keywind Google Images(1-A)LinkFormatter- Obtaining the Google Search URLs for each search string:LinkFormatter(stringList)argumentoptionaldefaultavailablestringListno[ list-string type ]self.fetch_links(self, large = False)argumentoptionaldefaultavailablelargeyesFalseTrue, False(1-A-1) Example of using LinkFormatterfrom keygim.keygim import KeyGoogleImages
LinkFormatter = KeyGoogleImages.LinkFormatter
search_strings = [ "landscape", "galaxies" ]
large = False
link_list = LinkFormatter(search_strings).fetch_links(large = large)
print(link_list[0]) # "https://www.google.com/search?q=landscape&tbm=isch"
print(link_list[1]) # "https://www.google.com/search?q=galaxies&tbm=isch"(1-B)GoogleImageDownloader- Downloading Google images provided a list of strings:GoogleImageDownloader(search_strings, mode = 0, driver_path = None)argumentoptionaldefaultavailablesearch_stringsno[ list-string type ]modeyesGoogleImageDownloader.MODE_CHROMEGoogleImageDownloader.MODE_CHROME, GoogleImageDownloader.MODE_FIREFOXdriver_pathyesNoneNone, [ string type ]self.configure(**kwargs)argumentoptionaldefaultavailablefolder_nameyesNoneNone, [ string type ]hide_windowyesFalseTrue, Falsewait_loadyesTrueTrue, Falsesearch_largeyesFalseTrue, Falsesave_searchyesFalseTrue, Falseitem_waityes1[ integer/float (>=0) type ]images_per_pageyes10[ integer (>0) type ]verboseyesTrueTrue, Falseurl_timeoutyes60[ integer/float (>0) type ]load_timeoutyes30[ integer/float (>0) type ]If a folder name is provided, all images will be stored in that folder after download. Should the folder not already exist, that folder will be created. By default, each search string will be the name of the folder storing the corresponding images.To scrape and download images in the background, sethide_windowto True.To search images in large mode, setsearch_largeto True.To wait between consecutive images, pass a number toitem_waitso that two images will be separated bytime_waitseconds.To specify the number of images per search string, pass an integer to the argumentimages_per_page.To hide the progress bar, setverboseto False.To specify the maximum number of seconds to connect to the direct link, pass a number tourl_timeout.To specify the maximum number of seconds to download an image, pass a number toload_timeout.self.download()Calling this function will initiate the downloading process.self.cleanup()Calling this function after finished downloading to remove corrupted image files in the folder.(1-B-1) Example of using GoogleImageDownloaderfrom keygim.keygim import KeyGoogleImages
GoogleImageDownloader = KeyGoogleImages.GoogleImageDownloader
search_strings = [ "scenery", "galaxies" ]
large = True
mode = GoogleImageDownloader.MODE_CHROME
driver_path = "./chromedriver.exe"
folder_name = "./KGM_DEMO/"
downloader = GoogleImageDownloader(search_strings, mode, driver_path)
downloader.configure(
  folder_name = folder_name,
  search_large = large
)
downloader.download()
downloader.cleanup()[2] Image Utilities(2-A)ImageLoader- Loading images from disk:ImageLoader(filename, mode = ImageLoader.COLOR_BGR_3D)argumentoptionaldefaultavailablefilenameno[ string type ]modeyesImageLoader.COLOR_BGR_3DImageLoader.COLOR_GRAY_2D, ImageLoader.COLOR_GRAY_3D, ImageLoader.COLOR_BGR_3D, ImageLoader.COLOR_BGRA_3D, ImageLoader.COLOR_RGB_3D, ImageLoader.COLOR_RGBA_3Dself.load()ImageLoader.load_images(nameList, mode = ImageLoader.COLOR_BGR_3D)argumentoptionaldefaultavailablenameListno[ list-string type ]modeyesImageLoader.COLOR_BGR_3DImageLoader.COLOR_GRAY_2D, ImageLoader.COLOR_GRAY_3D, ImageLoader.COLOR_BGR_3D, ImageLoader.COLOR_BGRA_3D, ImageLoader.COLOR_RGB_3D, ImageLoader.COLOR_RGBA_3DImageLoader.load_images_from_folder(folderName, quantity = "all", shuffle = False)argumentoptionaldefaultavailablefolderNameno[ string type ]quantityyes"all""all", [ integer (>0) type ]shuffleyesFalseTrue, False(2-B)ImageSaver- Saving images to disk:ImageSaver(filename, mode = ImageLoader.COLOR_BGR_3D)argumentoptionaldefaultavailablefilenameno[ string type ]modeyesImageSaver.COLOR_BGR_3DImageSaver.COLOR_GRAY_2D, ImageSaver.COLOR_GRAY_3D, ImageSaver.COLOR_BGR_3D, ImageSaver.COLOR_BGRA_3D, ImageSaver.COLOR_RGB_3D, ImageSaver.COLOR_RGBA_3Dself.save_image(image, overwrite = True)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]overwriteyesTrueTrue, Falseself.save_images(imageList, overwrite = True)argumentoptionaldefaultavailableimageListno[ list-numpy-uint8 type ], [ numpy-uint8 type ]overwriteyesTrueTrue, False(2-C)ImageDisplayer- Displaying images in a window:ImageDisplayer(image, windowName = "Image Preview")argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]windowNameyes"Image Preview"[ string type ]self.start()self.stop()self.display_for(interval = 5)argumentoptionaldefaultavailableintervalyes5[ integer/float (>0) type ]ImageDisplayer.display_multiple_for(imageList, windowName = "Image Preview", interval = 1)argumentoptionaldefaultavailableimageListno[ list-numpy-uint8 type ], [ numpy-uint8 type ]windowNameyes"Image Preview"[ string type ]intervalyes1[ integer/float (>0) type ](2-D)SnapshotTaker- Taking a screenshot:SnapshotTaker(mode = SnapshotTaker.COLOR_BGR_3D)argumentoptionaldefaultavailablemodeyesSnapshotTaker.COLOR_BGR_3DSnapshotTaker.COLOR_GRAY_2D, SnapshotTaker.COLOR_GRAY_3D, SnapshotTaker.COLOR_BGR_3D, SnapshotTaker.COLOR_BGRA_3D, SnapshotTaker.COLOR_RGB_3D, SnapshotTaker.COLOR_RGBA_3Dself.take_screenshot(key = None, message = None)argumentoptionaldefaultavailablekeyyesNoneNone, [ pynput-key type ]messageyesNoneNone, [ string type ]self.take_screenshots(key = None, quantity = 1, message = None)argumentoptionaldefaultavailablekeyyesNoneNone, [ pynput-key type ]quantityyes1[ integer (>0) type ]messageyesNoneNone, [ string type ](2-D-1) Example of using SnapshotTaker & ImageDisplayerfrom keygim.imageUtils import ImageUtils
from pynput.keyboard import Key
SnapshotTaker = ImageUtils.SnapshotTaker
ImageDisplayer = ImageUtils.ImageDisplayer
key = Key.esc
message = "Press Esc to take screenshot."
mode = SnapshotTaker.COLOR_BGR_3D
image = SnapshotTaker(mode = mode).take_screenshot(
  key = key, message = message
)
ImageDisplayer(image).display_for(interval = 3)(2-E)BoundaryBoxSelector- Selecting boundary box from background image:BoundaryBoxSelector(image, windowName = "Boundary Box Selector", boxColor = (255, 0, 0), boxWidth = 5)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]windowNameyes"Boundary Box Selector"[ string type ]boxColoryes(255, 0, 0)[ tuple-BGR type ]boxWidthyes5[ integer (>0) type ]self.draw()self.draw_multiple(quantity = 1)argumentoptionaldefaultavailablequantityyes1[ integer (>0) type]self.place(width, height)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type]self.place_multiple(width, height, quantity = 1)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type ]quantityyes1[ integer (>0) type ]BoundaryBoxSelector.crop_partial(image, bbox)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]bboxno[ BoundaryBoxSelector.BoundaryBox type ]BoundaryBoxSelector.crop_multiple(image, bboxes)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]bboxesno[ list-BoundaryBoxSelector.BoundaryBox type ](2-E-1) Example of using SnapshotTaker & BoundaryBoxSelector & ImageDisplayerfrom keygim.imageUtils import ImageUtils
from pynput.keyboard import Key
ImageDisplayer = ImageUtils.ImageDisplayer
BoundaryBoxSelector = ImageUtils.BoundaryBoxSelector
SnapshotTaker = ImageUtils.SnapshotTaker
key = Key.esc
message = "Press Esc to take screenshot."
image = SnapshotTaker().take_screenshot(key, message)
bbox = BoundaryBoxSelector(image).draw()
image = BoundaryBoxSelector.crop_partial(image, bbox)
ImageDisplayer(image).display_for(interval = 4)(2-E-A)BoundaryBoxSelector.BoundaryBox- Creating a boundary box object:BoundaryBoxSelector.BoundaryBox(SX, SY, EX, EY)argumentoptionaldefaultavailableSXno[ integer (>=0) type ]SYno[ integer (>=0) type ]EXno[ integer (>=0) type ]EYno[ integer (>=0) type ]self.__getitem__(key)argumentoptionaldefaultavailablekeyno"sx", "sy", "ex", "ey", "dx", "dy", "cx", "cy", "tuple"self.sort()(2-F)ImageRandomCropper- Cropping a random region from an image:ImageRandomCropper(image)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]self.crop(width, height, overflow = True)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type ]overflowyesTrueTrue, Falseself.crop_multiple(width, height, quantity = 1, overflow = True)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type ]quantityyes1[ integer (>0) type ]overflowyesTrueTrue, False(2-G)ImageOverLayer- Adding a background to a 4-channel image:ImageOverLayer(image)argumentoptionaldefaultavailableimageno[ numpy-uint8 (4 channels) type ]self.overlay(background)argumentoptionaldefaultavailablebackgroundno[ numpy-uint8 (>=3 channels) type ]self.overlay_multiple(backgroundList)argumentoptionaldefaultavailablebackgroundListno[ list-numpy-uint8 type ], [ numpy-uint8 type ](2-H)convert_color- Color conversion for multiple images:convert_color(imageList, color_code)argumentoptionaldefaultavailableimageListno[ list-numpy-uint8 type ], [ numpy-uint8 type ]color_codeno[ cv2-cvtColor type ](2-I)ScreenshotCropperSaver- Taking a screenshot, cropping it and saving the image:ScreenshotCropperSaver(filename = "temp.png", draw = True, save = True)argumentoptionaldefaultavailablefilenameyes"temp.png"[ string type ]drawyesTrueTrue, FalsesaveyesTrueTrue, Falseself.start(key = None, message = None, overwrite = True, quantity = 1)argumentoptionaldefaultavailablekeyyesNoneNone, [ pynput-key type ]messageyesNoneNone, [ string type ]overwriteyesTrueTrue, Falsequantityyes1[ integer (>0) type ](2-J)ImageResizer- Resizing an image while keeping its aspect ratio:ImageResizer(image)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]self.resize(width, height)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type ]ImageResizer.resize_multiple(imageList, width, height)argumentoptionaldefaultavailableimageListno[ numpy-uint8 type ], [ list-numpy-uint8 type ]widthno[ integer (>0) type ]heightno[ integer (>0) type ](2-K)ImageCorruptDisposer- Disposing corrupt images in a folder:ImageCorruptDisposer(folderName)argumentoptionaldefaultavailablefolderNameno[ string type ]self.parse(return_quantity = False)argumentoptionaldefaultavailablereturn_quantityyesFalseTrue, Falseself.dispose(extensions = "all")argumentoptionaldefaultavailableextensionsyes"all""all", [ list-string type ](2-L)PlainImageGenerator- Generating a uni-colored image:PlainImageGenerator(width, height, channels)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type ]channelsno[ integer (>0) type ]self.generate(quantity = 1)argumentoptionaldefaultavailablequantityyes1[ integer (>0) type ](2-L-1) Example of PlainImageGenerator & ImageDisplayerfrom keygim.imageUtils import ImageUtils, ImageAugmentor
PlainImageGenerator = ImageAugmentor.PlainImageGenerator
ImageDisplayer = ImageUtils.ImageDisplayer
width, height, channels = 250, 400, 3
image = PlainImageGenerator(width, height, channels).generate()
ImageDisplayer(image).display_for(interval = 4)(2-M)NoiseImageGenerator- Generating a static/noise image:NoiseImageGenerator(width, height, channels)argumentoptionaldefaultavailablewidthno[ integer (>0) type ]heightno[ integer (>0) type ]channelsno[ integer (>0) type ]self.generate(quantity = 1)argumentoptionaldefaultavailablequantityyes1[ integer (>0) type ](2-M-1) Example of NoiseImageGenerator & ImageDisplayerfrom keygim.imageUtils import ImageUtils, ImageAugmentor
NoiseImageGenerator = ImageAugmentor.NoiseImageGenerator
ImageDisplayer = ImageUtils.ImageDisplayer
width, height, channels = 250, 400, 3
image = NoiseImageGenerator(width, height, channels).generate()
ImageDisplayer(image).display_for(interval = 4)(2-N)MapGenerator- Generating a map image from two images:MapGenerator(image1, image2)argumentoptionaldefaultavailableimage1no[ numpy-uint8 type ]image2no[ numpy-uint8 type ]self.get()(2-O)PositionShifter- Shifting an image to the side:PositionShifter(image, safeMode = True)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]safeModeyesTrueTrue, Falseself.shift(width_offset_range, height_offset_range, quantity = 1, rate = 1)argumentoptionaldefaultavailablewidth_offset_rangeno[ tuple-integer type ]height_offset_rangeno[ tuple-integer type ]quantityyes1[ integer (>0) type ]rateyes1[ integer/float [0, 1] type ](2-P)BrightnessShifter- Changing an image's brightness level:BrightnessShifter(image, safeMode = True)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]safeModeyesTrueTrue, Falseself.shift(brightness_shift_range, quantity = 1, rate = 1)argumentoptionaldefaultavailablebrightness_shift_rangeno[ tuple-float [0, 1] type ]quantityyes1[ integer (>0) type ]rateyes1[ integer/float [0, 1] type ](2-Q)ImageZoomer- Zooming in and out from an image:ImageZoomer(image, safeMode = True)argumentoptionaldefaultavailableimageno[ numpy-uint8 type ]safeModeyesTrueTrue, Falseself.zoom(zoom_value)argumentoptionaldefaultavailablezoom_valueno[ integer/float (>0) type ]self.random_zoom(zoom_range, quantity = 1, rate = 1)argumentoptionaldefaultavailablezoom_rangeno[ tuple-float (>0) type ]quantityyes1[ integer (>0) type ]rateyes1[ integer/float [0, 1] type ](2-R)ImageGenerator- Generating training data to recognize a pattern:ImageGenerator(image1, image2)argumentoptionaldefaultavailableimage1no[ numpy-uint8 type ]image2no[ numpy-uint8 type ]self.configure(verbose = True, **kwargs)argumentoptionaldefaultavailableverboseyesTrueTrue, Falseposition_shift_rangeyes((-1, 1), (-1, 1))[ tuple-tuple-integer type ]zoom_rangeyes(1.0, 1.0)[ tuple-integer/float (>0) type ]brightness_rangeyes(1.0, 1.0)[ tuple-integer/float [0, 1] type ]position_shift_rateyes1[ integer/float (>=0) type ]brightness_rateyes1[ integer/float (>=0) type ]zoom_rateyes1[ integer/float (>=0) type ]plain_quantityyes1[ integer (>=0) type ]noise_quantityyes0[ integer (>=0) type ]background_quantityyes1[ integer (>=0) type ]region_quantityyes0[ integer (>=0) type ]background_folderyesNoneNone, [ string type ]self.summary()self.generate(return_backgrounds = False, verbose = True)argumentoptionaldefaultavailablereturn_backgroundsyesFalseTrue, FalseverboseyesTrueTrue, Falseself.generate_backgrounds(verbose = True)argumentoptionaldefaultavailableverboseyesTrueTrue, False