Simple python image uploader using CGI - python

I'm looking for a script that can allow someone to simply upload an image using CGI and will store the image in my /img/ folder. I tried using this one (http://webpython.codepoint.net/cgi_file_upload) but it gave this error:
<type 'exceptions.KeyError'>: 'file'
args = ('file',)
message = 'file'
Really anything simple will do. Just upload an image and save it.
Thanks!

Related

How to get GCP Function generated image to be recognized as valid in GC Storage?

I am trying to create an image thumbnail creation function using python, running in a google cloud platform's function. The image is sent as a base64 string to the cloud function, manipulated and made smaller with Python's Pillow package. It is then uploaded as an image, going from a Pillow Image object, to a BytesIO object, then saved to google cloud storage. This is all done successfully.
The problem here is very strange: Google Cloud Storage does not recognize the image until an access token is created manually. Otherwise, the image is left in an infinite loop, never loading, and never being able to be used.
I have reviewed this SO post, which has a very similar problem to mine (the image here shows exactly my problem: an uploaded file cannot be loaded properly), but it differs in two imporant categories: 1) They are manipulating the image array directly, while my code never touches it and 2) they are working in Node.js, where the Firebase SDK is different than in Python.
The code to generate the image is as follows:
def thumbnailCreator(request):
# Setting up the resourcse we are going to use
storage_client = storage.Client()
stor_bucket = storage_client.bucket(BUCKET_LINK)
# Retriving the Data
sent_data = request.get_json()['data']
name = sent_data['name']
userID = sent_data['userID']
# Process to go between base64 string to bytes, to a file object
imageString = stor_bucket.blob(PATH_TO_FULL_SIZE_IMAGE).download_as_string()
imageFile = BytesIO(imageString)
image = Image.open(imageFile)
# Resizing the image is the goal
image = image.resize(THUMBNAIL_SIZE)
# Go between pillow Image object to a file
imageFile = BytesIO()
image.save(imageFile, format='PNG')
imageBytes = imageFile.getvalue()
image64 = base64.b64encode(imageBytes)
imageFile.seek(0)
# Uploading the Data
other_blob = stor_bucket.blob(PATH_FOR_THUMBNAIL_IMAGE)
other_blob.upload_from_file(imageFile, content_type = 'image/png')
return {'data': {'response': 'ok', 'status': 200}}
Again, this works. I have a feeling there is something wrong with the MIME type. I am a novice when it comes to this type of programming/networking/image manipulation, so I'm always looking for a better way to do this. Anyway, thanks for any and all help.
It appears that the premise of this question - that a access token must be made manually for the image to work - is not accurate. After further testing, the error came from other parts of the code base I was working in. The above python script does work for image manipulation. An access token to the image can be generated via code, and be provided client-side.
Leaving this up in case someone stumbles upon it in the future when they need to work with Pillow/PIL in the Google Cloud Platform.

How can i get someone's profile pic on Discord to edit it using PIL?

I'm trying to make some code on python to edit someone's profile pic, but all I've got so far is this:
image = ctx.message.author.avatar_url
background = Image.open(image)
Apparently that just gets the URL itself, but i need the image itself to edit a picture with PIL. Any insight on how to get it?
with requests.get(ctx.message.author.avatar_url) as r:
img_data = r.content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)
So I played about with this link a bit:
https://cdn.discordapp.com/avatars/190434822328418305/6a56d4edf2a82409ffc8253f3afda455.png
And I was able to save my own avatar image (the one I use for my accounts everywhere). I was then able to open the file regularly with the photo viewer app within Pycharm.
After, it would simply become a case of opening the new jpeg file with PIL or pillow instead of trying to open anything from a website, if that makes sense.
You should consider that this will save a file onto your Discord bot server, so this is extremely crude, a malformed or maliciously formed jpeg file could lead to some sort of remote vulnerability.
Furthermore to your comment, if you want the size of the image you download to be bigger, for example, please see the amended link below to solve your problem there:
https://cdn.discordapp.com/avatars/190434822328418305/6a56d4edf2a82409ffc8253f3afda455.png?size=<Number from list [16,32,64,128,256,512,1024,2048]>
Hope this helps :)

Openstack python API: how to download image from glance using the python api

I am trying to write a python program to download images from glance service. However, I could not find a way to download images from the cloud using the API. In the documentation which can be found here:
http://docs.openstack.org/user-guide/content/sdk_manage_images.html
they explain how to upload images, but not to download them.
The following code shows how to get image object, but I don't now what to do with this object:
import novaclient.v1_1.client as nvclient
name = "cirros"
nova = nvclient.Client(...)
image = nova.images.find(name=name)
is there any way to download the image file and save it on disk using this object "image"?
Without installing glance cli you can download image via HTTP call as described here:
http://docs.openstack.org/developer/glance/glanceapi.html#retrieve-raw-image-data
For the python client you can use
img = client.images.get(IMAGE_ID)
and then call
client.images.data(img) # or img.data()
to retrieve generator by which you can access raw data of image.
Full example (saving image from glance to disk):
img = client.images.find(name='cirros-0.3.2-x86_64-uec')
file_name = "%s.img" % img.name
image_file = open(file_name, 'w+')
for chunk in img.data():
image_file.write(chunk)
You can do this using glance CLI with image-download command:
glance image-download [--file <FILE>] [--progress] <IMAGE>
You will have to install glance cli for this.
Also depending upon the cloud provider/service that you are using, this operation may be disabled for regular user. You might have to check with your provider.

Transformed image from GAE Images API upload to Facebook

Does anyone know how to convert a GAE Images API Image object into a "file like object"?
I'm trying to upload an image that I've transformed from the GAE Images API to Facebook. I'm using the execute_transforms function, which returns a "Image Representation" of the image.
I've tried to upload it using the following code, but I get a FB API Error "No Uploaded Image"
img = images.Image(ORIGINAL_IMAGE)
img.crop(0.0, 5.0/img.height, 713.0/img.width, 644.0/img.height)
output = img.execute_transforms(output_encoding=images.PNG)
graph = fb.GraphAPI(access_token)
graph.put_photo(output, 'Look at this cool photo!')
I think the issue is that output is not a "file like object" which is what put_photo requires, but the GAE docs doesn't have a function for casting into a "file like object". Tried creating temp files and writing to them, but GAE doesn't allow writing to filesystem. I've also tried to write to a StringIO object, but that didn't work.
Thanks
Use StringIO
enter code here
import StringIO
graph.put_photo(StringIO.StringIO(output), 'Look at this cool photo!')
Have a read of the docs at http://docs.python.org/library/stringio.html

how to save image using python?

I want to save a .jsp image from a web page in to my computer using python.
I have tried many methods including
retrieve function in mechanize and
urllib.urlretrieve('http://example.com/img.jsp', 'img.jsp')
but the problem is when I try to open the image using the image library it throws the following error
File "code.py", line 71, in extract_image
im = Image.open(image_file)
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
I have even tried saving the image in .png format, but its not working.
But I can do the save manually by going to the image url and then saving the image.
Pls help!
You haven't provided enough information, but my guess is that the web server isn't responding the way you think it is -- did you peek the HTTP traffic with Fiddler or Firebug or look at what's in the file?
Can you get a copy of the image some other way -- if so, compare that to what you downloaded programmatically.
Finally, I am not sure what a JSP image is -- if img.jsp is a JavaServerPage that responds with an image, that doesn't make the image a JSP image -- it's still in the format that corresponds to its Content-type.

Categories