how to save image using python? - 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.

Related

The extension in image url is different from actual format

I downloaded an image from a url such as "https://www.xxxx.com/filename.jpeg. I expected that that image is a jpeg image whose format is acceptable for Computer Vision Annotation Tool (CVAT). However, it was saved as filename.heif or filename.jpeg.heif, so it causes an error when I tried to create a task with that image because heif format is not acceptable in CVAT. (CVAT automatically downloads images and create a task once I put image urls and submit them.)
I usually put more than 1000 image urls to create a task, and it is really hard to find invalid url or image among them.
Is there any way to find the "actual format" only by looking at the image url? Or can I just skip invalid urls in CVAT?
Thank you.

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 :)

Is there a way to resize jpgs via http in python?

I am writing a webcrawler that finds and saves the urls of all the images on a website. I can get these without problem. I need to upload these urls, along with a thumbnail version of them, to a server via http request, which will render the image and collect feature information to use in various AI applications.
For some urls this works no problem.
http://images.asos-media.com/products/asos-waxed-parka-raincoat-with-zip-detail/7260214-1-khaki
resizes into
http://images.asos-media.com/products/asos-waxed-parka-raincoat-with-zip-detail/7260214-1-khaki?wid=200
but for actual .jpg images this method doesn't work, like for this one:
https://cdn-images.farfetch-contents.com/11/85/29/57/11852957_8811276_480.jpg
How can I resize the jpgs via url?
Resizing the image via the URL only works if the site you're hitting is using a dynamic media service or tool in their stack. That's why ASOS will allow you to append a query with the dimensions for resize, however different DM tools will have different query parameters.
If you want to make it tolerant you're best off downloading the image, resizing it with Python and then uploading it.

Image Uploading Script in Python

I was trying to make an image uploading script for Postimage.org.
I tried searching for an API but it seems that there is not any available. Can anyone help me how to make this script ? I don't have any idea how to make the uploading proccess? I think that something that i should do is open the image file in read binary mode ("rb").
Anyway i am waiting for your suggestions and ideas.
Firstly you should think about what happens when you press the upload button on the website. What your script could do is mimic this functionality, because essentially all it's triggering is a POST request to the web server with the specified information in the form and the image file data. You can initiate HTTP requests (e.g. GET, POST, etc.) using a library such as Requests (http://docs.python-requests.org/en/latest/index.html).
However, as this seems to have been discussed before, I will instead point you in the right direction: Send file using POST from a Python script

Grabbing a .jsp generated PNG in Python

I am trying to grab a PNG image which is being dynamically generated with JSP in a web service.
I have tried visiting the web page it is contained in and grabbing the image src attribute; but the link leads to a .jsp file. Reading the response with urllib2 just shows a lot of gibberish.
I also need to do this while logged into the web service in question, using mechanize. This seems to exclude the option of grabbing a screenshot with webkit2png or similar.
Thanks for any suggestions.
If you use urllib correctly (for example, making sure your User-Agent resembles a browser etc), the "gibberish" you get back is the actual file, so you just need to write it out to disk (open the file with "wb" for writing in binary mode) and re-read it with some image-manipulation library if you need to play with it. Or you can use urlretrieve to save it directly on the filesystem.
If that's a jsp, chances are that it takes parameters, which might be appended by the browser via javascript before the request is done; you should look at the real request your browser makes, before trying to reproduce it. You can do that with the Chrome Developer Tools, Firefox LiveHTTPHeaders, etc etc.
I do hope you're not trying to break a captcha.

Categories