class TextPen(Pen): '''For draw one line text on canvas ''' font = PenParameter("font") font_size = PenParameter("font_size") text_interline_spacing = PenParameter("text_interline_spacing") fill_color = PenParameter("fill_color", export_filter=lambda c:c and Color(c)) coordinates = PenParameter( 'coordinates', validator=coordinates_validator, export_filter=Coordinates ) text = PenParameter( "text", validator=bool, export_filter=lambda text: text.decode('utf8') \ if isinstance(text, str) else text ) def __init__(self, config): super(TextPen, self).__init__(config) self.drawer = Drawing() for name, parameter in self.known_parameter.iteritems(): if name not in ("text", "coordinates"): value = getattr(self, name) if value: setattr(self.drawer, name, value) def draw(self, canvas): self.drawer.text( self.coordinates.x, self.coordinates.y, self.text.encode('utf8') ) self.drawer.draw(canvas.image)
def draw_watermark(self, img): watermark_draw = Drawing() watermark_draw.stroke_color = Color('black') watermark_draw.font = os.path.join(os.path.dirname(__file__), "Colombia-Regular.ttf") watermark_draw.font_size = 3 * POINT_PER_MM watermark_draw.text_antialias = True metrics = watermark_draw.get_font_metrics(img, WATERMARK) watermark_draw.text(max(0, math.floor((POINT_PER_MM * self.paper['width']) - metrics.text_width)), max(0, math.floor((POINT_PER_MM * self.paper['height']) - metrics.text_height * 2)), WATERMARK) watermark_draw.draw(img)
def create_line_img(x_start, x_finish, brush_width, params, img, filename): step = 1.01 draw_img = Drawing() draw_img.stroke_color = Color("white") draw_img.fill_color = Color("white") draw_img.stroke_width = brush_width if x_start > x_finish: buf = x_start x_start = x_finish x_finish = buf x = x_start + step for i in range(3): while x < x_finish: x1 = x - step y1 = chosen_func(x1, params) y2 = chosen_func(x, params) draw_img.line((x1, y1), (x, y2)) x = x + 2 * step step -= 0.5 x = x_start + step draw_img.draw(img) img.save(filename=filename)
class WandPanelDisplay(PanelDisplay): def __init__(self, width=600, height=400): super().__init__("Graphics Panel Display", width, height) self.draw = Drawing() def drawRectangle(self, r, outline_colour="black", outline_width=1, fill=None): self.draw.stroke_color = Color(outline_colour) self.draw.stroke_width = outline_width self.draw.rectangle(left=r.tl.x, top=r.tl.y, right=r.br.x, bottom=r.br.y) def show(self): with Image(width=self.width, height=self.height, background=Color('lightblue')) as img: self.draw.draw(img) img.save(filename='draw-panel.svg')
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')
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 # -------------------------------------------------------------- # ----------print word------------------------------------------ draw.fill_color = Color('rgb('+str(color2[0])+','+str(color2[1])+','+str(color2[2])+')') # print('font_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='.\\photoWand\\'+str(j+1)+'.jpg') time2 = time.time() print(time2-time1) # img = Image(width=200, height=200, background=Color('red')) # draw = Drawing()
from wand.drawing import Drawing
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 # -------------------------------------------------------------- # ----------print word------------------------------------------ draw.fill_color = Color('rgb(' + str(color2[0]) + ',' + str(color2[1]) + ',' + str(color2[2]) + ')') # print('font_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='.\\photoWand_cn\\' + str(j + 1) + '.jpg') time2 = time.time() print(time2 - time1) # img = Image(width=200, height=200, background=Color('red')) # draw = Drawing() # draw.font = 'D:\code\MC lab\src\\rewrite python\GeneratePhoto\\fonts\\font_en\\cambria.ttc'
from wand.drawing import Drawing from wand.image import Image if len(sys.argv) < 3: print('{0} <ORIGINAL PATH> <TEXT>'.format(sys.argv[0])) sys.exit() original_path = sys.argv[1] text = sys.argv[2] with Image(filename=original_path) as image: with image.clone() as clone: with Drawing() as draw: draw.stroke_color = Color('#FF0000') draw.fill_color = Color('#FFFFFFA0') #RRGGBBAA(투명도) draw.rectangle(left=10, top=15, width=220, height=55) #사각형 그리기 draw.stroke_color = Color('#00FF00') draw.stroke_width = 2 draw.line((10, 5), (230, 5)) #직선 그리기 draw.line((10, 80), (230, 80)) draw.stroke_color = Color('#000000FF') draw.fill_color = Color('#000000') draw.font = 'C:\Windows\Fonts\JOKERMAN.TTF' draw.font_size = 30 draw.text(20, 51, text) #텍스트 출력(좌표, 텍스트) Drawing.draw(draw, clone) #draw(clone)와 동일. 이미지에 draw의 작업결과 출력 clone.save(filename='watermark_' + original_path)
n, srcTotal-n, mergedTiles, copiedTiles) interval+=1 if i in targetFiles: # Backup image if keepOriginals=="true": shutil.copy(target+"/"+i, target+"/"+i+".orig") else: pass # Merge images with Wand / ImageMagick sourceImg = Image(filename=src+"/"+i) targetImg = Image(filename=target+"/"+i) draw = Drawing() draw.composite(image=sourceImg, operator='src_over', left=0, top=0, \ width=sourceImg.width, height=sourceImg.height) draw.draw(targetImg) targetImg.save(filename=target+"/"+i) mergedTiles = mergedTiles+1 else: # Recreate path if needed try: os.makedirs(target+"/".join(i.split("/")[:-1])) except: pass shutil.copy(src+"/"+i, target+"/"+i) copiedTiles = copiedTiles+1 n+=1 elapsed_time = time.time()-start_time
def draw_box(self, progress_tracker=None): if not self.check_paper_layout(): return None tuckbox_dimensions = ['width', 'height', 'depth'] paper_dimensions = ['width', 'height'] if ((not all(dimension in self.tuckbox for dimension in tuckbox_dimensions)) or (not all(dimension in self.paper for dimension in paper_dimensions))): return None # How much white space on the sides margin_height = (self.paper['height'] - self.pattern_height()) / 2 margin_width = (self.paper['width'] - self.pattern_width()) / 2 # Main box draw draw = Drawing() draw.scale(POINT_PER_MM, POINT_PER_MM) draw.stroke_color = Color('black') draw.stroke_width = RESOLUTION / (200 * POINT_PER_MM) draw.fill_opacity = 0 draw.translate(margin_width, margin_height + self.lip_size() + self.tuckbox['depth']) draw.push() # Finger holds draw finger_draw = Drawing(draw) finger_draw.fill_opacity = 1 finger_draw.fill_color = Color('white') finger_draw.push() # Dashed draw dashed_draw = Drawing(draw) dash_array = [min(self.tuckbox['depth'], self.tuckbox['width'], self.tuckbox['height'])/13] * 2 dashed_draw.stroke_color = Color('rgb(100,100,100)') dashed_draw.fill_opacity = 0 dashed_draw.stroke_width = RESOLUTION / (300 * POINT_PER_MM) dashed_draw.stroke_dash_array = dash_array dashed_draw.stroke_dash_offset = 1 # Folding guides draw folding_guides_draw = Drawing() folding_guides_draw.scale(POINT_PER_MM, POINT_PER_MM) folding_guides_draw.stroke_color = Color('black') folding_guides_draw.stroke_width = RESOLUTION / (200 * POINT_PER_MM) back_draw = Drawing(draw) back_dashed_draw = Drawing(dashed_draw) back_folding_guides_draw = Drawing(folding_guides_draw) if progress_tracker is not None: progress_tracker(5) two_openings = 'two_openings' in self.options and self.options['two_openings'] two_pages = 'two_pages' in self.options and self.options['two_pages'] dash_array = [min(self.tuckbox['depth'], self.tuckbox['width'], self.tuckbox['height'])/13] * 2 # # Draw the box (or the first page) # self.draw_front(draw, dashed_draw, two_openings) if two_pages: offset_left_to_back = self.tuckbox['depth'] else: offset_left_to_back = self.tuckbox['depth']*2 + self.tuckbox['width'] self.draw_back(offset_left_to_back, back_draw, back_dashed_draw, finger_draw, two_openings) if two_pages: back_draw.polyline([(self.tuckbox['depth'], 0), (self.tuckbox['depth'] * 0.2, 0), (self.tuckbox['depth'] * 0.2, self.tuckbox['height']), (self.tuckbox['depth'], self.tuckbox['height'])]) draw.line((self.tuckbox['depth']*2 + self.tuckbox['width'], 0), (self.tuckbox['depth']*2 + self.tuckbox['width'], self.tuckbox['height'])) if progress_tracker is not None: progress_tracker(10) # Create the image image = Image(width=math.ceil(self.paper['width'] * POINT_PER_MM), height=math.ceil(self.paper['height'] * POINT_PER_MM), background=Color('white')) image.resolution = RESOLUTION image.unit = 'pixelsperinch' if two_pages: image2 = Image(width=math.ceil(self.paper['width'] * POINT_PER_MM), height=math.ceil(self.paper['height'] * POINT_PER_MM), background=Color('white')) image2.resolution = RESOLUTION image2.unit = 'pixelsperinch' else: image2 = image # Draw the faces self.draw_faces(image, progress_tracker, with_back = not two_pages) if two_pages: self.draw_faces(image2, progress_tracker, only_back=True) # Draw the lip(s) lip = self.draw_lip(top=True) if lip is not None: image.composite(lip, math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height) * POINT_PER_MM)) if two_openings: lip = self.draw_lip(bottom=True) if lip is not None: image.composite(lip, math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.tuckbox['depth']*2 + self.lip_size() + self.tuckbox['height']) * POINT_PER_MM)) if progress_tracker is not None: progress_tracker(80) # Draw all the lines over draw.draw(image) back_draw.draw(image2) finger_draw.draw(image2) self.draw_watermark(image) if two_pages: self.draw_watermark(image2) if progress_tracker is not None: progress_tracker(90) if "folds_dashed" in self.options and self.options["folds_dashed"]: dashed_draw.draw(image) back_dashed_draw.draw(image2) if "folding_guides" in self.options and self.options["folding_guides"]: front_vertical_folds = [margin_width + self.tuckbox['depth'], margin_width + self.tuckbox['depth'] + self.tuckbox['width']] if not two_pages: front_vertical_folds.extend([margin_width + self.tuckbox['depth']*2 + self.tuckbox['width'], margin_width + self.tuckbox['depth']*2 + self.tuckbox['width']*2]) back_vertical_folds = [margin_width + self.tuckbox['depth'], margin_width + self.tuckbox['depth'] + self.tuckbox['width']] front_horizontal_folds = [margin_height + self.lip_size(), margin_height + self.lip_size() + self.tuckbox['depth'], margin_height + self.lip_size() + self.tuckbox['depth'] + self.tuckbox['height']] if two_openings: front_horizontal_folds.append(margin_height + self.lip_size() + self.tuckbox['depth'] * 2 + self.tuckbox['height']) back_horizontal_folds = [] else: back_horizontal_folds = [margin_height + self.lip_size() + self.tuckbox['depth'] + self.tuckbox['height']] self.draw_folds(folding_guides_draw, front_vertical_folds, front_horizontal_folds, margin_height, margin_width) self.draw_folds(back_folding_guides_draw, back_vertical_folds, back_horizontal_folds, margin_height, margin_width) folding_guides_draw.draw(image) back_folding_guides_draw.draw(image2) if progress_tracker is not None: progress_tracker(100) if not two_pages: image2 = None return image, image2
def draw_lip(self, top=False, bottom=False): if "back" not in self.faces: return None if not (top ^ bottom): # 1 and only 1 of top or bottom should be true return None # First draw a full mask with the lip shape lip_full_mask_image = Image(width=math.ceil(self.tuckbox['width'] * POINT_PER_MM), height=math.ceil(self.lip_size() * POINT_PER_MM)) lip_full_draw = Drawing() lip_full_draw.scale(POINT_PER_MM, POINT_PER_MM) # This cannot be too "thin" or the floodfill later would spill over lip_full_draw.stroke_width = max(2 / POINT_PER_MM, RESOLUTION / (200 * POINT_PER_MM)) lip_full_draw.fill_color = Color('white') lip_full_draw.color(0, 0, 'reset') lip_full_draw.draw(lip_full_mask_image) lip_full_draw.stroke_color = Color('black') # 1/2 left of lip lip_full_draw.bezier([(0, self.lip_size()), (0, self.lip_size() - .75*self.lip_size()), (.2 * self.tuckbox['width'], self.lip_size() - self.lip_size()), (.5 * self.tuckbox['width'], self.lip_size() - self.lip_size())]) # 1/2 right of lip lip_full_draw.bezier([(self.tuckbox['width'], self.lip_size()), (self.tuckbox['width'], self.lip_size() - .75*self.lip_size()), (.8 * self.tuckbox['width'], self.lip_size() - self.lip_size()), (.5 * self.tuckbox['width'], self.lip_size() - self.lip_size())]) lip_full_draw.draw(lip_full_mask_image) lip_full_draw.fill_color = Color('black') lip_full_draw.border_color = Color('black') lip_full_draw.color(.5 * self.tuckbox['width'], 0.8*self.lip_size(), 'filltoborder') lip_full_draw.draw(lip_full_mask_image) if self.faces['back'][:1] == "#": lip_image = Image(width = lip_full_mask_image.width, height = lip_full_mask_image.height, background = Color(self.faces['back'])) else: # Prepare the front image angle = 180 if "back_angle" not in self.options else (self.options["back_angle"]+2)*90 if bottom: angle = (angle + 180) % 360 _, file_extension = os.path.splitext(self.faces['back']) tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) self.resize_rotate_image(self.faces['back'], tmp_file.name, angle, math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)) lip_image = Image(filename=tmp_file.name) lip_image.crop(top=lip_image.height - lip_full_mask_image.height) # u = image pixel # h = height # j = row # Radius of the hold is {self.tuckbox['width']*.1} # if value is 1 (or more_, it's white # Top is the tip of the lip, bottom is attached to the box # finger_hold_size_save is the row # at which it should be no attenuation below finger_hold_size_save = str( int(lip_image.height - math.ceil(self.tuckbox['width'] * POINT_PER_MM * 0.1))) lip_image = lip_image.fx( "j>"+finger_hold_size_save+"?u:1+(u-1)*(j/"+finger_hold_size_save+")") lip_image.composite(operator='lighten', image=lip_full_mask_image) if bottom: lip_image.rotate(180) return lip_image
def draw_faces(self, image, progress_tracker, with_back=True, only_back=False): margin_height = (self.paper['height'] - self.pattern_height()) / 2 margin_width = (self.paper['width'] - self.pattern_width()) / 2 face_sizes = { "front": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "back": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "left": (math.ceil(self.tuckbox['depth'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "right": (math.ceil(self.tuckbox['depth'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "top": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['depth'] * POINT_PER_MM)), "bottom": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['depth'] * POINT_PER_MM)), } face_positions = { "front": (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "back": (math.floor((margin_width + self.tuckbox['depth']*2 + self.tuckbox['width']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "left": (math.floor(margin_width * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "right": (math.floor((margin_width + self.tuckbox['depth'] + self.tuckbox['width']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "top": (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size()) * POINT_PER_MM)), "bottom": (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth'] + self.tuckbox['height']) * POINT_PER_MM)), } if only_back: faces = ["back"] # We are drawing the second page with the back face only, making some assumptions on its position face_positions["back"] = (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)) else: faces = list(face_sizes.keys()) if not with_back: faces.remove("back") face_angles = {} for face in faces: face_angles[face] = self.options[face + "_angle"] if face+"_angle" in self.options else 0 # Apply those face pictures for counter, side in enumerate(faces): if side in self.faces: if self.faces[side][:1] == "#": # we are filling with color color_face_draw = Drawing() color_face_draw.stroke_width = 0 color_face_draw.fill_color = Color(self.faces[side]) color_face_draw.rectangle(left = face_positions[side][0], top = face_positions[side][1], width = face_sizes[side][0], height = face_sizes[side][1]) color_face_draw.draw(image) else: _, file_extension = os.path.splitext(os.path.basename(self.faces[side])) tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) self.resize_rotate_image(self.faces[side], tmp_file.name, face_angles[side] * 90, *face_sizes[side]) with Image(filename=tmp_file.name) as i: image.composite(i, *face_positions[side]) if progress_tracker is not None: progress_tracker(10*(counter+2))
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')