本文整理汇总了Python中wand.color.Color方法的典型用法代码示例。如果您正苦于以下问题:Python color.Color方法的具体用法?Python color.Color怎么用?Python color.Color使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wand.color
的用法示例。
在下文中一共展示了color.Color方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_image
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def save_image(pdf_path, img_path, page_num):
"""
Creates images for a page of the input pdf document and saves it
at img_path.
:param pdf_path: path to pdf to create images for.
:param img_path: path where to save the images.
:param page_num: page number to create image from in the pdf file.
:return:
"""
pdf_img = Image(filename="{}[{}]".format(pdf_path, page_num))
with pdf_img.convert("png") as converted:
# Set white background.
converted.background_color = Color("white")
converted.alpha_channel = "remove"
converted.save(filename=img_path)
示例2: display_bounding_boxes
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def display_bounding_boxes(self, page_num, bboxes, alternate_colors=True):
elems = self.elems[page_num]
page_width, page_height = int(elems.layout.width), int(elems.layout.height)
img = pdf_to_img(self.pdf_file, page_num, page_width, page_height)
draw = Drawing()
draw.fill_color = Color("rgba(0, 0, 0, 0)")
color = Color("blue")
draw.stroke_color = color
for block in bboxes:
top, left, bottom, right = block[-4:]
draw.stroke_color = Color(
"rgba({},{},{}, 1)".format(
str(np.random.randint(255)),
str(np.random.randint(255)),
str(np.random.randint(255)),
)
)
draw.rectangle(
left=float(left),
top=float(top),
right=float(right),
bottom=float(bottom),
)
draw(img)
return img
示例3: display_bounding_boxes
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def display_bounding_boxes(img, blocks, alternatecolors=False, color=Color("blue")):
"""
Displays each of the bounding boxes passed in 'boxes' on an image of the pdf
pointed to by pdf_file
boxes is a list of 5-tuples (page, top, left, bottom, right)
"""
draw = Drawing()
draw.fill_color = Color("rgba(0, 0, 0, 0)")
draw.stroke_color = color
for block in blocks:
top, left, bottom, right = block[-4:]
if alternatecolors:
draw.stroke_color = Color(
"rgba({},{},{}, 1)".format(
str(np.random.randint(255)),
str(np.random.randint(255)),
str(np.random.randint(255)),
)
)
draw.rectangle(
left=float(left), top=float(top), right=float(right), bottom=float(bottom)
)
draw(img)
display(img)
示例4: _run_convert
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def _run_convert(filename, page, res=120):
idx = page + 1
temp_time = time.time() * 1000
# 由于每次转换的时候都需要重新将整个PDF载入内存,所以这里使用内存缓存
pdfile = getPdfReader(filename)
pageObj = pdfile.getPage(page)
dst_pdf = PdfFileWriter()
dst_pdf.addPage(pageObj)
pdf_bytes = io.BytesIO()
dst_pdf.write(pdf_bytes)
pdf_bytes.seek(0)
img = Image(file=pdf_bytes, resolution=res)
img.format = 'png'
img.compression_quality = 90
img.background_color = Color("white")
# 保存图片
img_path = '%s%d.png' % (filename[:filename.rindex('.')], idx)
img.save(filename=img_path)
img.destroy()
img = None
pdf_bytes = None
dst_pdf = None
print('convert page %d cost time %d' % (idx, (time.time() * 1000 - temp_time)))
示例5: render_chart
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def render_chart(pdf_file, page, bounds, dpi, target):
"""Renders part of a pdf file with imagemagick.
Pass this function the bounds and resolution.
"""
pdf_page = pdf_file + '[{}]'.format(page)
with Image(filename=pdf_page, resolution=dpi) as img:
factor = 1.0*dpi/100
x0 = bounds[0]
y0 = bounds[1]
w = bounds[2] - x0
h = bounds[3] - y0
img.crop(left=int(x0*factor), top=int(y0*factor),
width=int(w*factor), height=int(h*factor))
# put transparent image on white background
with Image(width=img.width, height=img.height,
background=Color("white")) as bg:
bg.composite(img, 0, 0)
bg.save(filename=target)
示例6: setup
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def setup(bot):
global geotiler
global Color, Drawing, display, Image, Color, Image, COMPOSITE_OPERATORS
global BeautifulSoup
check_folders()
check_files()
try:
import geotiler
except:
raise ModuleNotFound("geotiler is not installed. Do 'pip3 install geotiler --upgrade' to use this cog.")
try:
from bs4 import BeautifulSoup
except:
raise ModuleNotFound("BeautifulSoup is not installed. Do 'pip3 install BeautifulSoup --upgrade' to use this cog.")
try:
from wand.image import Image, COMPOSITE_OPERATORS
from wand.drawing import Drawing
from wand.display import display
from wand.image import Image
from wand.color import Color
except:
raise ModuleNotFound("Wand is not installed. Do 'pip3 install Wand --upgrade' and make sure you have ImageMagick installed http://docs.wand-py.org/en/0.4.2/guide/install.html")
bot.add_cog(OpenStreetMaps(bot))
示例7: display_bounding_boxes_within_notebook
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def display_bounding_boxes_within_notebook(
page_num, extractor, blocks, alternatecolors=False, color=Color("blue")
):
"""
Displays each of the bounding boxes passed in 'boxes' on an image of the pdf
pointed to by pdf_file
boxes is a list of 5-tuples (page, top, left, bottom, right)
"""
elems = extractor.elems[page_num]
page_width, page_height = int(elems.layout.width), int(elems.layout.height)
img = pdf_to_img(extractor.pdf_file, page_num, page_width, page_height)
draw = Drawing()
draw.fill_color = Color("rgba(0, 0, 0, 0)")
draw.stroke_color = color
for block in blocks:
top, left, bottom, right = block[-4:]
if alternatecolors:
draw.stroke_color = Color(
"rgba({},{},{}, 1)".format(
str(np.random.randint(255)),
str(np.random.randint(255)),
str(np.random.randint(255)),
)
)
draw.rectangle(
left=float(left), top=float(top), right=float(right), bottom=float(bottom)
)
draw(img)
return img
示例8: display_boxes
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def display_boxes(self, tree, html_path, filename_prefix, alternate_colors=False):
"""
Displays each of the bounding boxes passed in 'boxes' on images of the pdf
pointed to by pdf_file
boxes is a list of 5-tuples (page, top, left, bottom, right)
"""
imgs = []
colors = {
"section_header": Color("blue"),
"figure": Color("green"),
"figure_caption": Color("green"),
"table_caption": Color("red"),
"list": Color("yellow"),
"paragraph": Color("gray"),
"table": Color("red"),
"header": Color("brown"),
}
for i, page_num in enumerate(tree.keys()):
img = self.pdf_to_img(page_num)
draw = Drawing()
draw.fill_color = Color("rgba(0, 0, 0, 0.0)")
for clust in tree[page_num]:
for (pnum, pwidth, pheight, top, left, bottom, right) in tree[page_num][
clust
]:
draw.stroke_color = colors[clust]
draw.rectangle(left=left, top=top, right=right, bottom=bottom)
draw.push()
draw.font_size = 20
draw.font_weight = 10
draw.fill_color = colors[clust]
if int(left) > 0 and int(top) > 0:
draw.text(x=int(left), y=int(top), body=clust)
draw.pop()
draw(img)
img.save(filename=html_path + filename_prefix + "_page_" + str(i) + ".png")
imgs.append(img)
return imgs
示例9: display_boxes
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def display_boxes(
self, pdf_file: str, boxes: List[Bbox], alternate_colors: bool = False,
) -> List[Image]:
"""Display bounding boxes on the document.
Display each of the bounding boxes passed in 'boxes' on images of the pdf
pointed to by pdf_file
boxes is a list of 5-tuples (page, top, left, bottom, right)
"""
imgs = []
with Color("blue") as blue, Color("red") as red, Color(
"rgba(0, 0, 0, 0.0)"
) as transparent, Drawing() as draw:
colors = [blue, red]
boxes_per_page: DefaultDict[int, int] = defaultdict(int)
boxes_by_page: DefaultDict[
int, List[Tuple[int, int, int, int]]
] = defaultdict(list)
for i, (page, top, bottom, left, right) in enumerate(boxes):
boxes_per_page[page] += 1
boxes_by_page[page].append((top, bottom, left, right))
for i, page_num in enumerate(boxes_per_page.keys()):
img = pdf_to_img(pdf_file, page_num)
draw.fill_color = transparent
for j, (top, bottom, left, right) in enumerate(boxes_by_page[page_num]):
draw.stroke_color = colors[j % 2] if alternate_colors else colors[0]
draw.rectangle(left=left, top=top, right=right, bottom=bottom)
draw(img)
imgs.append(img)
return imgs
示例10: save_as_image
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def save_as_image(document, save_as, constants):
from wand.image import Image
from wand.color import Color
from wand.exceptions import DelegateError
temp_directory = pathlib.Path(tempfile.mkdtemp())
temp_pdf_name = temp_directory / (save_as.name + '.pdf')
document.SaveAs2(
FileName=str(temp_pdf_name.absolute()),
FileFormat=constants.wdFormatPDF,
)
try:
with Image(filename=str(temp_pdf_name), resolution=300) as pdf:
pdf.trim()
with Image(width=pdf.width, height=pdf.height, background=Color('white')) as png:
png.composite(pdf, 0, 0)
png.save(filename=str(save_as))
except DelegateError:
print('Error: You may need to install ghostscript as well', file=sys.stderr)
exit(1)
# https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat.aspx
示例11: svg_to_png
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def svg_to_png(infile, outfile, dpi=300):
with Image(resolution=300) as image:
with Color('transparent') as background_color:
library.MagickSetBackgroundColor(image.wand,
background_color.resource)
image.read(filename=infile, resolution=300)
png_image = image.make_blob("png32")
with open(outfile, "wb") as out:
out.write(png_image)
示例12: pdf2png
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def pdf2png(filename):
from wand.image import Image
from wand.color import Color
import os
with Image(filename="{}.pdf".format(filename), resolution=500) as img:
with Image(
width=img.width, height=img.height,
background=Color("white")) as bg:
bg.composite(img, 0, 0)
bg.save(filename="{}.png".format(filename))
os.remove('{}.pdf'.format(filename))
# ==================================================================#
# ==================================================================#
示例13: pdf2png
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def pdf2png(filename):
from wand.image import Image
from wand.color import Color
with Image(filename="{}.pdf".format(filename), resolution=500) as img:
with Image(
width=img.width, height=img.height,
background=Color("white")) as bg:
bg.composite(img, 0, 0)
bg.save(filename="{}.png".format(filename))
os.remove('{}.pdf'.format(filename))
示例14: render_to_jpeg
# 需要导入模块: from wand import color [as 别名]
# 或者: from wand.color import Color [as 别名]
def render_to_jpeg(html: HTML, buffer: io.BytesIO):
png_buffer = io.BytesIO()
html.write_png(png_buffer)
png_buffer.seek(0)
with Image(file=png_buffer) as image:
image.background_color = Color('#fff')
image.alpha_channel = 'remove'
image.format = 'jpeg'
image.save(file=buffer)