How to save image using Python - python

I have an image file decoded by base64.
Now, I want to save the image file to the specified directory.
the directory is described as image_dir_path
image_dir_path = '/images/store/'
image_file = base64.b64decode(image_file)
How can I save the image_file to image_dir_path?
I tried shutil.copy(image_file, image_dir_path), but it doesn't work for my case.
I'm sorry I coundn't find the question like this.

You can write any content to a file with a file object and its write method. As an example, let's grab some base64 encoded data from the web:
import base64, urllib
decoded = base64.b64decode(urllib.urlopen("http://git.io/vYT4p").read())
with open('/tmp/31558315.png', 'w') as handle:
handle.write(decoded)
You should be able to open the file under /tmp/31558315.png as a regular image.

Related

How to Publish Docx file with images to WordPress site?

So I am able to post Docx files to WordPress using WP REST-API using mammoth docx package in Python
I am able to upload an image to WordPress.
But when there are images in the docx file they are not uploading on the WordPress media section.
Any input on this?
I am using python for this.
Here is the code for Docx to HTML conversion
with open(file_path, "rb") as docx_file:
# html = mammoth.extract_raw_text(docx_file)
result = mammoth.convert_to_html(docx_file, convert_image=mammoth.images.img_element(convert_image))
html = result.value # The generated HTML
kindly do note that I am able to see images in the actual published post but they have a weird source image URL & are not appearing in the WordPress media section.
Weird image source URL like
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQECAgMCAgICAgQDAwIDBQQFBQUEBAQFBgcGBQUHBgQEBgkGBwgICAgIBQYJCgkICgcICAj/2wBDAQEBAQICAgQCAgQIBQQFCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAj/wAARCAUABQADASIAAhEBAxEB/8QAHwAAAQMFAQEBAAAAAAAAAAAAAAUGBwMECAkKAgsB/8QAhxAAAQIEBAMEBQYHCAUOFggXAQIDAAQFEQYHEiETMUEIIlFhCRQ & so on
Also Huge thanks to Contributors for the Python to WordPress repo
The mammoth cli has a function that extracts images, saves them to a directory and inserts the file names in the img tags in the html code. If you don't want to use mammoth in command line you could use this code:
import os
from mammoth.cli import ImageWriter, _write_output
output_dir = './output'
filename = 'filename.docx'
with open(filename, "rb") as docx_fileobj:
convert_image = mammoth.images.img_element(ImageWriter(output_dir))
output_filename = "{0}.html".format(os.path.basename(filename).rpartition(".")[0])
output_path = os.path.join(output_dir, output_filename)
result = mammoth.convert(
docx_fileobj,
convert_image=convert_image,
output_format='html',
)
_write_output(output_path, result.value)
Note that you would still need to change the img links as you'll be uploading the images to Wordpress, but this solves your mapping issue. You might also want to change the ImageWriter class to save the images to something else than tiff.

Python Base64 - Decoding and Displaying an image

I tried to decode the following base64 image
data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASwSURBVHhe7VoxaxsxFPYvye5sJZC5JRkcSoZCoMSFDIVOGWpSQ6FDqLN5CqGBEnAIoe7sKTSbF2fqmiWbMWTLkl+gPp2k89P56U7Syc7Z1Qdf6+9OTz70qvednlv78efwNWPsuyLo9HPUi9c1/sf57SH8NUXUL6eThPAPISfliNpPpwnhCDWpQtTumntImhCOEJNiRO2mo4cAqqSjh0hURUcPQaiCjh5SMR09BFAlHT1Eoio6eghCFXT0kIrp6CGAl9DDdo3VagThfkkPeWBntRP21okX7Oja9/sE3PSEXe7U2e/19Vl2RsmIsN9n0kPWppKACaPKechwQCx4ARsj9qjiEdz1R3bfnF3oO7HGcP89u6sbEpEhjyn/PGZt3BFZlvWQ4z1iwQvY6j3JaPfvw3rSIRa7vgX7QWHEriwTwjmfpBywXoNYeBMhpoSH4HI1YH/lVf/5BAr16Ra5oAmhBE3Hj8QO2dllz/IKR3p/1NFjm/1knPPzGPU4JxltKGAC57fvpqUMtL+H4HK190XeEfCaD0HT2YUzsd5k93J7WM+P56530t1lHS9BaWOZag9nxp+01sQ9+OztIbhcnUG6XeNtNVmaKJp2ggStJ1Mfqm+mCeWwi59C08P2bCIS4p1BjPf3kEWVK3tjvulPPOZHLwawwy77rvGUNpeq7a4hXiUEPvp5y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Z+doaC0ZtaI6nBI3ofrbh4ikacfexfp7gjVai9VrubqGQLO2nR4hHuBPUQvV/zQqOA/f4lypTUPA75NAXy10WM23oT3kG7rWNsd3ZLzJdq3XBGdXKvvQ3DTw8QTGr2x1Pr9fLMXbRUrD+GLbGPMWqkC7xjI5yqav0h7lyvbPhgmzOvdYDR2eIu4xlQOLTzkiQ0aapGB7YfkqjYeXnP300QIfjJ1NSVM2rrVTjHbi/JMiK/H+L3u6g3HYg/JnClsqHyDnA9hRvd3ydJky5leVN4viybavigAdC3KFb3oNKneVqGHaJ5QxJKe8dxv0otkw4V4RI52KVfGrq/L/8vKaSiGaSAa3qRsGah1zuGjC8tVo8eU1efNZ+EhAsumyaQ5MPTz2OpiD0FYJk0tsgs5Qj4Ph43O9RCqPPlSwechMVZd53oItbC+xHB9yP9Jr6yHLKteWQ/hWEY9p99DpojaTed6iELUi9PRQySqoqOHIFRBRw+pmI4eAqiOZuwfl74j6FwpdBsAAAAASUVORK5CYII=""")
print(base64.b64decode(x))
also with using encode("ascii") for x:
import base64
x = """iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASwSURBVHhe7VoxaxsxFPYvye5sJZC5JRkcSoZCoMSFDIVOGWpSQ6FDqLN5CqGBEnAIoe7sKTSbF2fqmiWbMWTLkl+gPp2k89P56U7Syc7Z1Qdf6+9OTz70qvednlv78efwNWPsuyLo9HPUi9c1/sf57SH8NUXUL6eThPAPISfliNpPpwnhCDWpQtTumntImhCOEJNiRO2mo4cAqqSjh0hURUcPQaiCjh5SMR09BFAlHT1Eoio6eghCFXT0kIrp6CGAl9DDdo3VagThfkkPeWBntRP21okX7Oja9/sE3PSEXe7U2e/19Vl2RsmIsN9n0kPWppKACaPKechwQCx4ARsj9qjiEdz1R3bfnF3oO7HGcP89u6sbEpEhjyn/PGZt3BFZlvWQ4z1iwQvY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umyaQ5MPTz2OpiD0FYJk0tsgs5Qj4Ph43O9RCqPPlSwechMVZd53oItbC+xHB9yP9Jr6yHLKteWQ/hWEY9p99DpojaTed6iELUi9PRQySqoqOHIFRBRw+pmI4eAqiOZuwfl74j6FwpdBsAAAAASUVORK5CYII=""".encode("ascii")
print(base64.b64decode(x))
I saved the result as PNG image and JPG.
the issue here that the files didn't open, and I dont understand how the whole thing works. I watched some tutorials and I failed to display the image on my computer.
try to write to the image file the binary of this string like this
f = open('img.png', 'wb')
f.write(base64.b64decode(x))
f.close()
You could do as follows:
import base64
image_64_decode = base64.decodestring(x)
image_result = open('filename.png', 'wb') # create a writable image and write the decoding result
image_result.write(image_64_decode)

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)

Convert PDF page to image with pyPDF2 and BytesIO

I have a function that gets a page from a PDF file via pyPdf2 and should convert the first page to a png (or jpg) with Pillow (PIL Fork)
from PyPDF2 import PdfFileWriter, PdfFileReader
import os
from PIL import Image
import io
# Open PDF Source #
app_path = os.path.dirname(__file__)
src_pdf= PdfFileReader(open(os.path.join(app_path, "../../../uploads/%s" % filename), "rb"))
# Get the first page of the PDF #
dst_pdf = PdfFileWriter()
dst_pdf.addPage(src_pdf.getPage(0))
# Create BytesIO #
pdf_bytes = io.BytesIO()
dst_pdf.write(pdf_bytes)
pdf_bytes.seek(0)
file_name = "../../../uploads/%s_p%s.png" % (name, pagenum)
img = Image.open(pdf_bytes)
img.save(file_name, 'PNG')
pdf_bytes.flush()
That results in an error:
OSError: cannot identify image file <_io.BytesIO object at 0x0000023440F3A8E0>
I found some threads with a similar issue, (PIL open() method not working with BytesIO) but I cannot see where I am wrong here, as I have pdf_bytes.seek(0) already added.
Any hints appreciated
Per document:
write(stream) Writes the collection of pages added to this object out
as a PDF file.
Parameters: stream – An object to write the file to. The object must
support the write method and the tell method, similar to a file
object.
So the object pdf_bytes contains a PDF file, not an image file.
The reason why there are codes like above work is: sometimes, the pdf file just contains a jpeg file as its content. If your pdf is just a normal pdf file, you can't just read the bytes and parse it as an image.
And refer to as a more robust implementation: https://stackoverflow.com/a/34116472/334999
[![enter image description here][1]][1]
import glob, sys, fitz
# To get better resolution
zoom_x = 2.0 # horizontal zoom
zoom_y = 2.0 # vertical zoom
mat = fitz.Matrix(zoom_x, zoom_y) # zoom factor 2 in each dimension
filename = "/xyz/abcd/1234.pdf" # name of pdf file you want to render
doc = fitz.open(filename)
for page in doc:
pix = page.get_pixmap(matrix=mat) # render page to an image
pix.save("/xyz/abcd/1234.png") # store image as a PNG
Credit
[Convert PDF to Image in Python Using PyMuPDF][2]
https://towardsdatascience.com/convert-pdf-to-image-in-python-using-pymupdf-9cc8f602525b

Python: get zip file from url, extract, and save files / images to server

I am getting a zipfile from a url.
Extracted, it has the following format:
parent_folder
my_file.csv
image_folder
my_image.jpg
I want to save this, extracted, to my server. Before saving, I want to 1) alter the name of parent_folder and 2) insert extra text into the .csv file
I have tried various combinations of code trying to figure out what is going on:
from StringIO import StringIO
from zipfile import ZipFile
from urllib import urlopen
from PIL import Image
url = urlopen("path.zip")
z = ZipFile(StringIO(url.read()))
# z= z.extractall() # not sure I need this?
for line in z.open("my_file.csv").readlines():
print line
# this does print so I could open a new file and write to it. Is that what I do?
img= Image.open(cStringIO.StringIO(z.open("image_folder/my_image.jpg")))
# error: must be convertible to a buffer, not ZipExtFile
# I don't know how to GET the image
Reason: z.open("image_folder/my_image.jpg") gives a ZipExtFile object, while cStringIO.StringIO wants a buffer.
Solution: Try this:
img= Image.open(cStringIO.StringIO(z.open("image_folder/my_image.jpg").read()))

Categories