I've been having hard time importing Image, and CGI together. So, basically I want to upload a gif image, and then display the output which is a thumbnail of the image. I'm getting bunch of errors. I'm unable to use from PIL import Pillow and cgi at the same time. Here is the code below. Your help is highly appreciated. I did hours and hours of research and can't figure that out. I'm getting this error: End of script output before headers: save_file.py
#!C:\Anaconda3\python.exe
from PIL import Image
import cgi, os
import cgitb; cgitb.enable() #cgitb enabled for bug tracking purposes
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
ext = fn[-3:]
condi = 'gif'
if ext == condi:
open('tmpmedia/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
selectfunction = "Please select a function from below"
else:
message = 'You can only upload a gif image. No file was uploaded'
size = (128,128)
saved = "thumb.jpg"
infile = ('C:\\xampp\\htdocs\\aztec\\tmpmedia\\gif' + fn)
try:
im = Image.open(infile)
except:
print("Unable to load image")
im.thumbnail(size)
im.save(saved)
print ("Content-Type: text/html\n")
print ("<html>")
print("<body>")
print(message)
print("</body>")
print("</html>")
I figured it out. There must be a try statement at the end after the print html, and then I would use from PIL import Image
Related
I tried creating a script which would scrape the latest uploaded files on a public picture sharing website and use imageai to recognize what is in the picture,then move it to a folder called the best guess by imageai.
The script works but it's really slow and after like a minute it consumes 5GB of RAM which makes my PC unstable.
I am new to Python and programming this is my second project so far so i only tried a few things written in stackoverflow posts.
import requests
from bs4 import BeautifulSoup
import re
import urllib3
import urllib
from urllib3 import ProxyManager, make_headers
from imageai.Prediction import ImagePrediction
import os
import shutil, os
execution_path = os.getcwd()
from PIL import Image
proxy = urllib3.ProxyManager('proxy')
def make_soup(url):
http = urllib3.PoolManager()
r = http.request("GET", url)
return BeautifulSoup(r.data,'lxml')
ip = 'proxy'
proxies = {
'http': ip,
'https': ip,
}
default="space"
filetype = []
i = 1
soup = make_soup("link")
for img in soup.find_all('img', {'src':re.compile('_tn.jpg')}):
os.chdir(default)
temp = img.get('src').replace("_tn","")
print(temp)
nametemp = img.get('alt').replace("png","jpg")
print(nametemp)
if str(nametemp) == 'None':
filename = str(i)
i = i+1
else:
filename = nametemp
imagefile = open(filename , "wb")
imagefile.write(requests.get(temp).content)
imagefile.close()
print( filename + " mentve")
if ".png" in filename:
try:
im = Image.open(filename +".png")
filetype = "png"
print("this is a png")
im.close()
except IOError:
print("png error")
if ".jpg" in filename:
try:
im=Image.open(filename)
print("this is a jpg")
filetype= "jpg"
im.close()
prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel()
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, filename ), result_count=1 )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
dir = str(predictions[0])
if not os.path.exists(dir):
os.makedirs(dir)
os.chdir(dir)
print(dir)
if not os.path.isfile(filename):
os.chdir(default)
shutil.move(filename , dir)
else:
print("file already exists")
except IOError:
print("jpg error")
if ".jpeg" in filename:
try:
im=Image.open(filename)
print("this is a jpeg")
filetype= "jpeg"
im.close()
except IOError:
print("jpeg error")
print(filetype)
As i am seeking advice on what could be wrong with my code i provided it fully above. Sorry if it breaks any rules i think it's the logical step here.
I would love some advice on reading material or some kind of idea on what should i research to better understand how Python handles RAM usage.
Anything is appreciated, but keep in mind this is my second project so far.
Thank you.
I am trying to convert an Excel spreadsheet to PDF using Python and the comtypes package using this code:
import os
import comtypes.client
FORMAT_PDF = 17
SOURCE_DIR = 'C:/Users/IEUser/Documents/jscript/test/resources/root3'
TARGET_DIR = 'C:/Users/IEUser/Documents/jscript'
app = comtypes.client.CreateObject('Excel.Application')
app.Visible = False
infile = os.path.join(os.path.abspath(SOURCE_DIR), 'spreadsheet1.xlsx')
outfile = os.path.join(os.path.abspath(TARGET_DIR), 'spreadsheet1.pdf')
doc = app.Workbooks.Open(infile)
doc.SaveAs(outfile, FileFormat=FORMAT_PDF)
doc.Close()
app.Quit()
This script above runs fine and the pdf file is created, but when I try to open it I get the error "The file cannot be opened - there is a problem with the file format" (but after closing this error dialog it is actually possible to preview the pdf file). I have tried a similar script to convert Word documents to pdfs and this worked just fine.
Any ideas on how I can resolve this problem with the file format error?
Found a solution - this seems to be working:
import os
import comtypes.client
SOURCE_DIR = 'C:/Users/IEUser/Documents/jscript/test/resources/root3'
TARGET_DIR = 'C:/Users/IEUser/Documents/jscript'
app = comtypes.client.CreateObject('Excel.Application')
app.Visible = False
infile = os.path.join(os.path.abspath(SOURCE_DIR), 'spreadsheet1.xlsx')
outfile = os.path.join(os.path.abspath(TARGET_DIR), 'spreadsheet1.pdf')
doc = app.Workbooks.Open(infile)
doc.ExportAsFixedFormat(0, outfile, 1, 0)
doc.Close()
app.Quit()
This link may also be helpful as an inspiration regarding the arguments to the ExportAsFixedFormatfunction: Document.ExportAsFixedFormat Method (although some of the values of arguments have to be modified a bit).
You need to describe ExportAsFixedFormat(0,outputfile) to save workbook in pdf format. The solution from http://thequickblog.com/convert-an-excel-filexlsx-to-pdf-python/ works for me.
from win32com import client
import win32api
input_file = r'C:\Users\thequickblog\Desktop\Python session 2\tqb_sample.xlsx'
#give your file name with valid path
output_file = r'C:\Users\thequickblog\Desktop\Python session 2\tqb_sample_output.pdf'
#give valid output file name and path
app = client.DispatchEx("Excel.Application")
app.Interactive = False
app.Visible = False
Workbook = app.Workbooks.Open(input_file)
try:
Workbook.ActiveSheet.ExportAsFixedFormat(0, output_file)
except Exception as e:
print("Failed to convert in PDF format.Please confirm environment meets all the requirements and try again")
print(str(e))
finally:
Workbook.Close()
app.Exit()
i'm trying to upload an image to a server (pythonanywhere.com) with a python script using web2py, so i can make some changes to the image and save it...i will use the script in a terminal and upload the image via curl like that:
curl -i -F filedata=#image.jpg http://my_username.pythonanywhere.com/DocScanner/default/upload
That's build in to the web2py SQLFORM. Add an upload field and web2py will stream the file to disk with a safe name and it will return the name to your code. Have a look at the web2py book which documents SQLFORM and upload fields.
import os
def decode_image3(src):
import base64
import re
import uuid
result = re.search("data:image/(?P<ext>.*?);base64,(?P<data>.*)", src, re.DOTALL)
if result:
ext = result.groupdict().get("ext")
data = result.groupdict().get("data")
else:
raise Exception("Do not parse!")
# 2, base64 decoding
img = base64.urlsafe_b64decode(data)
# 3, the binary file is saved
filename = "{}.{}".format(uuid.uuid4(), ext)
completeName = os.path.join(request.folder,'uploads', filename)
with open(completeName, "wb") as f:
f.write(img)
return filename
#request.restful()
def Image2():
response.view = 'generic.json'
def POST(**vars):
image = decode_image3(request.vars.image)
completeName = os.path.join(request.folder,'uploads', image)
stream = open(completeName, 'rb')
if os.path.exists(completeName):
rv = os.remove(completeName)
save = db.test.insert(image=stream, age=34)
return dict(msg=save)
return locals()
I am trying to save jpegs to a file from a list of urls. This code times out frequently and randomly. It has saved up to 113 jpegs, there are many more than that, and sometimes only saves 10 before timing out. Is there a way to put a wait in so the timeout doesn't occur? I have tried sleep in the commented section with no luck. Thanks for the feedback!
Heres the timeout error message:
import urllib.request
import urllib
import codecs
from urllib import request
import time
import csv
class File:
def __init__(self, data):
self.data = data
file = File("1")
with open("file.csv", encoding = "utf8") as f1:
file.data = list(csv.reader(f1, skipinitialspace = True))
for i in file.data[1:]:
if len(i[27]) != 0:
#i[14] creates a unique jpeg file name in the dir
image = open('C:\\aPath'+i[14]+'.JPG', 'wb')
path = 'aPath' + i[14] + '.JPG'
#time.sleep(2) Tried sleep here, didn't work
#i[27] is a working jpeg url
urllib.request.urlretrieve(i[27], path)
image.close()
print('done!')
There's no way to prevent the exception. You need to catch the exception and retry.
...
for i in file.data[1:]:
if not i[27]:
continue
path = 'aPath' + i[14] + '.JPG'
while True: # retry loop
try:
urllib.request.urlretrieve(i[27], path)
break # On success, stop retry.
except TimeoutError:
print('timeout, retry in 1 second.')
time.sleep(1)
BTW, you don't need to open file if you use urllib.request.urlretrieve.
So I am pulling jpg's from a url. I am able to save the image files as long as they are being saved to the same folder the python file is in. As soon as I attempt to change the folder(seen here as the outpath) the image files do not get created. I imagine it has something to do with my outpath, but it seems to be fine when I am printing and watching it in the console.
Ubuntu 11.10 OS by the way. I'm a newbie with both linux and python, so it could easily be either. :)
If I were to print the sequence taken from the CSV file it would look like: [['Champ1', 'Subname1', 'imgurl1'],['Champ2', 'subname2', 'imgurl2'],['Champ3','subname3','imgurl3']...]
(It was scraped from a website)
import csv
from urlparse import urlsplit
from urllib2 import urlopen, build_opener
from urllib import urlretrieve
import webbrowser
import os
import sys
reader = csv.reader(open('champdata.csv', "rb"), delimiter = ",", skipinitialspace=True)
champInfo = []
for champs in reader:
champInfo.append(champs)
size = len(champInfo)
def GetImages(x, out_folder="/home/sean/Home/workspace/CP/images"):
b=1
size = len(champInfo)
print size
while b < size:
temp_imgurls = x.pop(b)
filename = os.path.basename(temp_imgurls[2])
print filename
outpath = os.path.join(out_folder, filename)
print outpath
u = urlopen(temp_imgurls[2])
localFile = open(outpath, 'wb')
localFile.write(u.read())
localFile.close()
b+=1
GetImages(champInfo)
I understand it's quite crude, but it does work, only if I'm not attempting to change the save path.
Try providing the complete image path everywhere
E:/../home/sean/Home/workspace/CD/images
def GetImages(x):
b=1
size = len(champInfo)
print size
while b < size:
temp_imgurls = x.pop(b)
filename = temp_imgurls[2]
u = urlopen(temp_imgurls[2])
localFile = open(filename, 'wb')
localFile.write(u.read())
localFile.close()
And this code will be save files in the same directory where script is.
Updated Answer:
I think the answer to your problem is just to add a check for the output directory existence, and create it if needed. ie, add:
if not os.path.exists(out_folder):
os.makedirs(out_folder)
to your existing code.
More generally , you could try something more like this:
import csv
from urllib2 import urlopen
import os
import sys
default_outfolder = "/home/sean/Home/workspace/CD/images"
# simple arg passing wihtout error checking
out_folder = sys.argv[1] if len(sys.argv) == 2 else default_outfolder
if not os.path.exists(out_folder):
os.makedirs(out_folder) # creates out_folder, including any required parent ones
else:
if not os.path.isdir(out_folder):
raise RuntimeError('output path must be a directory')
reader = csv.reader(open('champdata.csv', "rb"), delimiter = ",", skipinitialspace=True)
for champs in reader:
img_url = champs[2]
filename = os.path.basename(img_url)
outpath = os.path.join(out_folder, filename)
print 'Downloading %s to %s' % (img_url, outpath)
with open(outpath, 'wb') as f:
u = urlopen(img_url)
f.write(u.read())
The above code works for champdata.csv of the form stuff,more_stuff,http://www.somesite.com.au/path/to/image.png
but will need to be adapted if I have not understood the actual format of your incoming data.