Image clarity issue in HTML page - python

I have Matplotlib & Seaborn visualizations that need to be saved in HTML. Since there is no direct method to do so, I first saved the images in PNG & then converted them to HTML. This decreased the quality of my images.
My code:
import aspose.words as aw
from PIL import Image
def pairplot_fun(eda_file, pairplot_inputs, pairplot_png, pairplot_html):
pairplot_var=pairplot_inputs[0]
sns.pairplot(eda_file, hue=pairplot_var, height=4);
plt.savefig(pairplot_png)
doc = aw.Document()
builder_pairplot = aw.DocumentBuilder(doc)
builder_pairplot.insert_image(pairplot_png)
doc.save(pairplot_html, dpi=1200)
Specifying the 'dpi' this way isn't making any difference. How do I improve the clarity of my image saved in HTML format?

You can specify image resolution in Aspose.Words HtmlSaveOptions using HtmlSaveOptions.image_resolution property.
doc = aw.Document("in.docx")
options = aw.saving.HtmlSaveOptions()
options.image_resolution = 1200
doc.save("out.html", options)

Related

how to save or convert holoviews object to image or video

I am trying to detect an image through a detector and want to save it to video or image, but I could not find anything with holoviews DynamicMap to save to image or to video. Please help
fname=r'D:\tiff_data\output_0302.tif'
#fname=r'D:\cite-on-main\poly_wet (1209).tif'
save='outputs/demo'
os.makedirs(save,exist_ok=True)
im=Image.open(fname)
import cv2
img = cv2.imread(fname)
median=np.median(img,axis=2)
upscaling=0.9
detector=Detector('./weights/', gpu='0', init_shape=img.shape[:2], init_upscaling=upscaling)
def explore(Score, Upscaling):
width = median.shape[1]
height = median.shape[0]
detections=detector.detect(median,Score,Upscaling)
aaa = hv.Image((np.arange(width),np.arange(height),median)).opts(title='',invert_yaxis=True, cmap='gray',width=median.shape[1]*scale, height=median.shape[0]*scale,)*hv.Path([hv.Bounds(tuple(r)) for r in detections[:,:4]]).opts(color='lime')
return aaa
dmap=hv.DynamicMap(explore, kdims=['Upscaling','Score'])
dmap.redim.values( Score=np.arange(0.1,1,0.05), Upscaling=np.arange(1,3,0.1))
for reference i have added an image
You can convert a DynamicMap to a HoloMap (http://holoviews.org/user_guide/Live_Data.html#converting-from-dynamicmap-to-holomap) and export a HoloMap to a png or gif or mp4 (https://holoviews.org/user_guide/Exporting_and_Archiving.html). You may need to install some extra dependencies depending on your backend, but the error messages should guide you to what you need. Exporting Matplotlib output is generally easier and faster than Bokeh output, but both should work.

Display a pdf file using colab

I have saved some images of my work in .pdf format using matplotlib, I know this is my fault from the beginning and I should save it directly as image but I did not know that I can not display pdf files on colab. To get these results I need another 10 days which is not good choice for me.
Actually I have found this which express my problem precisely but there was not answer.
It just seems strange to me that using matplotlib I can save pdf files but I can not load them using it again.
I just need to display the pdf file in colab cell ,I have tried:
import subprocess
subprocess.Popen(['myfile.pdf'],shell=True)
and this was the result:
<subprocess.Popen at 0x7f4d6a395978>
another methods as in this page do not work for me
Ok this works for me, maybe there are a simpler solution but for now this works
from pdf2image import convert_from_path
from IPython.display import display, Image
images = convert_from_path("myfile.pdf")
for i, image in enumerate(images):
fname = "image" + str(i) + ".png"
image.save(fname, "PNG")
Image(fname, width=600, height=300)
In a Jupyter notebook / Colab you can simply
from pdf2image import convert_from_path
images = convert_from_path("myfile.pdf")
images[0] # first page
The image will be able to render as the cell output. No need for IPython.display

How to extract images and image BBox coordinates using python?

I am trying to extract images in PDF with BBox coordinates of the image.
I tried using pdfrw library, it is identifying image objects and it have an attribute called media box which have some coordinates, i am not sure if those are correct bbox coordinates since for some pdfs it is showing something like this
['0', '0', '684', '864']
but image doesn't start at the start of the page, so i don't think it is bbox
I tried with following code using pdfrw
import pdfrw, os
from pdfrw import PdfReader, PdfWriter
from pdfrw.findobjs import page_per_xobj
outfn = 'extract.' + os.path.basename(path)
pages = list(page_per_xobj(PdfReader(path).pages, margin=0.5*72))
writer = PdfWriter(outfn)
writer.addpages(pages)
writer.write()
How do i get image along with it's bbox coordinates?
sample pdf : https://drive.google.com/open?id=1IVbj1b3JfmSv_BJvGUqYvAPVl3FwC2A-
I found a way to do it through a library called pdfplumber. It's built on top of pdfminer and is working consistently in my use-case. And moreover, its MIT licensed so it is helpful for my office work.
import pdfplumber
pdf_obj = pdfplumber.open(doc_path)
page = pdf_obj.pages[page_no]
images_in_page = page.images
page_height = page.height
image = images_in_page[0] # assuming images_in_page has at least one element, only for understanding purpose.
image_bbox = (image['x0'], page_height - image['y1'], image['x1'], page_height - image['y0'])
cropped_page = page.crop(image_bbox)
image_obj = cropped_page.to_image(resolution=400)
image_obj.save(path_to_save_image)
Worked well for tables and images in my case.

Python ipyleaflet export map as PNG or JPG or SVG

I have tried to export a visualisation of data with ipyleaflet as PNG or any other file format but i could not find a method that is working. For example in folium there is map.save(path). Is there a library or method in ipyleaflet that i have missed in my research which helps me to accomplish my goal?
here is some example code to generate a map
from ipyleaflet import *
center = [34.6252978589571, -77.34580993652344]
zoom = 10
m = Map(default_tiles=TileLayer(opacity=1.0), center=center, zoom=zoom)
m
I'd like to export this map as an image file without taking a screenshot manually.
I found two sources that allow to export javascript leaflet maps:
https://github.com/aratcliffe/Leaflet.print and https://github.com/mapbox/leaflet-image
Unfortunately i was not able to make use of them in python.
My colleague and I found a decent work around for ipyleaflet (python) image export. Here is how it works. The folium library is required for an export. The GeoJson data in this example is already prepared with style properties:
import folium
map = folium.Map([51., 12.], zoom_start=6,control_scale=True)
folium.GeoJson(data).add_to(map)
map.save('map.html')
This is how the result looks:
The html file can be further processed in python (windows) with subprocess calls to make a PDF or PNG out of it. I hope this helps as the ipyleaflet doc for python is almost non existant.
For generating html, you can use ipywidgets
from ipywidgets.embed import embed_minimal_html
embed_minimal_html('map.html', views=[m])
If you want to make a PNG, you can use ipywebrtc, more specifically:
https://ipywebrtc.readthedocs.io/en/latest/ImageRecorder.html
https://ipywebrtc.readthedocs.io/en/latest/WidgetStream.html
Or in code:
from ipywebrtc import WidgetStream, ImageRecorder
widget_stream = WidgetStream(widget=m, max_fps=1)
image_recorder = ImageRecorder(stream=widget_stream)
display(image_recorder)
Saving the PNG:
with open('map.png', 'wb') as f:
f.write(image_recorder.image.value)
Or converting to pillow image for preprocessing:
import PIL.Image
import io
im = PIL.Image.open(io.BytesIO(image_recorder.image.value))
ipyleaflet supports saving as html. Export of svg and png does not seem to be supported.
https://ipyleaflet.readthedocs.io/en/latest/map_and_basemaps/map.html#save-to-html
m.save('output.html')
I created an issue ticket for ipyleaflet:
https://github.com/jupyter-widgets/ipyleaflet/issues/1083

Embed .SVG files into PDF using reportlab

I have written a script in python that produces matplotlib graphs and puts them into a pdf report using reportlab.
I am having difficulty embedding SVG image files into my PDF file. I've had no trouble using PNG images but I want to use SVG format as this produces better quality images in the PDF report.
This is the error message I am getting:
IOError: cannot identify image file
Does anyone have suggestions or have you overcome this issue before?
Yesterday I succeeded in using svglib to add a SVG Image as a reportlab Flowable.
so this drawing is an instance of reportlab Drawing, see here:
from reportlab.graphics.shapes import Drawing
a reportlab Drawing inherits Flowable:
from reportlab.platypus import Flowable
Here is a minimal example that also shows how you can scale it correctly (you must only specify path and factor):
from svglib.svglib import svg2rlg
drawing = svg2rlg(path)
sx = sy = factor
drawing.width, drawing.height = drawing.minWidth() * sx, drawing.height * sy
drawing.scale(sx, sy)
#if you want to see the box around the image
drawing._showBoundary = True
As mentioned by skidzo, you can totally do this with the svglib package, which you can find here: https://pypi.python.org/pypi/svglib/
According to the website, Svglib is a pure-Python library for reading SVG files and converting them (to a reasonable degree) to other formats using the ReportLab Open Source toolkit.
You can use pip to install svglib.
Here is a complete example script:
# svg_demo.py
from reportlab.graphics import renderPDF, renderPM
from reportlab.platypus import SimpleDocTemplate
from svglib.svglib import svg2rlg
def svg_demo(image_path, output_path):
drawing = svg2rlg(image_path)
renderPDF.drawToFile(drawing, output_path)
if __name__ == '__main__':
svg_demo('/path/to/image.svg', 'svg_demo.pdf')
skidzo's answer is very helpful, but isn't a complete example of how to use an SVG file as a flowable in a reportlab PDF. Hopefully this is helpful for others trying to figure out the last few steps:
from io import BytesIO
import matplotlib.pyplot as plt
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph
from svglib.svglib import svg2rlg
def plot_data(data):
# Plot the data using matplotlib.
plt.plot(data)
# Save the figure to SVG format in memory.
svg_file = BytesIO()
plt.savefig(svg_file, format='SVG')
# Rewind the file for reading, and convert to a Drawing.
svg_file.seek(0)
drawing = svg2rlg(svg_file)
# Scale the Drawing.
scale = 0.75
drawing.scale(scale, scale)
drawing.width *= scale
drawing.height *= scale
return drawing
def main():
styles = getSampleStyleSheet()
pdf_path = 'sketch.pdf'
doc = SimpleDocTemplate(pdf_path)
data = [1, 3, 2]
story = [Paragraph('Lorem ipsum!', styles['Normal']),
plot_data(data),
Paragraph('Dolores sit amet.', styles['Normal'])]
doc.build(story)
main()
You need to make sure you are importing PIL (Python Imaging Library) in your code so that ReportLab can use it to handle image types like SVG. Otherwise it can only support a few basic image formats.
That said, I recall having some trouble, even when using PIL, with vector graphics. I don't know if I tried SVG but I remember having a lot of trouble with EPS.

Categories