I am working in a mobile application that allows users to upload images. I process these images using a django backend.
I want to save resized image in server.
I create thumbnails of the image using PIL, but all exif data of the image is lost. I want to preserve the exif data in the resized image.
I am trying to use the gexiv2 library to copy the exif data of the original image and save it to the resized image:
exif = GExiv2.Metadata(file_path)
To presrve exif data, I save the resized image to the disk and gexiv2 uses this file path:
# exif of orginal image
exif = GExiv2.Metadata(file_path)
# exif of resized image
newExif = GExiv2.Metadata('img/512_c')
# save all exif data of orinal image to resized
for tag in exif.get_exif_tags():
newExif[tag] = exif[tag]
# edit exif data - size
newExif['Exif.Photo.PixelXDimension'] = str(im.size[0])
newExif['Exif.Photo.PixelYDimension'] = str(im.size[1]).
But my issue is, django gives me the client uploaded images either as a file path
or as a buffer.
When I get the file path, I have no issue to get the exif data of the original image using gexiv2.
When I get an image buffer, I can't directly get the exif data using gexiv2, since gexiv2 needs the file path parameter to get the exif, so I want to save the image buffer to disk temporarily.
What is the best method to save the image buffer to disk?
Finally I got the solution,
https://stackoverflow.com/a/11813684/658976
This confirms all images uploaded to server had file path (no buffered images).
Related
I am coding a python script which uses Pillow to create images.
In my code I import a jpeg background and another jpeg image which I'll combine with draw.rounded_rectangle() and draw.text().
Problem is when I save the image directly to a jpeg file, the colors are very different from the png image. It's like the jpeg image has a greyscale filter applied.
Here's my code to save the images :
background.save('fin2.jpg', format="jpeg", quality=100, subsampling=0)
background.save('fin2.png', format="png")
os.system('sips -s format jpeg fin2.png --out ' + 'fin3.jpg')
As you can notice I use Apple's sips (scriptable image processing system) to convert the png to jpeg and the output is fine.
Here are the output images :
fin2.jpg
fin2.png
fin3.png
Any idea how to fix this in the code ?
(I am unable to upload the pictures directly to stackoverflow atm this is why I use external links)
I am reading in a list of PNG images from a directory and I want to maintain the image sizes.
However, when I go to save them, each image is automatically resized to fit the entire page AND it automatically rotates the images to landscape rather than keeping them portrait.
Example of what I am doing below:
from PIL import Image
x = []
# get list of images
for i in [list of image files in my directory]:
im = Image.open(i)
im = im.convert('RGB') # will not save as RGBA
x.append(im)
im.save('my_doc.pdf', save_all=True, append_images=x)
Yet, the images which are all the same dimensions and sized to fit neatly onto a single page in portrait orientation, are being rotated to landscape orientation and stretched to the limits of the page making them blurry as a result.
I want one image per page, using the native dimensions of the images, and in portrait.
Is there a way to prevent PIL from doing this?
Thanks in advance!
I have some photos taken by UAV, and I find that the view of some photos read by cv2.imread and plt.imread are different, one of them is upside down (180° rotation), but most photos are the same.
Why?
Unlike matplotlib (PIL), opencv takes EXIF information into account by default, see documentation:
If EXIF information is embedded in the image file, the EXIF
orientation will be taken into account and thus the image will be
rotated accordingly except if the flags IMREAD_IGNORE_ORIENTATION or
IMREAD_UNCHANGED are passed.
(to achieve the same with PIL, use exif_transpose)
I have a one page 30KB PDF.
When I convert it to an image with default settings, the jpeg file is 486KB.
If I set the quality to 10, I can cut the size to 155KB but it's still significantly larger than the original PDF.
I am under the impression that a jpeg is usually smaller in file size than a pdf.
It's not a huge problem with just a few PDFs but converting thousands would require 5 or 6 times the disk space.
Is it possible to convert a PDF to JPEG and keep the file size at least the same without destroying the quality of the image?
Thanks
I'm using python 3.6 on Windows 10
from pdf2image import convert_from_path
poppler_path = r"C:\poppler-21.03.0\Library\bin"
images = convert_from_path(r'C:\test.pdf', poppler_path=poppler_path)
for image in images:
image.save('out.jpg', 'JPEG', optimize=True, quality=10)
I need to process large image jpeg files that do not contain thumbnails. I want to create thumbnails and insert into their Exif file, so that I can decide what processing is necessary later on.
On the PIL documentation https://pillow.readthedocs.io/en/3.1.x/reference/Image.html It says:
Image.thumbnail(size, resample=3)
Make this image into a thumbnail. This method modifies the image to contain a thumbnail version of itself, ...
These two sentences are contradictory: is the image now a thumbnail or the image now has an embedded thumbnail? It seems to be the former.