Download images in python - python

i have a app which convert the image to pencil sketch here i am trying to download the output image in browser previously i have done but its downloading inside the browser
Is there any other way to download the image in browser
import streamlit as st
import numpy as np
from PIL import Image,ImageDraw
import cv2
if st.button("Download Sketch Images"):
im_pil = Image.fromarray(final_sketch)
im_pil.save('Pencil Sketch.jpeg')
st.write('Download completed')

There seems to be a widget included in streamlit to do what you ask for.
st.download_button(label="Download Sketch Images", data=im_pil.tobytes(), file_name='Pencil Sketch.jpeg')
https://docs.streamlit.io/library/api-reference/widgets/st.download_button

Related

How can I extract text from an image in a pdf using the python port of Apache/Tika 2.6.0?

import tika
from tika import parser
import pytesseract
from PIL import Image
import numpy
import scipy
from tika import config
tika.initVM()
headers={'X-Tika-OCRLanguage': 'eng','X-Tika-PDFextractInlineImages': 'true','X-Tika-PDFOcrStrategy': 'ocr_and_text_extraction'}
parsed_pdf = parser.from_file("Tespdf.pdf",headers=headers)
data = parsed_pdf['content']
# Printing of content
print(data)
I added pytesseract,numpy and scikit-image to preprocess the images. I have successfully tested image files using pytesseract however if I install them in a pdf and use tika I am not getting the text...

Extracting data from table and saving it. Layout parser package

I am new in working with python and I am using Melissa Dell's package to extract data from a table image. My image looks like this:
enter image description here
And my code, for now, is the following one:
pip install layoutparser[ocr]
import layoutparser as lp
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
import cv2
from google.cloud.vision_v1 import types
import json
import re
from google.cloud import vision
pip show google-cloud-vision
ocr_agent = lp.GCVAgent.with_credential('mycredebtials.json',
languages = ['es'])
img = plt.imread(r'D:\pdfDispacher.do_Página_2.jpg', cv2.IMREAD_COLOR)
print(img)
plt.imshow(img)
res = ocr_agent.detect(img, return_response=True)
texts = ocr_agent.gather_text_annotations(res)
layout = ocr_agent.gather_full_text_annotation(res, agg_level=lp.GCVFeatureType.WORD)
lp.draw_box(img, layout)
lp.draw_text(img, layout, font_size=12, with_box_on_text=True,
text_box_width=1)
What I need is to tell python to get all the columns and rows and save them in CSV format. But I am not able to get this done.
I really appreciate it if anyone can help me with the next lines.

Retrieve image from url using dlib

I want to retrieve an image from the url using the dlib library.
I have tried the following code:
win = dlib.image_window()
img = dlib.load_rgb_image(urllib.request.urlopen(url).read())
win.set_image(img)
But the window opens empty, when I expected the image from the url to open.
After a lot of trial and error, this seems to display the image for me:
import urllib.request
import dlib
import numpy
import io
from PIL import Image
win = dlib.image_window()
img = numpy.array(Image.open(io.BytesIO(urllib.request.urlopen(url).read())))
win.set_image(img)

How to import png images Python

I've tried to import a png file in Python 3.6 with Jupyter Notebook with no success.
I've seen some examples that don't work, at least not anymore, i.e.
import os,sys
import Image
jpgfile = Image.open("picture.jpg")
There is no module called Image that I can install with either:
conda install Image
or
pip install Image
Any simple solution would be greatly appreciated!
You can display an image from file in a Jupyter Notebook as follows:
from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(url=img)
where img = 'fig31_Drosophila.jpg' is the path and filename of the image you want. (here, the image is in the same folder as the main script)
alternatively:
from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(filename=img)
You can specify optional args (for width and height for instance:
from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(url=img, width=100, height=100)

How do I read image data from a URL?

What I'm trying to do is fairly simple when we're dealing with a local file, but the problem comes when I try to do this with a remote URL.
Basically, I'm trying to create a PIL image object from a file pulled from a URL. Sure, I could always just fetch the URL and store it in a temp file, then open it into an image object, but that feels very inefficient.
Here's what I have:
Image.open(urlopen(url))
It flakes out complaining that seek() isn't available, so then I tried this:
Image.open(urlopen(url).read())
But that didn't work either. Is there a Better Way to do this, or is writing to a temporary file the accepted way of doing this sort of thing?
In Python3 the StringIO and cStringIO modules are gone.
In Python3 you should use:
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
Using a StringIO
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
The following works for Python 3:
from PIL import Image
import requests
im = Image.open(requests.get(url, stream=True).raw)
References:
https://github.com/python-pillow/Pillow/pull/1151
https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#280-2015-04-01
Using requests:
from PIL import Image
import requests
from StringIO import StringIO
response = requests.get(url)
img = Image.open(StringIO(response.content))
Python 3
from urllib.request import urlopen
from PIL import Image
img = Image.open(urlopen(url))
img
Jupyter Notebook and IPython
import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)
Unlike other methods, this method also works in a for loop!
Use StringIO to turn the read string into a file-like object:
from StringIO import StringIO
from PIL import Image
import urllib
Image.open(StringIO(urllib.request.urlopen(url).read()))
For those doing some sklearn/numpy post processing (i.e. Deep learning) you can wrap the PIL object with np.array(). This might save you from having to Google it like I did:
from PIL import Image
import requests
import numpy as np
from StringIO import StringIO
response = requests.get(url)
img = np.array(Image.open(StringIO(response.content)))
The arguably recommended way to do image input/output these days is to use the dedicated package ImageIO. Image data can be read directly from a URL with one simple line of code:
from imageio import imread
image = imread('https://cdn.sstatic.net/Sites/stackoverflow/img/logo.png')
Many answers on this page predate the release of that package and therefore do not mention it. ImageIO started out as component of the Scikit-Image toolkit. It supports a number of scientific formats on top of the ones provided by the popular image-processing library PILlow. It wraps it all in a clean API solely focused on image input/output. In fact, SciPy removed its own image reader/writer in favor of ImageIO.
select the image in chrome, right click on it, click on Copy image address, paste it into a str variable (my_url) to read the image:
import shutil
import requests
my_url = 'https://www.washingtonian.com/wp-content/uploads/2017/06/6-30-17-goat-yoga-congressional-cemetery-1-994x559.jpg'
response = requests.get(my_url, stream=True)
with open('my_image.png', 'wb') as file:
shutil.copyfileobj(response.raw, file)
del response
open it;
from PIL import Image
img = Image.open('my_image.png')
img.show()
Manually wrapping in BytesIO is no longer needed since PIL >= 2.8.0. Just use Image.open(response.raw)
Adding on top of Vinícius's comment:
You should pass stream=True as noted https://requests.readthedocs.io/en/master/user/quickstart/#raw-response-content
So
img = Image.open(requests.get(url, stream=True).raw)
USE urllib.request.urlretrieve() AND PIL.Image.open() TO DOWNLOAD AND READ IMAGE DATA :
import requests
import urllib.request
import PIL
urllib.request.urlretrieve("https://i.imgur.com/ExdKOOz.png", "sample.png")
img = PIL.Image.open("sample.png")
img.show()
or Call requests.get(url) with url as the address of the object file to download via a GET request. Call io.BytesIO(obj) with obj as the content of the response to load the raw data as a bytes object. To load the image data, call PIL.Image.open(bytes_obj) with bytes_obj as the bytes object:
import io
response = requests.get("https://i.imgur.com/ExdKOOz.png")
image_bytes = io.BytesIO(response.content)
img = PIL.Image.open(image_bytes)
img.show()
from PIL import Image
import cv2
import numpy as np
import requests
image=Image.open(requests.get("https://previews.123rf.com/images/darrenwhi/darrenwhi1310/darrenwhi131000024/24022179-photo-of-many-cars-with-one-a-different-color.jpg", stream=True).raw)
#image =resize((420,250))
image_array=np.array(image)
image
To directly get image as numpy array without using PIL
import requests, io
import matplotlib.pyplot as plt
response = requests.get(url).content
img = plt.imread(io.BytesIO(response), format='JPG')
plt.imshow(img)

Categories

Resources