I've just created a dashboard in Dash, which fits automatically to my computer monitor and looks half decent. I now have the problem that I have to produce a way to print to PDF.
I've seen the Vanguard example which uses external CSS to format into an A4-size, and then a print button (using external javascript?). Is there a more 'Dash-native' way of doing this that doesn't require me to learn another programming language (I only know Python)?
There doesn't seem to be anything on the Dash User Guide https://dash.plot.ly/, all I've found is this https://plot.ly/python/pdf-reports/ which describes how to print individual plotly figures to pdf.
Ideally, it would format as it is now online (online layout) without losing space down the sides, and then I could use another layout (pdf layout) which could be printed out.
Any links or suggestions on how to proceed with this would be very much appreciated!
You can get pdf from plotly if save plot as png image (the most left option on image):
And then using that code convert png to pdf:
from PIL import Image
# Save your plot as png file (option "Download as png" in upper-left corner)
PNG_FILE = 'horizontal-bar.png'
PDF_FILE = 'horizontal-bar.pdf'
rgba = Image.open(PNG_FILE)
rgb = Image.new('RGB', rgba.size, (255, 255, 255)) # white background
rgb.paste(rgba, mask=rgba.split()[3]) # paste using alpha channel as mask
rgb.save(PDF_FILE, 'PDF', resolution=100.0)
If you are a lazy one, you can use library webkit2png check here how. This allowing you not clicking on Download as png button and just convert from html to png and then to pdf.
Related
I am currently using python to remove watermarks in PDF files. For example, I have a file like this:
The green shape on the center of the page is the watermark. I think it's not stored in the PDF in text form, because I can't find that text by simply searching using Edge browser (which can read PDF files).
Also, I cannot find the watermark by image. I extracted all images from the PDF using PyMuPDF, and the watermark (which was supposed to appear on each page) is not to be found.
The code I used for extracting is like this:
document = fitz.open(self.input)
for each_page in document:
image_list = each_page.getImageList()
for image_info in image_list:
pix = fitz.Pixmap(document, image_info[0])
png = pix.tobytes() # return picture in png format
if png == watermark_image:
document._deleteObject(image_info[0])
document.save(out_filename)
So how do I find and remove the watermark using python's libraries? How is the watermark stored inside a PDF?
Are there any other "magic" libraries that can do this task, other than PyMuPDF?
For anyone interested in details see the solution provided here.
Removal of the type of watermark used in this file works with PyMuPDF's low-level code interface. There is no direct, specialized high-level API for doing this.
I've done a simple image processing using openCV that has a RGB-Array as the output.
is it possible to transfer and display my RGB-Array to a HTML without initially saving it as .jpg or any other picture format?
I can't use CGI, since I need to display this picture in an application, that only allows a HTML Code.
You could just draw them to an HTML canvas object.
Drawing to canvas requires the use of JavaScript, but you could easily put your drawing function in the onload event of the body element. This StackOverflow thread talks more about how to draw individual pixels to a canvas object. They built a function for drawing individual pixels, and then demonstrate how to use it to draw a simple image. If you just build a script that takes the data in some hidden html element, and then have your Python script manipulate that hidden element, you'd only need to write your code once.
I'm new to Dash apps. I'm interested in creating a web app where the user uploads their own image, and then I alter it in some way, and show the resulting image (ideally using pillow v 6.0.0). A basic example would be to take in an image, rotate it, and show the rotated image. I've been using this example as a template for uploading an image and showing the same image. How can I alter the example to rotate the image rather than just displaying the same image?
I have .ppt files containing presentations and I need to split a single .ppt file into the slides that it is made of and to store each slide as an image. Any way of achieving this?
Any help would be appreciated. Thanks in advance!
With Aspose.Slides for Python, you can easy save each presentation slide to an image. The following code example shows you how to do this:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation("example.ppt") as presentation:
for slide_index, slide in enumerate(presentation.slides):
# Convert the current slide to an image at 100% scale.
slide_image = slide.get_thumbnail(1, 1)
# Save the slide image to a PNG file.
image_path = "slide_{}.png".format(slide_index + 1)
slide_image.save(image_path, draw.imaging.ImageFormat.png)
This is a paid library, but you can get a temporary license or use a trial mode to evaluate all features for managing presentations. You can see the conversion results without any code by using Online PowerPoint Converter. It is based on this library.
Alternatively, you can use Aspose.Slides Cloud SDK for Python. The code example below shows you how to do the same using Aspose.Slides Cloud:
import asposeslidescloud
from asposeslidescloud.apis.slides_api import SlidesApi
from asposeslidescloud.models import *
slides_api = SlidesApi(None, "my_client_id", "my_client_secret")
with open("example.ppt", "rb") as file_stream:
# Convert all presentation slides to PNG images at 100% scale.
result_path = slides_api.convert(file_stream, SlideExportFormat.PNG)
print("A ZIP file with slide images was saved to " + result_path)
This is also a paid product based on REST, but you can make 150 free API calls per month for managing presentations. I work as a Support Developer at Aspose and we will be glad to answer your questions of this products on Aspose.Slides forum.
I am working on a project where our database is on svg format. Now, the weird thing (I don't know much about image formats) is that the image looks totally different if I open it on a computer program like Image Viewer, ImageMagick or Pinta, to how the image looks when I open it on a browser (be it Mozilla or Chrome). I am attaching an image (converted from svg to png) for convenience and you can see that the difference is really big if you open it on a browser compared to a normal program (if you download it and open it on your computer).
Now, I opened it on Python to see what is going on, and apparently the image is on RBGA format. I thought to convert it to RGB, and did it using the following code:
img = Image.open(os.path.join(PNG_REPO, page)[:-4] + ".png")
arr = np.array(img)
rgbImage = cv2.cvtColor(arr, cv2.COLOR_RGBA2RGB)
img = Image.fromarray(rgbImage)
img.save("please.png")
and then, weird stuff happened. The image seems to have become completely black (attached below) and opening it on Python, a local program or a browser doesn't make any difference anymore.
Anyone has any idea what is going on? I think that I am losing it and I am completely stucked.
In fact, SVG and PNG are totally different.
SVG (Scallable vector graphics) is not a standard image format : the image is not stored as an raay of pixel (or derivative), but as a vectorized format. To display it, you would have to render the vectors into pixels, then display them. The big assets of this approach are the quality vs. size : for illustrations, you can have a fantastic quality for almost nothing in size, however, it requires more time to process, and the decoding is very different from other image formats, thus it is often not supported by image viewers, and the renders can depend a bit on the renderer.
PNG (Portable network graphics) on the other hand is still based on pixel, and is very widely supported.
As for your question, your image have an alpha (transparency) channel. When converting it to RGB, you are getting rid of this transparency, which opencv translates into a fixed color. Here, this color is (0,0,0), that is to say black.
You should try this :
img = Image.open(os.path.join(PNG_REPO, page)[:-4] + ".png")
img.load()
new_img = Image.new("RGB", img.size, (255, 255, 255))
new_img.paste(img, mask=img.split()[3]) # 3 is the alpha channel
new_img.save("should_be_good.png")