def readImageNs(self, name, i, filename, gtin): koord = [[0, 415], [420, 835], [840, 1255], [1260, 1675], [1680, 2095], [2105, 2520], [2525, 2940], [2945, 3360]] page = int(i / 8) n = i % 8 print("Page %s" % (page)) print("Image %s" % (n)) print(gtin[1:14]) with open('ean.png', 'wb') as f: EAN13('%s' % (gtin[1:13]), writer=ImageWriter()).write(f) # with Image(filename='%s[%s]' % (name, page), resolution=300) as img: if page != self.page: self.image = Image(filename='%s[%s]' % (self.pdf, page), resolution=300) self.page = page with self.image.clone() as img: img.convert('png') img.crop(0, koord[n][0], 709, koord[n][1]) draw = Drawing() ean = Image(filename="./ean.png", resolution=300).clone() draw.composite(operator="over", left=5, top=235, width=round(ean.width * 0.65), height=round(ean.height * 0.65), image=ean) draw(img) img.save(filename='%s.png' % (filename))
def combine_images(*images: List[Union[str, bytes, WandImage]]) -> Image: with ThreadPool() as pool: images = pool.map(normalize_image, images) max_width = round(mean(img.width for img in images)) max_height = round(mean(img.height for img in images)) sum_width = max_width * len(images) canvas = WandImage(width=sum_width, height=max_height) resizer = ImageResizer(max_width, max_height) left = 0 draw = Drawing() draw.fill_color = Color('white') draw.rectangle(left=0, top=0, right=canvas.width, bottom=canvas.height) draw(canvas) for img in images: if img.height < max_height: img = resizer.resize(img, max_height / img.height) if img.width < max_width: img = resizer.resize(img, max_width / img.width) elif img.width > max_width: # Do a bit of cropping so it's centered crop_left = round((img.width - max_width) / 2) crop_right = crop_left + max_width img.crop(crop_left, 0, crop_right, img.height) draw.composite(operator='over', top=0, left=left, width=img.width, height=img.height, image=img) draw(canvas) left += img.width if DEBUG_IMAGE: # pragma: no cover server_name = os.environ.get('DISPLAY', ':0') display(canvas, server_name=server_name) return Image(content=canvas.make_blob(format='png'), content_type='image/png')
def composite_with_image(self, img_back, img, left, top, save_name=None, is_display=False): draw = Drawing() draw.composite(operator='atop', left=left, top=top, width=img.width, height=img.height, image=img) draw(img_back) if is_display: display(img_back) if save_name is not None: img_back.save(filename=save_name) return img_back
def generate_background(self, tiles) : """ Displays the tiles on the background """ self.img = Image(width=self.number_of_cols*256, height=self.number_of_rows*256) current_row = 0 for row in tiles : current_col = 0 for tile in row: response = self.get_tile(tile) try: with Image(file=response) as tile_img: draw = Drawing() draw.composite(operator='add', left=current_col*256, top=current_row*256, width=tile_img.width, height=tile_img.height, image=tile_img) draw(self.img) finally: response.close() current_col += 1 current_row += 1
if (int((dt.datetime.now() - last_run).total_seconds()) > shottime): if F.IsDayLight(Settings['Latitude'],Settings['Longitude'],Settings['Elevation']): # syslog.syslog(syslog.LOG_INFO, 'Setting Day Parameters') F.SetCamera(cam,DaySettings) else: # syslog.syslog(syslog.LOG_INFO, 'Setting Night Parameters') F.SetCamera(cam,NightSettings) # syslog.syslog(syslog.LOG_INFO, 'Taking Photo') cam.capture(Settings['FTPFile']) img = Image(filename=Settings['FTPFile']) OverImg = Image(filename='/boot/TMLogo.png') draw = Drawing() draw.composite(operator='over',left=img.width - OverImg.width - 5,top=5,width=OverImg.width,height=OverImg.height,image=OverImg) draw(img) draw = Drawing() draw.fill_color = Color('blue') draw.fill_opacity = 0.5 draw.rectangle(0,img.height - 30,img.width,img.height) draw(img) draw = Drawing() draw.font = 'wandtests/assets/League_Gothic.otf' draw.font_size = 20 draw.fill_color = Color('white') draw.text_alignment = 'left' draw.text(5, img.height - 5, Settings['Description']) draw(img)
digit = images[digitIndex].clone() else: digit = imagesColored[digitIndex].clone() operator = 'darken' fontWidth = digit.width fontHeight = digit.height minFontHeight = 0.9 * fontHeight maxFontHeight = 1.3 * fontHeight minFontWidth = 0.7 * fontWidth maxFontWidth = 1.3 * fontWidth if boxHeight > minFontHeight and boxHeight < maxFontHeight and \ boxWidth > minFontWidth and boxWidth < maxFontWidth: digit.resize(boxWidth, boxHeight, 'cubic') draw.composite(operator=operator, left=boxLeft, top=boxTop, width=boxWidth, height=boxHeight, image=digit) else: if fontWidth <= minFontWidth: fontWidth = minFontWidth elif fontWidth >= maxFontWidth: fontWidth = maxFontWidth if fontHeight <= minFontHeight: fontHeight = minFontHeight elif fontHeight >= maxFontHeight: fontHeight = maxFontHeight digit.resize(int(round(fontWidth)), int(round(fontHeight)), 'cubic') draw.composite(operator=operator, left=round((boxLeft + boxRight - fontWidth) / 2.0),
break if boxIndex > 0 and not firstChar: if boxes[boxIndex]['boxLeft'] < boxes[boxIndex-1]['boxLeft'] or \ boxes[boxIndex]['boxBottom'] > boxes[boxIndex-1]['boxBottom'] + rowFloor: print 'Out of boxes in row', row, "character", character break firstChar = 0 asciiCode = ord(character) if boxes[boxIndex]['boxChar'] == character: fontChar = imagesMatch[asciiCode].clone() else: fontChar = imagesNomatch[asciiCode].clone() operator = 'darken' fontChar.resize(boxes[boxIndex]['boxWidth'], boxes[boxIndex]['boxHeight'], 'cubic') draw.composite(operator=operator, left=boxes[boxIndex]['boxLeft'], top=boxes[boxIndex]['boxTop'], width=boxes[boxIndex]['boxWidth'], height=boxes[boxIndex]['boxHeight'], image=fontChar) boxIndex += 1 if boxIndex > 0 and boxIndex < len(boxes) and \ boxes[boxIndex]['boxLeft'] >= boxes[boxIndex-1]['boxLeft'] and \ boxes[boxIndex]['boxBottom'] <= boxes[boxIndex-1]['boxBottom'] + rowFloor: print 'Extra boxes in row', row while boxIndex < len(boxes): if boxes[boxIndex]['boxLeft'] < boxes[boxIndex-1]['boxLeft'] or \ boxes[boxIndex]['boxBottom'] > boxes[boxIndex-1]['boxBottom'] + rowFloor: break boxIndex += 1 draw(img) # Create the output image.
elif boxOctal == digitIndex and not (characters[characterIndex] in blatant) and not (boxFields[0] in blatant): digit = images[digitIndex].clone() operator = 'darken' else: digit = imagesColored[digitIndex].clone() operator = 'darken' fontWidth = digit.width fontHeight = digit.height minFontHeight = minFontScale*fontHeight minFontWidth = minFontScale*fontWidth maxFontHeight = maxFontScale*fontHeight maxFontWidth = maxFontScale*fontWidth if boxHeight > minFontHeight and boxHeight < maxFontHeight and \ boxWidth > minFontWidth and boxWidth < maxFontWidth: digit.resize(boxWidth, boxHeight, 'cubic') draw.composite(operator=operator, left=boxLeft, top=boxTop, width=boxWidth, height=boxHeight, image=digit) else: digit.resize(int(round(fontWidth*defaultFontScale)), int(round(fontHeight*defaultFontScale)), 'cubic') draw.composite(operator=operator, left=round((boxLeft+boxRight-fontWidth*defaultFontScale)/2.0), top=round((boxTop+boxBottom-fontHeight*defaultFontScale)/2.0), width=fontWidth*defaultFontScale, height=fontHeight*defaultFontScale, image=digit) characterIndex += 1 col += 1 if (octalDigitIndex % 6) == 4: col += 1 if (octalDigitIndex % 6) == 5: col += 7 characterIndex += 1 boxIndex += 1
from wand.image import Image, COMPOSITE_OPERATORS from wand.drawing import Drawing from wand.display import display from pathlib import Path save_path = Path("./") filepaths = [i for i in Path.iterdir(Path("./"))] watermark = Image(filename="../watermark.png") prefixes = [".py", ".py~"] for original in filepaths: print(original) if (original.suffix in prefixes) or ("_sm" in original.stem): continue print(original) new_file = save_path.joinpath(f"{original.stem}_sm{original.suffix}") pic = Image(filename=original) pic.transform(resize="500000@") draw = Drawing() draw.composite(operator="plus", left=10, top=10, width=208, height=35, image=watermark) draw(pic) pic.save(filename=new_file)
# Here is a thing to overcome a problem what octopus has to do to eliminate # horizontal lines on the input pages. One side effect is that '=' is often # eliminated, which is very troublesome. However, there are patterns we can # try to use to reinsert an = where one is missing. lastRight = 0 if (numCharsInRow > 0): lastRight = boxes[boxIndex - 1]['boxRight'] if index < len(charList)-1 and \ character == '=' and boxes[boxIndex]['boxChar'] != '=' and \ boxes[boxIndex]['boxChar'] == charList[index+1] and \ boxes[boxIndex]['boxLeft'] > lastRight + 80*scale: boxLeft = int(round(boxes[boxIndex]['boxLeft'] - 40 * scale)) fontChar = imagesNomatch[ord('=')].clone() draw.composite(operator='darken', left=boxLeft, top=boxes[boxIndex]['boxTop'], width=int(round(fontChar.width * scale)), height=int(round(fontChar.height * scale)), image=fontChar) # Note that this will advance index (the pointer to characters in the line) # but not boxIndex. continue #sumBottomsInRow += boxes[boxIndex]['boxBottom'] if numCharsInRow == 0: avgBottom = boxes[boxIndex]['boxBottom'] else: avgBottom = (1.0 - decayBottom) * boxes[boxIndex][ 'boxBottom'] + decayBottom * avgBottom numCharsInRow += 1 asciiCode = ord(character) if boxes[boxIndex]['boxChar'] == character: fontChar = imagesMatch[asciiCode].clone()
else: fontChar = imagesNomatch[asciiCode].clone() fontWidth = fontChar.width * scale fontHeight = fontChar.height * scale minFontHeight = 0.9 * fontHeight maxFontHeight = 1.3 * fontHeight minFontWidth = 0.7 * fontWidth maxFontWidth = 1.3 * fontWidth operator = 'darken' if boxes[boxIndex]['boxHeight'] > minFontHeight and boxes[boxIndex]['boxHeight'] < maxFontHeight and \ boxes[boxIndex]['boxWidth'] > minFontWidth and boxes[boxIndex]['boxWidth'] < maxFontWidth: fontChar.resize(boxes[boxIndex]['boxWidth'], boxes[boxIndex]['boxHeight'], 'cubic') draw.composite(operator=operator, left=boxes[boxIndex]['boxLeft'], top=boxes[boxIndex]['boxTop'], width=boxes[boxIndex]['boxWidth'], height=boxes[boxIndex]['boxHeight'], image=fontChar) else: if fontWidth <= minFontWidth: fontWidth = minFontWidth elif fontWidth >= maxFontWidth: fontWidth = maxFontWidth if fontHeight <= minFontHeight: fontHeight = minFontHeight elif fontHeight >= maxFontHeight: fontHeight = maxFontHeight fontChar.resize(int(round(fontWidth)), int(round(fontHeight)), 'cubic') draw.composite( operator=operator,
endIndex = startIndex + 4 * 8 # Loop on lines on the selected page. draw = Drawing() row = 0 for index in range(startIndex, endIndex): # Loop on the octal digits in the row. col = 0 characterIndex = 0 characters = list(lines[index]) for octalDigitIndex in range(0, 8 * 6): outputPoint = transformation.Transform((col, row)) digit = int(characters[characterIndex]) draw.composite(operator='atop', left=outputPoint[0] - digitWidth / 2, top=outputPoint[1] - digitHeight / 2, width=digitWidth, height=digitHeight, image=images[digit]) characterIndex += 1 col += 1 if (octalDigitIndex % 6) == 4: col += 1 if (octalDigitIndex % 6) == 5: col += 7 characterIndex += 1 # Next row, please row += 1 if (index % 4) == 3: row += 1.2 draw(img)
print "%s, %s%% > %i tiles so far, %i to go, %s merged, %s copied" % \ (helper.timeString(int(time.time()-start_time)), str(int(round(n/srcTotal*100.00))), \ 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
boxTop = backgroundHeight - 1 - int(boxFields[4]) boxWidth = boxRight + 1 - boxLeft boxHeight = boxBottom + 1 - boxTop digitIndex = int(characters[characterIndex]) if boxOctal == digitIndex: digit = images[digitIndex].clone() operator = 'darken' #'xor' else: digit = imagesColored[digitIndex].clone() operator = 'darken' digit.resize(boxWidth, boxHeight, 'cubic') draw.composite(operator=operator, left=boxLeft, top=boxTop, width=boxWidth, height=boxHeight, image=digit) characterIndex += 1 col += 1 if (octalDigitIndex % 6) == 4: col += 1 if (octalDigitIndex % 6) == 5: col += 7 characterIndex += 1 boxIndex += 1 # Next row, please row += 1 if (index % 4) == 3: row += 1.2
# Here is a thing to overcome a problem what octopus has to do to eliminate # horizontal lines on the input pages. One side effect is that '=' is often # eliminated, which is very troublesome. However, there are patterns we can # try to use to reinsert an = where one is missing. lastRight = 0 if (numCharsInRow > 0): lastRight = boxes[boxIndex-1]['boxRight'] if index < len(charList)-1 and \ character == '=' and boxes[boxIndex]['boxChar'] != '=' and \ boxes[boxIndex]['boxChar'] == charList[index+1] and \ boxes[boxIndex]['boxLeft'] > lastRight + 80*scale: boxLeft = int(round(boxes[boxIndex]['boxLeft'] - 40 * scale)) fontChar = imagesNomatch[ord('=')].clone() draw.composite(operator='darken', left=boxLeft, top=boxes[boxIndex]['boxTop'], width=int(round(fontChar.width * scale)), height=int(round(fontChar.height * scale)), image=fontChar) # Note that this will advance index (the pointer to characters in the line) # but not boxIndex. continue #sumBottomsInRow += boxes[boxIndex]['boxBottom'] if numCharsInRow == 0: avgBottom = boxes[boxIndex]['boxBottom'] else: avgBottom = (1.0 - decayBottom) * boxes[boxIndex]['boxBottom'] + decayBottom * avgBottom numCharsInRow += 1 asciiCode = ord(character) if boxes[boxIndex]['boxChar'] == character: fontChar = imagesMatch[asciiCode].clone() else: fontChar = imagesNomatch[asciiCode].clone()