0

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>
Lavacat
  • 11
  • 4
  • what is the output for `print(pixel)`? It does not seem to contain three values, as expected by the line `r,g,b = pixel` – anurag Jan 20 '21 at 05:47
  • Well, what does that `print(pixel)` give you right before the error? – deceze Jan 20 '21 at 05:47
  • It will be helpful if you show the output for `print(type(pixel))` as well! – anurag Jan 20 '21 at 05:56
  • Also in the server.py code, there is a reason why I removed the first 5 characters from the URL because my HTML code sends text=http://url.url/png.png – Lavacat Jan 20 '21 at 12:21
  • The output for pixel is (0, 0, 0, 0). probably due to some image downloading error – Lavacat Jan 20 '21 at 12:27
  • Well, `(0, 0, 0, 0)` are *four* values, not three as you expect. That's the issue… – deceze Jan 20 '21 at 12:27
  • why is the error expecting 3 variables instead? – Lavacat Jan 20 '21 at 12:52
  • `r, g, b` are three variables. `(0, 0, 0, 0)` are four values. That doesn't match. If you *always* expect four values, then add a forth variable on the left side; if you don't need a forth variable just name it `_`: `r, g, b, _ = pixel`. If you sometimes get four items in `pixel` and sometimes only three, use rest-unpacking: `r, g, b, *_ = pixel`. Or shorten your tuple: `r, g, b = pixel[:3]`. – deceze Jan 20 '21 at 12:55
  • I know the problem, the image downloaded is invalid, when I changed the im variable to use a pyautogui screenshot it returned with a output success and wrote the data.json and size.json successfully. – Lavacat Jan 20 '21 at 13:01

0 Answers0