So I have a picture converter that converts online pictures to hex.
whenever I attempt to input a picture it gives me an error that says ValueError: too many values to unpack (expected 3) I have tried other solutions on the internet but they haven't worked.
Python version is 3.9.1
Full output from server (web server that is using the image script as a module):
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 54645)
Traceback (most recent call last):
File "C:\Users\Lavacat\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 316, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Users\Lavacat\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 347, in process_request
self.finish_request(request, client_address)
File "C:\Users\Lavacat\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\Lavacat\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Users\Lavacat\AppData\Local\Programs\Python\Python39\lib\http\server.py", line 427, in handle
self.handle_one_request()
File "C:\Users\Lavacat\AppData\Local\Programs\Python\Python39\lib\http\server.py", line 415, in handle_one_request
method()
File "D:\Desktop 3\Desktop 2\Desktap\HTTP Datastore\server.py", line 26, in do_POST
imagehandle.photoconvert(str(body.decode("utf-8"))[5:], 2)
File "D:\Desktop 3\Desktop 2\Desktap\HTTP Datastore\imagehandle.py", line 18, in photoconvert
r,g,b = pixel
ValueError: too many values to unpack (expected 3)
----------------------------------------
Server code:
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from io import BytesIO
import os
import imagehandle
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
with open(str(self.path)[1:], 'rb') as file:
self.send_response(200)
self.end_headers()
if self.path == "/server.py":
self.wfile.write(b"Access denied. No touchy touchy!")
else:
self.wfile.write(file.read())
print(self.path)
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b"")
print(body.decode("utf-8"))
imagehandle.photoconvert(str(body.decode("utf-8"))[5:], 2)
response.write(body)
self.wfile.write(response.getvalue())
httpd = HTTPServer(('127.0.0.1', 8079), SimpleHTTPRequestHandler)
httpd.serve_forever()
ImageHandle code:
from colormap import rgb2hex
from PIL import Image
from shutil import move
import re, time, os
import PIL
from urllib.request import urlopen
def tobyte(r,g,b):
a = chr(r)+chr(g)+chr(b)
return a
def photoconvert(url, div):
im = PIL.Image.open(urlopen(url))
WIDTH1, HEIGHT1 = im.size
npd = ''
for pixel in im.getdata():
print(pixel)
r,g,b = pixel
if mode == 2: #hex compression
npd = npd + rgb2hex(r,g,b)
elif mode == 3: #byte compression
npd = npd + tobyte(r,g,b)
else: #grayscale compression
npd = npd + str("{:03d}".format(r+g+b))
if mode == 3:
npd = npd.encode('utf8')
pixf = open("data_r.json", "wb+")
elif mode == 2:
npd = npd.replace('#', '', len(npd))
pixf = open("data_r.json", "w+")
else:
pixf = open("data_r.json", "w+")
sizef = open("size_r.json", "w+")
st = [WIDTH1, HEIGHT1]
fst = re.sub('[ ]', '', str(st))
pixf.write(npd)
sizef.write(fst)
pixf.close()
sizef.close()
f = 'data_r.json'
move(f,f.replace('_r.json','.json'))
f2 = 'size_r.json'
move(f2,f2.replace('_r.json','.json'))
print("success")
HTML code
<html>
<body>
<form method="post" enctype="text/plain">
<input type="text" id="textinput" name="text">
<button>Submit</button>
</form>
</body>
</html>