def _caption_seinfeld(self): caption = self.text drawing = Drawing() drawing.font = 'assets/fonts/NimbusSanL-RegIta.otf' drawing.font_size = self.width / 14 text = fill(caption, 25) drawing.fill_color = Color('#000') drawing.translate(10, self.height / 12) # the number of shadows depends on image width, with a min of 1 shadows = max(int(self.width / 150), 1) for _ in range(shadows): drawing.translate(-1, 1) drawing.gravity = 'south' drawing.text(0, 0, text) drawing.fill_color = Color('#fff') drawing.text(0, 0, text) return caption, drawing
def _caption_sanandreas(self): caption = self.text drawing = Drawing() drawing.font = 'assets/fonts/TwCenMTStd-ExtraBold.otf' drawing.font_size = self.width / 20 drawing.text_interline_spacing = drawing.font_size / 5 drawing.fill_opacity = 0.8 drawing.gravity = 'south' text = fill(caption, 30) drawing.fill_color = Color('#000') offset = drawing.font_size / 12 drawing.translate(offset, self.height / 15) drawing.text(0, 0, text) drawing.translate(-offset, offset) drawing.fill_color = Color('#eee') drawing.text(0, 0, text) return caption, drawing
def _caption_akari(self): caption = 'わぁい{0} あかり{0}大好き'.format(self.text) drawing = Drawing() drawing.font = 'assets/fonts/rounded-mgenplus-1c-bold.ttf' drawing.font_size = self.width / 15 text = fill(caption, 23) drawing.gravity = 'south' drawing.text_interline_spacing = drawing.font_size / -5 offset = max(self.width / 400, 2) # first the shadow drawing.translate(offset, -offset) drawing.fill_color = Color('#000') drawing.fill_opacity = 0.5 drawing.text(0, 0, text) # then the text drawing.translate(-offset, offset) drawing.fill_color = Color('#fff') drawing.fill_opacity = 1.0 drawing.stroke_color = Color('#000') drawing.stroke_width = max(self.width / 600, 1) drawing.text(0, 0, text) return caption, drawing
def process(self, area: os.DirEntry, city: os.DirEntry, pic: os.DirEntry, save_dir: str): # os.DirEntry.name is the picture's filename filename = pic.name # Store the absolute path of the current directory # so that we can return to it when we're done, # so that the calling function doesn't end up # with a working directory that is not the same # as the one it had before the call to process() return_dir = os.path.abspath(os.curdir) # os.path.splitext gives us both the file name and # the extension of the picture. We need the extension # because we're going to use it to tell Wand # (and, in turn, ImageMagick) what extension # to give to the image and we want to retain # the original one (name, extension) = os.path.splitext(pic) text = f'{city.name}({area.name})' save_name = f'{self.i}-{text}{extension}' if self.edit: with Image(filename=filename) as image: draw = Drawing() draw.fill_color = "white" draw.font_size = 90 draw.gravity = "south_east" draw.text(30, 30, text) draw.draw(image) os.chdir(save_dir) image.save(filename=save_name) self.i = self.i + 1 else: copyfile(pic.name, f"{save_dir}/{save_name}") os.chdir(return_dir)
def generate(count): # ---------------get three colors------------------------------- 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] print(randomScene) randomSceneImage = Image(filename=randomScene) widthRange = randomSceneImage.size[0] - 100 heightRange = randomSceneImage.size[1] - 32 randomSceneImage.crop(left=random.randint(0, widthRange), top=random.randint(0, heightRange), width=100, height=32) # randomSceneImage.save(filename='.\\photoWand\\'+str(j+1) + '_texture.jpg') # -------------------------------------------------------------- # ----------create the base layer, base texture +base color----- baseImage = Image( width=100, height=32, background=Color('rgb(' + str(color1[0]) + ',' + str(color1[1]) + ',' + str(color1[2]) + ')')) # print('base_color = ' + 'rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')') baseImage.composite_channel(channel='undefined', image=randomSceneImage, operator='blend', left=0, top=0) baseImage.gaussian_blur(4, 10) baseImage.resolution = (96, 96) # -------------------------------------------------------------- # -----generate font-------------------------------------------- word = randomWords() fonts = pathwalk('./fonts/font_en/') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1] initialPointsize = 45 draw = Drawing() draw.font = randomFont 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) draw.rotate(rotateX) # -------------------------------------------------------------- # --------get suitable FontPointSize---------------------------- draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) while metric.text_width > 100 or metric.text_height > 36: initialPointsize -= 5 draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline' # -------------------------------------------------------------- # ----------gravity--------------------------------------------- draw.gravity = 'center' # -------------------------------------------------------------- # --------------shadow/border----------------------------------- if random.random() < 0.5: # shadow addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) draw.fill_color = Color('black') draw.text(x=abs(int(addx)), y=abs(int(addy)), body=word) else: # border draw.stroke_color = Color('rgb(' + str(color3[0]) + ',' + str(color3[1]) + ',' + str(color3[2]) + ')') draw.stroke_width = math.ceil(initialPointsize / 10) - 1 # -------------------------------------------------------------- # ----------print word------------------------------------------ draw.fill_color = Color('rgb(' + str(color2[0]) + ',' + str(color2[1]) + ',' + str(color2[2]) + ')') draw.text(x=0, y=0, body=word) draw.draw(baseImage) # -------------------------------------------------------------- # ------------gray---------------------------------------------- baseImage.colorspace = 'gray' # -------------------------------------------------------------- print(word) baseImage.save(filename='./photo_en/' + str(count + 1) + '_' + word + '.jpg')
draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline' # -------------------------------------------------------------- # ----------gravity--------------------------------------------- draw.gravity = 'center' # -------------------------------------------------------------- # --------------shadow/border----------------------------------- if random.random() < 0.5: # shadow addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) draw.fill_color = Color('black') draw.text(x=abs(addx), y=abs(addy), body=word) else: # border draw.stroke_color = Color('rgb('+str(color3[0])+','+str(color3[1])+','+str(color3[2])+')') draw.stroke_width = math.ceil(initialPointsize/10)-1 # --------------------------------------------------------------
draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline' # -------------------------------------------------------------- # ----------gravity--------------------------------------------- draw.gravity = 'center' # -------------------------------------------------------------- # --------------shadow/border----------------------------------- if random.random() < 0.5: # shadow addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) draw.fill_color = Color('black') draw.text(x=abs(addx), y=abs(addy), body=word) else: # border draw.stroke_color = Color('rgb(' + str(color3[0]) + ',' + str(color3[1]) + ',' + str(color3[2]) + ')') draw.stroke_width = math.ceil(initialPointsize / 10) - 1
def generate(count): # ---------------get three colors------------------------------- 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] print(randomScene) randomSceneImage = Image(filename=randomScene) widthRange = randomSceneImage.size[0] - 100 heightRange = randomSceneImage.size[1] - 32 randomSceneImage.crop(left=random.randint(0, widthRange), top=random.randint(0, heightRange), width=100, height=32) # randomSceneImage.save(filename='.\\photoWand\\'+str(j+1) + '_texture.jpg') # -------------------------------------------------------------- # ----------create the base layer, base texture +base color----- baseImage = Image(width=100, height=32, background=Color('rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')')) # print('base_color = ' + 'rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')') baseImage.composite_channel(channel='undefined', image=randomSceneImage, operator='blend', left=0, top=0) baseImage.gaussian_blur(4, 10) baseImage.resolution = (96, 96) # -------------------------------------------------------------- # -----generate font-------------------------------------------- word = randomWords() fonts = pathwalk('./fonts/font_en/') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1] initialPointsize = 45 draw = Drawing() draw.font = randomFont 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) draw.rotate(rotateX) # -------------------------------------------------------------- # --------get suitable FontPointSize---------------------------- draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) while metric.text_width > 100 or metric.text_height > 36: initialPointsize -= 5 draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline' # -------------------------------------------------------------- # ----------gravity--------------------------------------------- draw.gravity = 'center' # -------------------------------------------------------------- # --------------shadow/border----------------------------------- if random.random() < 0.5: # shadow addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) draw.fill_color = Color('black') draw.text(x=abs(int(addx)), y=abs(int(addy)), body=word) else: # border draw.stroke_color = Color('rgb('+str(color3[0])+','+str(color3[1])+','+str(color3[2])+')') draw.stroke_width = math.ceil(initialPointsize/10)-1 # -------------------------------------------------------------- # ----------print word------------------------------------------ draw.fill_color = Color('rgb('+str(color2[0])+','+str(color2[1])+','+str(color2[2])+')') draw.text(x=0, y=0, body=word) draw.draw(baseImage) # -------------------------------------------------------------- # ------------gray---------------------------------------------- baseImage.colorspace = 'gray' # -------------------------------------------------------------- print(word) baseImage.save(filename='./photo_en/'+str(count+1)+'_'+word+'.jpg')