Qt - Loading image with wrong extension - python

I'm currently working on a Qt program that works with images that are supplied by the users. One problem I've run into is that a lot of images are saved with the wrong extension - e.g. an image is saved in JPG format but has a PNG extension.
Qt doesn't seem to deal well with this. When I load an image like this into a QImage, the QImage fails to load. I've been looking through the docs, but I haven't come across anything relating to this.
Other programs I've used are able to correctly identify the image as a JPG despite the PNG extension, so there should be no reason for Qt to be unable to do this, but I'm not having any luck.
Any suggestions?

I solved this by using a QImageReader. An example is shown below using PySide.
First I created an instance of QImageReader and set it to read the format from the content.
image_reader = QtGui.QImageReader()
image_reader.setDecideFromContent(True)
This setting tells the reader to only look at the image's data to determine its format and not the extension.
Then I set the filename to the filename of the image I wanted to load and called read().
image_reader.setFileName(file_path_here)
image = image_reader.read()
Read returns a QImage object, so I proceeded with the rest of my code from there.

Related

how to fix problem of boxes generated by pdf2image while converting pdf to images.?

I'm trying to convert pdf to images using pdf2image but getting problem of extra generated boxes.
This is my input pdf file screenshot
this in input file
from pdf2image import convert_from_path
images = convert_from_path('input_pdf.pdf',output_folder=r'C:\Users\Baith')
images[0].save('output.jpg')
after executing above code got this output
output_file
Since pdf2image is only a thin wrapper around pdftoppm, itself part of poppler, I would advise trying different parameters with the CLI tools to see it a specific combination works.
As for pdf2image itself, you might want to try use_cropbox=True and see if it still add lines.
Feel free to open an issue directly of the repository, if you can provide a sample PDF I would be happy to assist with the issue.

photo format returned in telethon

I am using telethon(a library for working with telegram). I can not figure out the output for photos. Can someone tell me how to convert this format to jpg viewer? Thanks
\x01\x17(\x91\xef\xa7G\xdaDx\xf6\xff\x00\xf5\xd4\xcbrH\xcf\xc8G\xa8\xff\x00\xf5\xd6la\xe5e\\\x92\t\xab\xe0\x05\x8c\x8c\xec\xe7\x08~\x9dk>g{"\xd4U\xaeL\x97\x06N\x13c\x11\xd4\x0ei%\x96\xe1W\xe4\x84\x13\xfe\xe9\xa8\xec\x14\x0b\xb9H\x1c\x15\xe2\xb4*\x96\xaa\xe2\x94l\xecQ\x13]y`\x98#>\x9bM\x15u\xf3\xb5\xbe\x94S$\xc8\x82\xd6Y\xd9\x19\xbe\xe9\x1ds\xcd>uX\x1b\x0e\xe3\'\x91\xc1\xa2\x8a\x96\x91Wc,\xa7\x8a\xdeWw\x9bp~\x80\x03\xd6\xae\xff\x00i[c;\x8f\xfd\xf3E\x15ih&\xee\xc6\xb6\xa9jP\xe1\xdb\xa7\xf7M\x14Q#\x8f
You need to download that image to be able to see it. to do so you could use the download_media method like so
path = client.download_media(messageThatHasTheImage)
that method will return where the image was saved by default.

How to load image using pygame.image.load()?

I just want to know the syntax. How do I load an image using pygame.image.load() ?
Let's take an example, I want to load an image called cat.png - and type this
pygame.image.load('cat.png')
Then, where should the image cat.png be saved?
By default, Python only searches in your current directory for files, however, if you wish to load images from a separate directory, you may find this useful:
Importing images from a directory (Python)
For the syntax, I reccomend you refer to the documentation, here:
https://www.pygame.org/docs/ref/image.html#pygame.image.load
pygame.image.load()ΒΆ
load new image from a file
load(filename) -> Surface
load(fileobj, namehint="") -> Surface
Load an image from a file source. You can pass either a filename or a Python file-like object.
Any images loaded directly with
pygame.image.load('Image Name')
should be saved in the same folder as the python script that is using the image

Can't make python urllib urlretrieve work. Resulting image is corrupted

I've successfully written a code that go through several urls, find a specific image in each of them, and saves its address. now i want to download the image.
I'm using this.
def update(name,set,url):
urllib.urlretrieve(url,"c:/path/"+set+"/"+url)
it is currently working, but the images this code obtains can't be opened. i get a message that says that either i don't have the proper update or the windows viewer can't open it because it doesn't support it

Uncompress and save zlib data in PDF with python

We get PDF files delivered to us daily and we need to get the images out. For example, what I want to do is to get the image back out of this PDF file I have, with python. Most pdf files we get are multipage and we want to export each embedded image to separate files. Most have jpeg files in them, but his one does not.
Object 5 is embedded as a zlib compressed stream. I am pretty sure it is zlib compressed because it is marked as FlateDecode and the start of the stream is \x78\x9c which is typical for zlib. You can see (part of) the hex dump here
The question is, how do I 'deflate' it and save the resulting file.
Thank you for sharing your wisdom.
I searched everywhere and tried many things but couldn't get to work. I managed to decompress the data like this:
import zlib
with open("MDL1703140088.pdf", "rb") as f:
pdf = f.read()
image = zlib.decompress(pdf[640:69307])
640 is zlib header(b'x\x9c') position and 69307 is the position of something like footer of pdf spec. b'\nendstream\n' is there. Detail is in the spec and some helpful Q&A can be found here. But omitting the end position is allowed in this case because decompress() seems to ignore following non-compressed data. You can validate this by:
decomp = zlib.decompressobj()
image = decomp.decompress(pdf[640:])
print(decomp.unused_data) # starts from b'\nendstream\n
So far so good. But when I write image to a PNG file, it cannot be read by any image viewer. Actually decompressed data looks so quite empty here and there. I attached some PNG header, but no luck. Hey, it's too much...
As I said earlier (strangely my comment was removed by someone), you'd better use some other existing tools. If Acrobat is not your option, what about pdftopng (part of Xpdf)? pdftopng MDL1703140088.pdf . gave me a valid PNG file flawlessly. Obviously command-line tools can be executed in Python, as you may know.

Categories

Resources