I am currently able to download an entire image from a URL using
from PIL import Image, ImageTk
import cStringIO
import urllib
url = http://prestigemgmt.us/wp-content/uploads/2013/03/dog.jpg
self.image = Image.open(cStringIO.StringIO(urllib.urlopen(url).read()))
It works fine and gets the whole image from the website. My question is there any way to get, lets say only the right half of the image.
I understand I could edit the image after it is downloaded, but speed is an important aspect, so ideally I would download only what I need.
It is not possible to do this.
The common image file formats like PNG and JPEG are encoded in a manner that you cannot download an arbitrary part of the picture without downloading the full picture.
You need to download the whole picture, decode it and edit it after the download.
For the advanced knowledge you can always study PNG and JPEG file formats.
If you are in the control of the server providing the images you can write a server-side script which edits the image on the server and then sends the edit over the wire.
Related
I am exporting some matplotlib plots from Databricks to Blob Storage. No problem here, just using:
plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
The problem is that when I download and open the image locally, it is blank. I am not sure if I have to transform it or at some point specify the metadata, cause it appears as application/octet-stream in the Blob.
any ideas?
• When you are trying to download and open the image locally, you are still seeing the image as blank because you are using the command as posted by you for saving them in readable image format in the Azure blob storage, which is correct but when you are showing the plot or downloading it, you should use the command ‘plt.show('/dbfs/my_plot.png')’ before the below commands as shown: -
plt.show('/dbfs/my_plot.png')
plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
• If you are not using the above command as said, you will only get the image as blank only. Also, if you don’t want undesirable white space around your image, you can remove that using the command as below as this white space may also be the reason behind your image showing as blank as the space might have consumed greater area than the actual plot.
plt.savefig('/dbfs/my_plot.png', bbox_inches='tight')
For more information and details regarding the solution for this problem, kindly refer to the below community thread: -
Save plot to image file instead of displaying it using Matplotlib
I have a script that is downloading and performing certain operations on files from a website. A couple of times a week some of the images will throw this error (edit for bump):
OSError: cannot identify image file '57343948435235236aede7dceb672559.png'
Even when manually running code as simple as the following, it produces the error:
imagefile = Image.open('57343948435235236aede7dceb672559.png')
Here are the important facts:
If I redownload the file using requests.get() I get the same error when attempting to open it.
If I redownload the file by saving it using a web browser, I instead get the error PIL.UnidentifiedImageError: cannot identify image file when attempting to open it using PIL
I can view the files (using Window Photo Viewer, a Linux image viewer, etc.) so they aren't completely corrupted.
If I open the image in Paint and save it under the same name, I can then open it using PIL.
Given all this, it appears to be an issue with specific image files hosted on the site; they are viewable in applications, but not openable using PIL. I'm assuming something is wrong with the byte structure or headers in the image, and saving overwrites those issues. Is there some library or automatic process I can use to 'fix' these images when the error occurs?
Below, I am including a minimal, reproducible example. PLEASE NOTE: THE EXAMPLE URL IN THIS CASE IS NSFW IN NATURE, AND THEREFORE HAS BEEN REDACTED. SEE MY COMMENT TO THIS POST FOR A PASTEBIN LINK TO THE FULL CODE.
from PIL import Image
url = 'SEE ABOVE NOTE'
file_name =(url).split('/')[-1:][0]
# Downloading the image file data.
file_data = get(url).content
# Write the data to a file.
with open(file_name, 'wb+') as file_object:
file_object.write(file_data)
# Opening the image with PIL
with Image.open(file_name) as image_file:
# "OSError: cannot identify image file" happens here
pass
I have the following problem:
1.- I want to send a file in discord without dowloading. I don't know if this is posible but I want to send it for example with BytesIo.
2.- I have one picture saved on my Bot files and the other one comes from ctx.author.avatar
3.- I want to blend both images and send the result. With blend I mean like for example if I would be using cv2 I would use addWeighted().
The Code I have right know what does is dowload the picture of the member, using cv2 to read both pictures, resize them and use addWeighted. After that I save the blend picture and I send it as a message. When all It's done I delete the pictures (both, the avatar and the blend one). From my point of view this is really inefficient, thats why I want know if there is a way using PIL and BytesIo or something to use the dataArray to blend them and send it without dowload it.
So in short, I want to know if there is a way to blends both images without download the second one (member avatar picture) and send it without dowloading the blend image.
I can the code I already have if needed but as my code is download the picture I guess that won't help.
You can get the image's URL from the message (Get a picture from the message):
message.attachments[0].url
Then load the image into memory with the requests library (How to open an image from an url with opencv using requests from python)
The way I found at the end to solve it, was using just Pillow doing the following:
image1 = Image.open("img.jpg", mode='r')
image2 = Image.open(requests.get(url, stream=True).raw)
With I have both images on memory, so I just needed to resize both of them and blend. Finally I just used io.Bytes() to make into bytes and send it.
How to blend -> https://pythontic.com/image-processing/pillow/blend
I am using PyCharm in order to download an image from a fixed URL
This is the code I'm using in order to do it:
import urllib.request
import random
def download_web_image(url):
name=random.randrange(0,1000)
fullname=str(name)+".jpg"
urllib.request.urlretrieve(url,fullname)
download_web_image('imagizer.imageshack.com/v2/626x626q90/673/MT82dR.jpg')
This is what happens when I double click the downloaded image:
But as you see the image is already downloaded in the Python directory and it is a proper image:
What do I have to do in order for the image to be properly displayed in Pycharm?
This has nothing to do with the fact that the image was downloaded using Python. You will see the same behaviour if you copy an image into your project folder. You can fix it by telling PyCharm about which file types should be displayed as images.
In Settings (File->Settings on Windows) expand Editor then select File Types
Choose Image from the list of Regognized File Types (scroll down...) then make sure that Registered Patterns contains all the file types you want to be displayed as images. If *.jpg is not listed there, add it using the green + on the right.
I'm generating some pdf's with Reportlab and Django using a web interface. The pdf's are always going to be a single page. I'd like to generate a png or jpg image of the generated page and return that to the browser for the user to preview before saving the final pdf and delivering it to the end user. Is there anyway to do this?
This answer explains that you can use ghostscript to convert pdf to png. Depending of the requirements of your app (traffic, response time, nb of pdfs ...) it may or may not be a solution for you.
This is just an idea, but may be you can generate the preview image in parallel using PIL ImageDraw and get rid of the pdf-to-png conversion.
I hope it helps