I am trying to invert an image using PIL (python-imaging-library) and PIL.ImageOps, can I somehow save that image as a URL so I can enter it into my web browser?
-
see this link: https://stackoverflow.com/questions/7877282/how-to-send-image-generated-by-pil-to-browser, is this your answer? – I'mahdi Aug 01 '21 at 16:47
-
Please clarify what you are trying to do. Do you want to create a URL for serving on the web, e.g. ``https://www.example.com/some_image.jpg``, a URL for serving locally, e.g. ``https://127.0.0.1/some_image.jpg``, or a just any URI that can be used by a browser, e.g. ``file://tmp/some_image.jpg``? – MisterMiyagi Aug 01 '21 at 16:51
2 Answers
If by 'as a URL' you mean for the image to be hosted using some web service, I would use something like a Google Cloud Platform or AWS Bucket. Note that these will require a credit card, but offer free trials so you have a few months with which to experiment.
Set up a GCP Bucket and download the credentials file and name is something like 'auth.json'. After saving the PIL file to disk, you would then be able to upload to your bucket with the following code:
from google.cloud import storage
import uuid
def upload_image(img_path, ext):
# uploads a single image with given path and ext up to a gcp bucket
if ext != "png" and ext != "jpg":
print("only .png and .jpg filetypes are supported")
return
storage_client = storage.client.Client.from_service_account_json(
"AUTHFILE.json", project="PROJECTNAME")
# using the passed in credentials from the json file, access the GCP project
bucket = storage_client.get_bucket("BUCKETNAME")
# grab the bucket from the project
path = img_path
# path to the image
imageID = uuid.uuid4()
blob = bucket.blob(f"FOLDERNAME/{imageID.hex}.{ext}")
# make the name into a uuid (so its unique every time), hex so its shorter
if ext == "jpg":
blob.content_type = "image/jpeg"
else:
blob.content_type = "image/png"
# make sure file extension and blob type match
with open(path, 'rb') as f:
blob.upload_from_file(f)
print(f"Image successfully uploaded as {imageID.hex}.{ext}.")
upload_image("myImage.png", "png")
If you want to make it publicly visible, change the permissions of allUsers
under the Permissions
tab.
Here is an example image hosted on the web using a GCP Bucket: https://storage.googleapis.com/digichef/images/f7bd821df9a6429e8656f24bd21b458a.png

- 600
- 5
- 13
Seeing this question, you might have misunderstood what is considered a "URL".
URL does not only include absolute paths (like https://www.stackoverflow.com
). It also includes relative paths. Examples of relative paths are file.txt
and c:/Documents/school/financial.xls
.
Mostly, you can view a file with file:///
plus a full path. The second example I listed above is a full path. Please check here for additional information.
If you want to put your photo on the internet, use a third-party service. Cubeupload is the one I use, but feel free to experiment with others and see which one you like the most.

- 173
- 1
- 15