How can I add jpeg compression to an image programatically? - python

I'm not asking for complete source code, just the gist of it. I essentially want to open a photo in python to add jpeg artifacts to it and resave it. Is this possible? I haven't been able to find anything on this.

Related

pdf in python which consist data from .xlsx file and png image

I wanted to create a pdf using Python 3x.
The pdf should have some text data which is stored in a .xlsx file i.e.., it should read data from .xlsx file and write into the .pdf file.
Along with that, the pdf should have a png image of passport size.
I have come up with two basic ideas which are:-
First one is by writing a program which create a text file in which all required data from the pdf will be written along with the png image. After that the program will convert it into a pdf file.
Second one is by writing a program which will create the pdf file and write the data from .xlsx file as well as insert the image too into the pdf file.
I don't know whether these ideas can be used or not and how it can be used but after going through some researches on GFG, Stack overflow..., I have got totally confused and ended up asking this problem on this platform.
I have tried some modules like PIL, FPDF, reportlab,.. and am successfully able to create a pdf file with either texts or images but unable to combine both in the same text file.
Also I am confused in deciding which idea I should implement.
What I need from you guys is the answer of few of my questions which are:-
Are the ideas I mentioned above(second one specially) practically possible?
Can I make a program which imports data from file as well as png image into the same pdf. What modules and functions will be used there and how.
Please provide the code with comments or defining/elaborating the work of function used.
I hope I will get the desired result soon. Meanwhile I will try to solve it out by myself.

Batch proces Colour profiles of images to any profile needed with python

Im working on a project that needs to process images from one file and output to another the change is varied but the main one is colour profiles that need to be changed however everything i see so far is only able to convert to sRGB or that range but i would need to ether be able to add a profile or have an extensive or full list of profiles to convert to. for example one of the profiles ill be needing to use is eciRGB v2.
Please help me to automate this in python (I cant use photoshop...) .
You can do this with ImageMagick and terminal.
Download the colour profile(eciRGB_v2.icc) and make sure you know the path to it, I recommend keeping it in the same directory as your images.
example set up
Then open Terminal at that directory and run this code:
convert image.tif -profile eciRGB_V2.icc output.tif
example of result
Link to download ImageMagick: https://imagemagick.org/index.php

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.

Qt - Loading image with wrong extension

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.

Adding EXIF data to Images

I want to add modify exif data in an image by modifying the ImageDescription tag. I use the library from the following:
https://github.com/bennoleslie/pexif
I am able to modify the ImageDescription tag and read the modified tag after writing it to another image file. But now when i upload the image to imgur and instagram and download the image again, and read the exif data, the modified ImageDescription tag is not there anymore in the exif data. To read the exif data, i use tools like exiftool and identify -v, but none them display the modified ImageDescription. I also used the above pxeif library to read the tag name ImageDescription and its not there. Any suggestions for why this is happening?
Below is the code i used using the pexif library and the image is a .jpg:
img = pexif.JpegFile.fromFile(path_to_images + image)
image_id = image.split('.')[0]
img.exif.primary.ImageDescription = image_id
img.writeFile(path_to_encoded_images + image_id + "_encoded.jpg")
This likely isn't the fault of your code or the library. Many image hosting services deliberately strip exif data. So, even if the data is well formed, don't count on it surviving imgur or instagram (or others for that matter).
See http://imgur.userecho.com/topic/42809-where-is-the-exif-data/ and http://www.embeddedmetadata.org/social-media-test-results.php
As to why they do this, there are probably various answers. Probably one big one is that less savvy users won't unwittingly post their gps coordinates all over the place via geo tagging.

Categories

Resources