How to save image in-memory and upload using PIL? - python

I'm fairly new to Python. Currently I'm making a prototype that takes an image, creates a thumbnail out of it and and uploads it to the ftp server.
So far I got the get image, convert and resize part ready.
The problem I run into is that using the PIL (pillow) Image library converts the image is a different type than that can be used when uploading using storebinary()
I already tried some approaches like using StringIO or BufferIO to save the image in-memory. But I'm getting errors all the time. Sometimes the image does get uploaded but the file appears to be empty (0 bytes).
Here is the code I'm working with:
import os
import io
import StringIO
import rawpy
import imageio
import Image
import ftplib
# connection part is working
ftp = ftplib.FTP('bananas.com')
ftp.login(user="banana", passwd="bananas")
ftp.cwd("/public_html/upload")
def convert_raw():
files = os.listdir("/home/pi/Desktop/photos")
for file in files:
if file.endswith(".NEF") or file.endswith(".CR2"):
raw = rawpy.imread(file)
rgb = raw.postprocess()
im = Image.fromarray(rgb)
size = 1000, 1000
im.thumbnail(size)
ftp.storbinary('STOR Obama.jpg', img)
temp.close()
ftp.quit()
convert_raw()
What I tried:
temp = StringIO.StringIO
im.save(temp, format="png")
img = im.tostring()
temp.seek(0)
imgObj = temp.getvalue()
The error I'm getting lies on the line ftp.storbinary('STOR Obama.jpg', img).
Message:
buf = fp.read(blocksize)
attributeError: 'str' object has no attribute read

For Python 3.x use BytesIO instead of StringIO:
temp = BytesIO()
im.save(temp, format="png")
ftp.storbinary('STOR Obama.jpg', temp.getvalue())

Do not pass a string to storbinary. You should pass a file or file object (memory-mapped file) to it instead. Also, this line should be temp = StringIO.StringIO(). So:
temp = StringIO.StringIO() # this is a file object
im.save(temp, format="png") # save the content to temp
ftp.storbinary('STOR Obama.jpg', temp) # upload temp

Related

Not able to save Image by downloading in python

I need to download a png file from a website and save the same in local directory .
The code is as below :
import pytesseract
from PIL import Image
from pathlib import Path
k = requests.get('https://somewebsite.com/somefile.png',stream =True)
Img=Image.open(k) # <----
Img.save("/new.png")
while executing it in JupyterNotebook
If I execute, i always get an error "response object has no attribute seek"
On the other hand , if I change the code to
Img= Image.open(k.raw), it works fine
I need to understand why it is so
You can save image data from a link using open() and write() functions:
import requests
URL = "https://images.unsplash.com/photo-1574169207511-e21a21c8075a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=880&q=80"
name = "IMG.jpg" #The name of the image once saved
Picture_request = requests.get(URL)
if Picture_request.status_code == 200:
with open(name, 'wb') as f:
f.write(Picture_request.content)
Per pillow the docs:
:param fp: A filename (string), pathlib.Path object or a file object.
The file object must implement file.read,
file.seek, and file.tell methods,
and be opened in binary mode.
response itself is just the response object. Using response.raw implements read, seek, and tell.
However, you should use response.content to get the raw bytes of the image. If you want to open it, then use io.BytesIO (quick explanation here).
import requests
from PIL import Image
from io import BytesIO
URL = "whatever"
name = "image.jpg"
response = requests.get(URL)
mybytes = BytesIO()
mybytes.write(response.content) # write the bytes into `mybytes`
mybytes.seek(0) # set pointer back to the beginning
img = Image.open(mybytes) # now pillow reads from this io and gets all the bytes we want
# do things to img

python generate QR code png without file output

I am using pypng and pyqrcode for QR code image generation in django app.
import os
from pyqrcode import create
import png # pypng
import base64
def embed_QR(url_input, name):
embedded_qr = create(url_input)
embedded_qr.png(name, scale=7)
def getQrWithURL(url):
name = 'url.png'
embed_QR(url, name)
with open(name, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode('utf-8')
return image_data
When I call getQrWithURL with a url, it produces a file url.png to my directory. Is there a way to only get the image data without producing a file output?
thanks for your help.
Use a BytesIO as a writable stream:
import io
# Make a writeable stream
buffer = io.BytesIO()
# Create QR and write to buffer
embedded_qr = create(url_input)
embedded_qr.png(buffer,scale=7)
# Extract buffer contents - this is what you would get by reading a PNG disk file but without creating it
PNG = buffer.getvalue()
Your variable PNG now contains this:
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xe7\x00\x00\x00\xe7\x01\x00\x00\x00\x00\xcd\x8f|\x8d\x00\x00\x01CIDATx\x9c\xed\x971\x92\x830\x0cE\xc5PPr\x04\x8e\x92\xa3\xc1\xd1|\x14\x8e\x90\x92\x82\xb1V_2\t\xde\xb0;i?c\x15q\xecG\xf3\x91\xfc%D\xff\x89,\x8d6\xda\xe8\x97\xf4)\x88AW\xfb\xed\xb7Q\x93\xef\'fj\x7ft\x1f\x9e\xf2Xt\xb7\xe34\x97Cb\x9a\xa43\x8a\xe3XL\xfd-\xa8\xc8\x1c\x19\xbc\x11-\xbb\x1b\xd0\xa3&m\x11\xe8\xbd\xaeX"\x1a\xbe\x01\xa1Q\x9aW\xaeBE\x8fXe\xee\xf4\x1c\xc44\xcd\x19\n\x93dX\xfcc\xb1\xcd\xc8L\x91A\x08\xb5c\xd5\xcd\x1f\xea\xb7*\xbfl\xd4\xf4\x9ao\xb8\xdeB\xbb\xd3-c\xa4h\xbc\x9dn\xa3\xd5\xa4\t\x1dW\xdf\t5\x9dt\xb1b,N\x88\xc8}\xe5\x93t\xd4\x8aQ\xa1Pp\xbd\x06\xe43\x9f\x9d\x90\x90\xfa-\xdb\xa3\xe3b\xa2\xc0Rw+6\n\',U\x18S\xdfo\xdf\xa0\xa3\x18\xf70J\xac\xf2\xea\xc6\xf5\xdb\xa0\xa3%\xdc>"\x91R\xbd\r>z\xccHq\xcb|T\xba\x98\xfa\xa8\xe8G1J\xcfN\xfd[\xc3/[x\xbbQ\x04=\x8d\xfek\x16\xafn\xf1\xfc\x14m\x18BK\xef\x9a\xa8\xa9\xd7\xa4\'\xed\xfd\x902\xd3\xf0\r\x8c\x12z\xb4\xe1\x0fW\xa1\xa2\x7fF\xa3\x8d6\xfa\x15\xfd\x01\xb9MCH#\xc3\xa2\x96\x00\x00\x00\x00IEND\xaeB`\x82'

How do I download a JPG file from Amazon S3 into memory using Python?

I'm struggling to download a JPG file from Amazon S3 using Python, I want to load this code onto Heroku so I need to the image to be loaded into memory rather than onto disk.
The code I'm using is:
import boto3
s3 = boto3.client(
"s3",
aws_access_key_id = access_key,
aws_secret_access_key = access_secret
)
s3.upload_fileobj(image_conv, bucket, Key = "image_3.jpg")
new_obj = s3.get_object(Bucket=bucket, Key="image_3.jpg")
image_dl = new_obj['Body'].read()
Image.open(image_dl)
I'm getting the error message:
File ..... line 2968, in open
fp = builtins.open(filename, "rb")
ValueError: embedded null byte
Calling image_dl returns a massive long list of what I assume are bytes, one small section looks like the following:
f\xbc\xdc\x8f\xfe\xb5q\xda}\xed\xcb\xdcD\xab\xe6o\x1c;\xb7\xa0\xf5\xf5\xae\xa6)\xbe\xee\xe6\xc3vn\xdfLVW:\x96\xa8\xa3}\xa4\xd8\xea\x8f*\x89\xd7\xcc\xe8\xf0\xca\xb9\x0b\xf4\x1f\xe7\x15\x93\x0f\x83ty$h\xa6\x83\xc8\x99z<K\xc3c\xd4w\xae\xa4\xc2\xfb\xcb\xee\xe0
The image before I uploaded to S3 returned the below and that's the format that I'm trying to return the image into. Is anyone able to help me on where I'm going wrong?
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1440x1440 at 0x7F2BB4005EB0>
Pillow's Image class needs either a filename to open, or a file-like object that it can call read on. Since you don't have a filename, you'll need to provide a stream. It's easiest to use BytesIO to turn the byte array into a strem:
import boto3
from PIL import Image
from io import BytesIO
bucket = "--example-bucket--"
s3 = boto3.client("s3")
with open("image.jpg", "rb") as image_conv:
s3.upload_fileobj(image_conv, bucket, Key="image_3.jpg")
new_obj = s3.get_object(Bucket=bucket, Key="image_3.jpg")
image_dl = new_obj['Body'].read()
image = Image.open(BytesIO(image_dl))
print(image.width, image.height)
Try first to load raw data into a BytesIO container:
from io import StringIO
from PIL import Image
file_stream = StringIO()
s3.download_fileobj(bucket, "image_3.jpg", file_stream)
img = Image.open(file_stream)
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_fileobj

How to base64 encode an image using python

I have a stream of data that comes from a device and I need to convert it into a jpeg and then base64 encode it to transfer it over the network.
My Python 2.7 code so far looks like this:
from PIL import Image
import io
image = Image.open(io.BytesIO(self.imagebuffer)) # Image buffer contains the image data from the device.
image.save("test.jpg") # Writes the image to the disk in jpeg format
image.show() # Opens the image using the OS image view
I can see I have the image I want and can save it to the disk in jpeg format.
What I don't understand is if I can base64 encode the image from the image object or if I need to write it to the disk first. I would like to avoid writing it if possible.
The 2nd question I have is what is PIL doing in this process? Is it taking the data and putting the required special codes into the file to make it a jpeg file? I think the answer tot his is yes as I can change the file extension to .bmp and the correct file is written on the disk.
So in summary, is it possible to get a base64 encoded version of my jpeg file from the image object without writing it to disk first?
Try this code
Image base64 encoded format
Python code:
import os
import base64
image = 'test.jpg'
encoded_string = ""
with open(image, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
file = encoded_string
This code does the job:
from PIL import Image
import io
import base64
import cStringIO
image = Image.open(io.BytesIO(imagebuffer))
encodingbuffer = cStringIO.StringIO()
image.save(encodingbuffer, format="JPEG")
encodedimage = base64.b64encode(encodingbuffer.getvalue())
Save the img in a buffer - doesn't touch disk
regex for jpeg headers and footers - fault tolerance ( Header: FF D8 FF, Footer: FF D9)
base64 the data
flush to file
I`m working with .dwg file and Python 2.7, this works for me:
import os
import base64
# Open the file
infile = open(input_file, 'r')
# 'r' says we are opening the file to read, infile is the opened file object that we will read from
# encode file to base64
base64EncodedStr = base64.b64encode(infile.read())
Please try this to base64 encode an image using python
import base64
with open("test.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
print(encoded_string)
OR
import base64
image = open('test.jpeg', 'rb')
image_read = image.read()
image_encode = base64.b64encode(image_read)
print(image_encode)

Image handling in Python with Numpy

We are importing an screen capture from a web page direct into a variable in Python; and then producing a Numpy array using the following code :
To capture is a PNG image (note - the device url has an embedded cgi to do the capture work) :
response = requests.get(url.format(ip, device), auth=credentials)
Once screen is captured, covert to a Numpy array called image :
image = imread(BytesIO(response.content))
After analysis of image, we would like to FTP the captured PNG to a server for reference at a later date. The best solution we can find right now involves using imsave to create a file locally and then FTP with storbinary to take the local image and put it on the server.
Is it possible to FTP response.content; or a conversion of the numpy array back into a PNG (using imsave?) direct to the server and skip the local storage step?
Update
As per MattDMo comment, we tried:
def ftp_artifact (ftp_ip, ftp_dir, tid, artifact_name, artifact_path, imgdata) :
ftp = FTP(ftp_ip)
ftp.login("autoftp","autoftp")
ftp.mkd ("FTP/" + ftp_dir)
ftp.cwd("FTP/" + ftp_dir)
filepath = artifact_path
filename = artifact_name
f = BytesIO(imgdata)
ftp.storbinary ('STOR ' + filename, f)
ftp.quit()
Where imgdata is the result of io.imread. The result file is 5x bigger and not an image. The BytesIO object is the numpy array I presume?
In the ftplib module, the FTP.storbinary() method takes an open file object as its second argument. Since your BytesIO object can act as a file object, all you'd need to do is pass that - no need for a temporary file on the server.
EDIT
Without seeing your full code, what I suspect is happening is that you are passing the NumPy array to storbinary(), not the BytesIO object. You also need to make sure the object's read pointer is at the beginning by calling bytesio_object.seek(0) before uploading. The following code demonstrates how to do everything:
from ftplib import FTP
from io import BytesIO
import requests
r = requests.get("http://example.com/foo.png")
png = BytesIO(r.content)
# do image analysis
png.seek(0)
ftp = FTP("ftp.server.com")
ftp.login(user="username", passwd="password")
# change to desired upload directory
ftp.storbinary("STOR " + file_name, png)
try:
ftp.quit()
except:
ftp.close()
Took a bit of research but our student figured it out :
def ftp_image_to(ftp_ip, ftp_dir, filename, data):
ftp = FTP(ftp_ip)
print("logging in")
ftp.login('autoftp', 'autoftp')
print("making dir")
ftp.mkd('FTP/' + ftp_dir)
ftp.cwd('FTP/' + ftp_dir)
print("formatting image")
bytes = BytesIO()
plt.imsave(bytes, data, format='png')
bytes.seek(0)
print("storing binary")
ftp.storbinary('STOR ' + filename, bytes)
ftp.quit()
Thanks IH!

Categories