def fill_image_with_random_pixels(filename: str) -> None: random.seed(0) image = PythonMagick.Image(filename) for x in range(0, image.size().width()): for y in range(0, image.size().height()): color = PythonMagick.Color(random_short(), random_short(), random_short()) image.pixelColor(x, y, color) image.write(filename)
paths.append([root, fn]) return paths time1 = time.time() for i in range(10): word = python2access.randomWords() # ---------------generate background color---------------------------- redQuantum = random.randint(0, 65525) greenQuantum = random.randint(0, 65535) blueQuantum = random.randint(0, 65535) # -------------------------------------------------------------------- color = PythonMagick.Color(redQuantum, greenQuantum, blueQuantum) # -----------------create photo----------------------- img = PythonMagick.Image('100x32', color) img.xResolution = 96 img.yResolution = 96 # ---------------------------------------------------- # ---------------------generate font------------------------------------------------------------------- fonts = pathwalk('D:\code\MC lab\\usingImageMagick\\fonts\\font_en\\') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1] initialPointsize = 40 img.font(randomFont)
randomSceneImage = PythonMagick.Image(randomScene) randomSceneGeo = PythonMagick.Geometry(randomSceneImage.size()) widthRange = randomSceneGeo.width() - 100 heightRange = randomSceneGeo.height() - 32 cutGeo = PythonMagick.Geometry('100x32+' + str(random.randint(0, widthRange)) + '+' + str(random.randint(0, heightRange))) randomSceneImage.crop(cutGeo) # randomSceneImage.write('cutOutImage.jpg') # ------------------------------------------------------------------ # --------create the base layer, base texture + base color---------- baseImage = PythonMagick.Image( '100x32', PythonMagick.Color(color1[0], color1[1], color1[2])) # baseImage.write('baseColor.jpg') baseImage.composite(randomSceneImage, 0, 0, PythonMagick.CompositeOperator.BlendCompositeOp) # baseImage.write('mixture.jpg') baseImage.xResolution = 96 baseImage.yResolution = 96 baseImage.blur(4, 8) # ------------------------------------------------------------------ # -------------generate font---------------------------------------- word = python2access_cn.randomWords_cn() fonts = pathwalk('.\googleFonts\\') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1]
] # fonts = ['traffic_bold'] fontsProgBar = tqdm(total=len(fonts), desc='Fonts') for font in fonts: # Getting list of old _trim images and delete them oldProcessedImages = [ image for image in os.listdir(font) if image.endswith('_trim.png') ] for image in oldProcessedImages: os.remove(os.path.join(font, image)) # for each glyph image, remove the background and trim the image images = [image for image in os.listdir(font) if image.endswith('.png')] # Define colors white = PM.Color("#ffffff") trans = PM.Color("#00000000") glyphProgBar = tqdm(total=len(images), desc='Glyphs', leave=False) for image in images: # Read the image glyphImage = PM.Image(os.path.join(font, image)) # Remove the backgroundf glyphImage.transparent(white, False) # Trim the image glyphImage.trim() # If glyph is a Number resize it to 87 pixel height(35 for zero). # otherwise 58 pixel height. if os.path.splitext(image)[0].isnumeric(): if os.path.splitext(image)[0] == '0': glyphImage.resize('x35')
def work(): for j in range(50): colorString = random.choice(result) color = [] for i in colorString: color += [int(i)] for i in range(len(color)): color[i] = math.floor(color[i] / 255 * 65535) color1 = color[0:3] color2 = color[3:6] color3 = color[6:9] # ------------------------------------------------------------- # ---------------get the base layer texture------------------------------- Scenes = pathwalk('.\SceneData\\') randomScene = random.choice(Scenes) randomScene = randomScene[0] + randomScene[1] randomSceneImage = PythonMagick.Image(randomScene) randomSceneGeo = PythonMagick.Geometry(randomSceneImage.size()) widthRange = randomSceneGeo.width() - 100 heightRange = randomSceneGeo.height() - 32 cutGeo = PythonMagick.Geometry('100x32+' + str(random.randint(0, widthRange)) + '+' + str(random.randint(0, heightRange))) randomSceneImage.crop(cutGeo) # randomSceneImage.write('cutOutImage.jpg') # ------------------------------------------------------------------ # --------create the base layer, base texture + base color---------- baseImage = PythonMagick.Image( '100x32', PythonMagick.Color(color1[0], color1[1], color1[2])) # baseImage.write('baseColor.jpg') baseImage.composite(randomSceneImage, 0, 0, PythonMagick.CompositeOperator.BlendCompositeOp) # baseImage.write('mixture.jpg') baseImage.xResolution = 96 baseImage.yResolution = 96 baseImage.blur(4, 10) # ------------------------------------------------------------------ # -------------generate font---------------------------------------- # word = python2access.randomWords() word = 'F**K' fonts = pathwalk('.\\fonts\\font_en\\') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1] initialPointsize = 45 baseImage.font(randomFont) fontcolor = PythonMagick.Color(color2[0], color2[1], color2[2]) wordLength = len(word) tmp = int(math.floor(abs(random.gauss(0, 1)) * 6)) if random.randint(1, 2) == 1: rotateX = random.randint(0, tmp) else: rotateX = random.randint(360 - tmp, 360) # ------------------------------------------------------------------ # -------------------generate suitable FontPointsize---------------- baseImage.fontPointsize(initialPointsize) metric = PythonMagick.TypeMetric() baseImage.fontTypeMetrics(word, metric) while metric.textWidth() > 100 or metric.textHeight() > 36: initialPointsize -= 5 baseImage.fontPointsize(initialPointsize) baseImage.fontTypeMetrics(word, metric) # ------------------------------------------------------------------ # -----------------generate shadow/border---------------------------------- if random.random() > 0.5: baseImage.strokeColor( PythonMagick.Color(color3[0], color3[1], color3[2])) baseImage.strokeWidth(math.ceil(initialPointsize / 10) - 1) else: addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) if addx >= 0: addx = '+' + str(addx) else: addx = str(addx) if addy >= 0: addy = '+' + str(addy) else: addy = str(addy) # print(addx+addy) geoShadow = PythonMagick.Geometry('100x32' + addx + addy) addToPointsize = math.floor(abs(random.gauss(0, 1))) # print('addToPointsize = ', addToPointsize) baseImage.fontPointsize(initialPointsize + addToPointsize) baseImage.fillColor(PythonMagick.Color('black')) baseImage.annotate(word, geoShadow, PythonMagick.GravityType.CenterGravity, rotateX) # ------------------------------------------------------------------ # -----------------print word--------------------------------------- baseImage.fontPointsize(initialPointsize) baseImage.fillColor(fontcolor) geo = PythonMagick.Geometry('100x32') baseImage.annotate(word, geo, PythonMagick.GravityType.CenterGravity, rotateX) # ------------------------------------------------------------------ # -----------------underline---------------------------------------- if int(math.floor(abs(random.gauss(0, 1)))) > 2: underline = '' for i in range(wordLength): underline += '_' baseImage.annotate(underline, geo, PythonMagick.GravityType.CenterGravity, rotateX) # ------------------------------------------------------------------ # ------------------------GREYColorSpace---------------------------- baseImage.colorSpace(PythonMagick.ColorspaceType.GRAYColorspace) # ------------------------------------------------------------------ baseImage.magick('jpg') # print(word) baseImage.write('.\photo\\' + str(j + 1) + '.jpg')
def create_image(filename: str, width: int) -> str: geometry = PythonMagick.Geometry(int(width), int(width * 3 / 4)) color = PythonMagick.Color("Black") image = PythonMagick.Image(geometry, color) image.write(filename) return filename
def CompileFlag(sourcepath, destFolder): if not destFolder: destFolder = "output/" filename = os.path.splitext(os.path.basename(sourcepath))[0] image = PythonMagick.Image(sourcepath) imagetype = image.type nonecolor = PythonMagick.Color(1 * MaxRGB, 0, 0, 255 * MaxRGB) canvas = PythonMagick.Image(PythonMagick.Geometry(128, 128), nonecolor) canvas.type = imagetype dropshadow = PythonMagick.Image(PythonMagick.Geometry(128, 128), nonecolor) dropshadow.type = imagetype image = PythonMagick.Image(sourcepath) image2 = PythonMagick.Image(sourcepath) #image2 = PythonMagick.Image(image2) #image2 = PythonMagick.Image( PythonMagick.Geometry( image.size().width(), image.size().height()), nonecolor ) #image2.composite(image, PythonMagick.Geometry(0,0,0,0), op) #image.sample("115x73") image.transform("115x73") image.enhance() dropshadow.fillColor(PythonMagick.Color(0, 0, 0, 25 * MaxRGB)) dropshadow.draw( PythonMagick.DrawableRectangle( (128 / 2) - (115 / 2) - 1, (128 / 2) - (73 / 2) - 1, (128 / 2) + (115 / 2) + 1, (128 / 2) + (73 / 2) + 1)) dropshadow.blur(5, 5) x = int(128 / 2) y = int(128 / 2) #geom = PythonMagick.Geometry(int(128/2) - int(115/2), int(128/2) - int(73/2) , int(128/2) + int(115/2), int(128/2) + int(73/2)) geom = PythonMagick.Geometry(0, 0, int(128 / 2) - int(115 / 2), int(128 / 2) - int(73 / 2)) op = PythonMagick.CompositeOperator.OverCompositeOp dropshadow.composite(image, geom, op) dropshadow.type = imagetype dropshadow.write(destFolder + filename + ".dds") tiny = PythonMagick.Image(dropshadow) tiny.type = imagetype tiny.transform("24x24") #tiny.sample("!24x24") tiny.write(destFolder + "small/" + filename + ".dds") mapflag = PythonMagick.Image(PythonMagick.Geometry(256, 256), nonecolor) colourFrequencies = ColourSet(image2) sortedColours = [(k, colourFrequencies[k]) for k in sorted( colourFrequencies, key=colourFrequencies.get, reverse=True)] maxIntensity = 0 minIntensity = 255 for i in range(10): if i >= len(sortedColours): break sortedColour = sortedColours[i][0][1:-1].split(',') intensity = int(sortedColour[0]) + int( 1.2 * float(sortedColour[1])) + int(0.5 * float(sortedColour[2])) if intensity > maxIntensity: maxIntensity = intensity if intensity < minIntensity: minIntensity = intensity for x in range(image2.size().width()): for y in range(image2.size().height()): c = ColourToRGBArray(image2.pixelColor(x, y)) intensity = c[0] + (1.2 * float(c[1])) + (0.5 * float(c[2])) actualIntensity = (intensity - minIntensity) / (maxIntensity - minIntensity) if (actualIntensity < 0.0): actualIntensity = 0 elif (actualIntensity > 1.0): actualIntensity = 255 * MaxRGB else: actualIntensity = int(actualIntensity * 255 * MaxRGB) newColour = PythonMagick.Color( min(actualIntensity + MaxRGB, 255 * MaxRGB), actualIntensity, actualIntensity, 1 * MaxRGB) image2.pixelColor(x, y, newColour) #image2.sample("!186x118") image2.transform("186x118") dropshadow2 = PythonMagick.Image(PythonMagick.Geometry(256, 256), nonecolor) dropshadow2.type = imagetype dropshadow2.fillColor(PythonMagick.Color(0, 0, 0, 25 * MaxRGB)) dropshadow2.draw( PythonMagick.DrawableRectangle( (256 / 2) - (186 / 2) - 1, (256 / 2) - (118 / 2) - 1, (256 / 2) + (186 / 2) + 1, (256 / 2) + (118 / 2) + 1)) dropshadow2.blur(10, 10) x = int(256 / 2) y = int(256 / 2) geom = PythonMagick.Geometry(0, 0, int(256 / 2) - int(186 / 2), int(256 / 2) - int(118 / 2)) op = PythonMagick.CompositeOperator.OverCompositeOp dropshadow2.composite(image2, geom, op) dropshadow2.type = imagetype dropshadow2.fillColor(PythonMagick.Color(0, 0, 0, 255 * MaxRGB)) dropshadow2.strokeColor( PythonMagick.Color(255 * MaxRGB, 255 * MaxRGB, 255 * MaxRGB, 1 * MaxRGB)) dropshadow2.strokeWidth(2) dropshadow2.draw( PythonMagick.DrawableRectangle( (256 / 2) - (186 / 2) - 1, (256 / 2) - (118 / 2) - 1, (256 / 2) + (186 / 2) + 1, (256 / 2) + (118 / 2) + 1)) dropshadow2.write(destFolder + "map/" + filename + ".dds")