Image preview with Reportlab? - python

I'm generating some pdf's with Reportlab and Django using a web interface. The pdf's are always going to be a single page. I'd like to generate a png or jpg image of the generated page and return that to the browser for the user to preview before saving the final pdf and delivering it to the end user. Is there anyway to do this?

This answer explains that you can use ghostscript to convert pdf to png. Depending of the requirements of your app (traffic, response time, nb of pdfs ...) it may or may not be a solution for you.
This is just an idea, but may be you can generate the preview image in parallel using PIL ImageDraw and get rid of the pdf-to-png conversion.
I hope it helps

Related

Images in QTextEdit - some are displayed, some not

I want to display a short manual in a tab of program
(Windows 10, anaconda distribution, python 3.7.6, pyqt 5.12).
I wrote a text document with html inside and load it in a QTextEdit using
self.te_short_manual = QTextEdit() # Textedit for display
with open(os.path.join(py_file_folder, r"doc_files\tab5_documentation.html"), "r") as doc_file:
lines = doc_file.readlines()
doc_text = "\n".join(lines)
self.te_short_manual.setHtml(doc_text)
self.te_short_manual.setReadOnly(True)
In the text document I use the img command to load an image:
<p>
<img src="banana_select_roi_nb2.tif" alt="Safely selecting a ROI" width="500" >
<br>FIG 1: <i>How to use the color map limits to better define the ROI.</i>
</p>
Using this image image_faisceau_ 28.tif, I see the image as wished (but not in Firefox).
Using this image banana_select_roi_nb.tif, I do not see the image (in Firefox neither).
Using this image IR_0309.jpg, I do not see the image (but I see it in Firefox).
All images and the text document are in the same directory.
I read about QWebView here
Display HTML file with image in QTextEdit, but would like to avoid it.
Running supportedImageFormats() shows a long list including tif, tiff, jpg, png, ... formats
https://forum.qt.io/topic/54924/solved-a-png-image-within-html-code-in-a-qtextedit
reported something similar with the image not showing in firefox when showing in QTextEdit,
but adding "html/" in the src-value did not help. There is still this kind of placeholder displayed... and anyway, one of the two tif-images works.
In addition to some help to solve the problem on my machine, comments on the expected robustness
off my apporach would be appreciated. I wonder what happens if I give the program to somebody else. Ideally I would like it to work on all standard Anaconda installs.

Discord.Py blend pictures and send them without dowload them

I have the following problem:
1.- I want to send a file in discord without dowloading. I don't know if this is posible but I want to send it for example with BytesIo.
2.- I have one picture saved on my Bot files and the other one comes from ctx.author.avatar
3.- I want to blend both images and send the result. With blend I mean like for example if I would be using cv2 I would use addWeighted().
The Code I have right know what does is dowload the picture of the member, using cv2 to read both pictures, resize them and use addWeighted. After that I save the blend picture and I send it as a message. When all It's done I delete the pictures (both, the avatar and the blend one). From my point of view this is really inefficient, thats why I want know if there is a way using PIL and BytesIo or something to use the dataArray to blend them and send it without dowload it.
So in short, I want to know if there is a way to blends both images without download the second one (member avatar picture) and send it without dowloading the blend image.
I can the code I already have if needed but as my code is download the picture I guess that won't help.
You can get the image's URL from the message (Get a picture from the message):
message.attachments[0].url
Then load the image into memory with the requests library (How to open an image from an url with opencv using requests from python)
The way I found at the end to solve it, was using just Pillow doing the following:
image1 = Image.open("img.jpg", mode='r')
image2 = Image.open(requests.get(url, stream=True).raw)
With I have both images on memory, so I just needed to resize both of them and blend. Finally I just used io.Bytes() to make into bytes and send it.
How to blend -> https://pythontic.com/image-processing/pillow/blend

How to add images to Pythonista's ui designer

I have been fooling around with python and Pythonista 2.5 on iOS. I currently am far too inexperienced to create good UIs in scripts and need some help using the designer in Pythonista. I currently wish to add an image asset, yet I am only able to use stock images provided, is there any folder path I can follow to add my images to that list, or is there another simple way of doing it?
Keep in mind I have little experience and thank you for any help!
You can store the images files in any folder. A simple way to copy external images would be to first copy it in clipboard (may be from photos) and then save it as png file by running the following script.
(Custom images are currently not supported in the UI editor, You have to load them via code.
https://forum.omz-software.com/topic/3668/images-in-ui-designer
But you can use the [+] button in the code editor (at the top) to view bundled images/textures. (images in the current directory are also shown.)
https://forum.omz-software.com/topic/3760/itunes-file-sharing )
import clipboard
image = clipboard.get_image()
image.show()
image.save('img1.png')
You can also use dropbox or appex scripts to store images.

Downloading a section of an Image from a website in python?

I am currently able to download an entire image from a URL using
from PIL import Image, ImageTk
import cStringIO
import urllib
url = http://prestigemgmt.us/wp-content/uploads/2013/03/dog.jpg
self.image = Image.open(cStringIO.StringIO(urllib.urlopen(url).read()))
It works fine and gets the whole image from the website. My question is there any way to get, lets say only the right half of the image.
I understand I could edit the image after it is downloaded, but speed is an important aspect, so ideally I would download only what I need.
It is not possible to do this.
The common image file formats like PNG and JPEG are encoded in a manner that you cannot download an arbitrary part of the picture without downloading the full picture.
You need to download the whole picture, decode it and edit it after the download.
For the advanced knowledge you can always study PNG and JPEG file formats.
If you are in the control of the server providing the images you can write a server-side script which edits the image on the server and then sends the edit over the wire.

Django/Python resize image

I have a image from
files = request.FILES['new_photo'].read()
and i want to know it's size and possibly resize it.
How i can do it?
thanks.
UPD
before. i read() file and gave this string and some info? to SQL function. And she save it on ftp.
how i can get data same as the data returned from read() method?
i try to use Image.tostring()
and save it. But i have 0x0px image.
Here is a small snippet from one of my web applications (sightly modified), which creates a thumbnail with the maximum size of 200x200 pixels. Maybe you can use some parts of it:
from PIL import Image
image = request.FILES['new_photo']
if image:
img = Image.open(image)
img.save(path.join(app.config['UPLOAD_FOLDER'],
secure_filename('upload-%d.jpg' % self.obj.id)), 'JPEG')
img.thumbnail((200, 200), Image.ANTIALIAS)
img.save(path.join(app.config['UPLOAD_FOLDER'],
secure_filename('upload-%d.200.jpg' % self.obj.id)), 'JPEG')
you can use PIL for processing images .
http://www.pythonware.com/products/pil/
User one of the thumbnail libraries available such as http://djangothumbnails.com/ or more http://code.djangoproject.com/wiki/ThumbNails
If you would like to add "automated image processing" to your project, you might consider using Django-imagekit.
The author has included Installion and usage instructions on this Github Repo page.
Hope this helps you as I found it from the same search as I found this Question :)

Categories