def alphaMask( base=str(), layer=str(), mask=str(), output=str(), outputDir=None, extension='png', ): """ Take two image files and a layer mask. Convert layer file and mask file to the same size as base. Ensure that layer and base are RGBa or RGB, and the same. Ensure that mask is greyscale. Apply the mask to the layer over the base, and save output. """ success = bool() with imgOpen(base) as imgBase, imgOpen(layer) as imgLayer, imgOpen( mask) as imgMask: ## Ensure layer and base are the same mode and size, and mask is also the same size if imgBase.mode != imgLayer.mode: # Convert layer mode to base mode imgLayer = imgLayer.convert(imgBase.mode) if imgBase.size != imgLayer.size: # Rescale layer to match base imgLayer = imgLayer.resize(imgBase.size) if imgBase.size != imgMask.size: # Rescale mask to match base imgMask = imgMask.resize(imgBase.size) if outputDir: try: composite( imgBase, imgLayer, imgMask.convert(GRAYSCALE_MODE), ).save(outputDir + '/' + output + "." + extension, ) success = True except: success = False else: try: composite( imgBase, imgLayer, imgMask.convert('L'), ).save(output + "." + extension, ) success = True except: success = False return success
def collectPredictionsForTheImage(model,imagePath:str,stepX:int,stepY:int): ''' runs through the 30x30 grids of the image and predicts for each of them name of the ,model included in the pathand the extension / name of the image included in the path and the extension returns an array of predictions of the image (percentage for each class) ''' #model = keras.models.load_model(modelPath) im = asarray((imgOpen(imagePath)).convert("RGB").resize((CONST.rescaleImageX,CONST.rescaleImageY))) x=0 y=0 if CONST.useDataset1: outPredictions = []#the predictions arraays will be stored here else: outPredictions = empty((CONST.numOfColours,CONST.rescaleImageX//stepX,CONST.rescaleImageY//stepY)) while x<CONST.rescaleImageX: while y<CONST.rescaleImageY: predicted = model.predict(expand_dims(im[x:x+stepX,y:y+stepY],axis=0)) if CONST.useFourMachinesFromPart1: outPredictions.append(predicted[0].tolist()) else: for c,p in enumerate(predicted[0]): outPredictions[c][x//30][y//30] = p y+=stepY x+=stepX y=0 return outPredictions
def differenceLayer( base=str(), layer=str(), output=str(), outputDir=None, extension='png', ): """ Take two image files. Convert the layer to the same size as base. Ensure that layer and base are same type (RGB, or RGBa). Difference layer base and layer, and save output. """ success = bool() with imgOpen(base) as imgBase, imgOpen(layer) as imgLayer: ## Ensure layer and base are the same mode and size if imgBase.mode != imgLayer.mode: # Convert layer mode to base mode imgLayer = imgLayer.convert(imgBase.mode) if imgBase.size != imgLayer.size: # Rescale layer to match base imgLayer = imgLayer.resize(imgBase.size) ## Apply effect and save image if outputDir: try: difference( imgBase, imgLayer, ).save(outputDir + '/' + output + "." + extension, ) success = True except: success = False else: try: difference( imgBase, imgLayer, ).save(output + "." + extension, ) return True except: return False
def frameLightCycle( base=str(), frame=int(), cycle=int(), output=str(), extension='png', forceGray=True, ): """ Take an image, a frame number, and a cycle value. cycle the light value of pixels in the frame based on the frame number. The cycle value indicates how many frames before the light cycle come full circle Return True if a new frame is saved. MODIFY ARGUMENTS BASED ON PREVIOUS FRAME FUNCTIONS i.e. output information """ # Load the Images with imgOpen(base) as imgBase, imgNew( imgBase.mode, imgBase.size, ) as imgOutput: # Iterate through base pixels and light cycle based on a ratio of frame to cycle for imgX in range(imgBase.size[X_INDEX]): for imgY in range(imgBase.size[Y_INDEX]): if forceGray: # MAX_BYTE_VALUE , MAX_BYTE_MODULUS imgBase = imgBase.convert(GRAYSCALE_MODE) imgOutput = imgOutput.convert(GRAYSCALE_MODE) imgOutput.putpixel( #print( frame , cycle , ( frame / cycle ) * MAX_BYTE_VALUE , (imgX, imgY), int( imgBase.getpixel((imgX, imgY), ) + ((frame / cycle) * MAX_BYTE_VALUE)) % MAX_BYTE_MODULUS, ) else: imgOutput.putpixel( # Pixel position (imgX, imgY), lightCycle( imgBase.getpixel((imgX, imgY), ), frame, cycle, ), ) # Save the new image try: imgOutput.save( output + "." + extension, compress_level=0, ) return True except: return False return False
def hueLayer( base=str(), layer=str(), output=str(), outputDir=None, extension='png', ): """ Create a hue layer effect. Convert layer to same size as base, and greyscale Create a new image the same size and mode as base. Iterate through each pixel in base, and corrisponding pixel in layer. Take the pixel value of layer, and add it to hue of base (modulus 1), then draw the RGB value to the new output image. Also, apply alpha layer of base to output image if base mode is RBGa. """ with imgOpen(base) as imgBase, imgOpen(layer) as imgLayer, imgNew( imgBase.mode, imgBase.size, ) as imgOutput: ## Ensure layer and base are the same mode and size if imgBase.mode != imgLayer.mode: # Convert layer mode to base mode imgLayer = imgLayer.convert(imgBase.mode) if imgBase.size != imgLayer.size: # Rescale layer to match base imgLayer = imgLayer.resize(imgBase.size) # Iterate through the base layers pixels # Look at numpy, and other ways to do this efficiently for imgX in range(imgBase.size[X_INDEX]): for imgY in range(imgBase.size[Y_INDEX]): # Grab base pixel RGB values at ( imgX , imgY ) and convert to HSV if len(imgBase.mode) == MODE_RGB_LENGTH: imgOutput.putpixel( # Image pixel position (imgX, imgY), # Image pixel RGB huePixel( imgBase.getpixel((imgX, imgY), ), imgLayer.getpixel((imgX, imgY), ), ), ) elif len(imgBase.mode) == MODE_RGBA_LENGTH: imgOutput.putpixel( # Image pixel position (imgX, imgY), # Image pixel RGB huePixel( imgBase.getpixel((imgX, imgY), ), imgLayer.getpixel((imgX, imgY), ), ), ) elif len(imgBase.mode) == MODE_RGBA_LENGTH: pass # RGBA image, also write alpha layer from base else: # Wrong base image type, return False # consider better result for error logging in scripts that # call this module return False # Grab layer HSV[ value ] value at ( imgX , imgY ) # Write an RGB value at (imgX , imgY ) to output. # output HSV[ hue ] = base HSV[ hue ] + layer[ value ] # convert output HSV to RGB and write pixel try: imgOutput.save(output + "." + extension, ) return True except: return False