Django/Python resize image - python

I have a image from
files = request.FILES['new_photo'].read()
and i want to know it's size and possibly resize it.
How i can do it?
thanks.
UPD
before. i read() file and gave this string and some info? to SQL function. And she save it on ftp.
how i can get data same as the data returned from read() method?
i try to use Image.tostring()
and save it. But i have 0x0px image.

Here is a small snippet from one of my web applications (sightly modified), which creates a thumbnail with the maximum size of 200x200 pixels. Maybe you can use some parts of it:
from PIL import Image
image = request.FILES['new_photo']
if image:
img = Image.open(image)
img.save(path.join(app.config['UPLOAD_FOLDER'],
secure_filename('upload-%d.jpg' % self.obj.id)), 'JPEG')
img.thumbnail((200, 200), Image.ANTIALIAS)
img.save(path.join(app.config['UPLOAD_FOLDER'],
secure_filename('upload-%d.200.jpg' % self.obj.id)), 'JPEG')

you can use PIL for processing images .
http://www.pythonware.com/products/pil/

User one of the thumbnail libraries available such as http://djangothumbnails.com/ or more http://code.djangoproject.com/wiki/ThumbNails

If you would like to add "automated image processing" to your project, you might consider using Django-imagekit.
The author has included Installion and usage instructions on this Github Repo page.
Hope this helps you as I found it from the same search as I found this Question :)

Related

Discord.Py blend pictures and send them without dowload them

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

Downloading a section of an Image from a website in python?

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.

Get an image without seeing it first

I am currently working with SimpleCV for some image processing. Basically my goal is to have my application take a picture from a simple command in Python, i.e. 'GetPicture()'.
In SimpleCV, it is very easy to get an image from the camera, by using
cam = Camera()
img = cam.getImage()
My problem is that the first line actually shows the camera stream on the screen, and this I don't want. I would like to be able to get the image while displaying something else on the screen, thus taking the picture in 'blind mode'. Possible ?
Any help is much appreciated.
Julien
For those interested...
Eventually I used Pygame in combination with v4l (video for linux) driver. The v4l driver has a no-preview option, which once set, enables one to take a picture in 'blind' mode.
Julien

Loading an image from filename in a wx.Panel

I'm seeking a sample code to load a PNG image inside a wx.Panel, I've seen a couple of them using Python Imaging Library (PIL) and a hug bunch of code line.
I would like to keep my environment with as few library as possible, which means without PIL since I don't need to do any kind of image processing and I suppose that wx enable such processing.
Thanks
EDIT : code to achieve this from Mike's answer
image = wx.Image('path/to/image.png', wx.BITMAP_TYPE_ANY)
imageBitmap = wx.StaticBitmap(myPanel, wx.ID_ANY, wx.BitmapFromImage(image))
Then imageBitmap can be used as any other wx widget.
I wrote a really simple image viewer tutorial here that might help you: http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/ It just uses wxPython, I think.

Image preview with Reportlab?

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

Categories