How to save an image of any format in Python?


I have been investigating the way to save an image in python but so far I have not found anything that can be useful, what I need is to know that import have and how to save the image in python, this image will keep it in a database in mongodb, but I still do not know how to save an image in python could you help me, the closest I've found is this:

Guradar image in python 3.x

I would thank you in advance for your help.


Thanks to @EdgarAlejandro I managed to finish my code that I was looking for and I want to share them with you

#!/usr/bin/env python
import Image
import pymongo
import base64

from PIL import Image, ImageOps
from pymongo import MongoClient
con = MongoClient()
db = con.test

image = 'masexito.jpg'

img ='/home/santiago/Documents/Python/img/exito2.jpg')
#encoded = base64.b64encode(img)
with open(image, "rb") as image_file:
    encoded_string = base64.b64encode(

user = {
    'nombre' : 'Saul',
    'edad' : 38,
    'localidad' : 'Monterrey',
    'curp' : 'NDUBD88DND87',
    'imagen' : encoded_string

use = db.users

print 'Insertado Correctamente' + str(use.inserted_id)

for row in use.find():
    print row

P.D .: They must have installed the corresponding libraries

asked by Santiago Huh 07.08.2017 в 19:17

1 answer


You need Pillow

from PIL import Image, ImageOps

image = request.FILES['logo'] #imagen que usuario sube desde la web o
image = '/path/to/image.png'  #imagen de tu PC

#debes usar la variable image dependiendo si es desde la web o tu PC

img ='path donde guardas')
answered by 07.08.2017 / 19:35