How use Gridfs of mongodb with PIL (Python Image Library) - python

i use mongodb and save file to gridfs
now i want edit images from gridfs ...
i use this code
def thumbnail(file_obj):
import StringIO
from PIL import Image
im = StringIO.StringIO()
im.write(file_obj.raw_file)
im_ful = Image.open(im)
return im_ful.info
but pil said "cannot identify image file"
thats image also ;)
how can fix it

You need an im.seek(0) before the Image.open(im) call. Otherwise PIL tries to read from the end of the file, gets no data, and fails.

Related

conveting bytes to image using tinytag and PIL

I am using tinytags module in python to get the cover art of a mp3 file and want to display or store it. The return type of the variable is showing to be bytes. I have tried fumbling around with PIL using frombytes but to no avail. Is there any method to convert the bytes to image?
from tinytag import TinyTag
tag = TinyTag.get("03. Me, Myself & I.mp3", image=True)
img = tag.get_image()
I actually got a PNG image when I called tag.get_image() but I guess you might get a JPEG. Either way, you can wrap it in a BytesIO and open it with PIL/Pillow or display it. Carrying on from your code:
from PIL import Image
import io
...
im = tag.get_image()
# Make a PIL Image
pi = Image.open(io.BytesIO(im))
# Save as PNG, or JPEG
pi.save('cover.png')
# Display
pi.show()
Note that you don't have to use PIL/Pillow. You could look at the first few bytes and if they are a PNG signature (\x89PNG) save data as binary with PNG extension. If the signature is JPEG (\xff \xd8) save data as binary with JPEG extension.

PDF to IMG to PDF all done in memory

In order to remove sensitive content from a PDF, I am converting it to image and back to PDF again.
I am able to do this while saving the jpeg image, however I would eventually like to adapt my code so that the file is in memory the whole time. PDF in memory -> JPEG in memory -> PDF in memory. I'm having trouble with the intermediary step.
from pdf2image import convert_from_path, convert_from_bytes
import img2pdf
images = convert_from_path('testing.pdf', fmt='jpeg')
image = images[0]
# opening from filename
with open("output/output.pdf","wb") as f:
f.write(img2pdf.convert(image.tobytes()))
On the last line, I am getting the error:
ImageOpenError: cannot read input image (not jpeg2000). PIL: error reading image: cannot identify image file <_io.BytesIO object at 0x1040cc8f0>
I'm not sure how to be converting this image to the string that img2pdf is looking for.
The pdf2image module will extract the images as Pillow images. And according the Pillow tobytes() documention: "This method returns the raw image data from the internal storage." Which is some bitmap representation.
To get your code working use BytesIO module like so:
# opening from filename
import io
with open("output/output.pdf","wb") as f, io.BytesIO() as output:
image.save(output, format='jpg')
f.write(img2pdf.convert(output.getvalue()))

How do I change my image format?

I'm trying to blur an image using PIL:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
When I do im.show() and save it to my hard drive, it saves as a BMP file, which is incompatible with the place where I'm trying to upload it. How do I change the file format from BMP to something else that is compatible?
Just use the save() function directly:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
im.save("saved.jpg")
This function supports many formats, as explained in the documentation.

How to get the format of image with PIL?

After loading an image file with PIL.Image, how can I determine whether the image file is a PNG/JPG/BMP/GIF? I understand very little about these file formats, can PIL get the format metadata from the file header? Or does it need to 'analyze' the data within the file?
If PIL doesn't provide such an API, is there any python library that does?
Try:
from PIL import Image
img = Image.open(filename)
print(img.format) # 'JPEG'
More info
https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format
https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html

Converting base64 images to base64 JPG

I have a feature with my application where a user can upload a photo. I would like to convert the photo into a JPG file and then upload it to my servers.
The photo is received in base64. I've seen other answers which suggest using PIL however, it saves the image to a local directory. Instead I would like to convert the received image into a base64 JPG image.
How would I do this? Thanks.
try this:
import base64
from PIL import Image
from io import BytesIO
im = Image.open(BytesIO(base64.b64decode(data)))
output = BytesIO()
im.save(output, 'JPEG')
jpg_img = output.read()
it will save the data to a stream ( the same way tou enter it) and then you can read from that stream

Categories

Resources