0

hi im doing an automated youtube channel i dont know much about programming can someone help me .. im getting this error if OSError: cannot write mode P as JPEG

here is the code for it i think as i said im dont know much i was copying what the guy was doing

I added the full error at last id appreciate a help

# stuff to save JPEG files

RAWMODE = {
    "1": "L",
    "L": "L",
    "RGB": "RGB",
    "RGBX": "RGB",
    "CMYK": "CMYK;I",  # assume adobe conventions
    "YCbCr": "YCbCr",
}

# fmt: off
zigzag_index = (
    0,  1,  5,  6, 14, 15, 27, 28,
    2,  4,  7, 13, 16, 26, 29, 42,
    3,  8, 12, 17, 25, 30, 41, 43,
    9, 11, 18, 24, 31, 40, 44, 53,
    10, 19, 23, 32, 39, 45, 52, 54,
    20, 22, 33, 38, 46, 51, 55, 60,
    21, 34, 37, 47, 50, 56, 59, 61,
    35, 36, 48, 49, 57, 58, 62, 63,
)

samplings = {
    (1, 1, 1, 1, 1, 1): 0,
    (2, 1, 1, 1, 1, 1): 1,
    (2, 2, 1, 1, 1, 1): 2,
}
# fmt: on


def convert_dict_qtables(qtables):
    warnings.warn(
        "convert_dict_qtables is deprecated and will be removed in Pillow 10"
        "(2023-01-02). Conversion is no longer needed.",
        DeprecationWarning,
    )
    return qtables


def get_sampling(im):
    # There's no subsampling when images have only 1 layer
    # (grayscale images) or when they are CMYK (4 layers),
    # so set subsampling to the default value.
    #
    # NOTE: currently Pillow can't encode JPEG to YCCK format.
    # If YCCK support is added in the future, subsampling code will have
    # to be updated (here and in JpegEncode.c) to deal with 4 layers.
    if not hasattr(im, "layers") or im.layers in (1, 4):
        return -1
    sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]
    return samplings.get(sampling, -1)


def _save(im, fp, filename):

    try:
        rawmode = RAWMODE[im.mode]
    except KeyError as e:
        raise OSError(f"cannot write mode {im.mode} as JPEG") from e

FULL ERROR

Traceback (most recent call last):
  File "C:\Users\USER\Desktop\AutoTube\myvenv\lib\site-packages\PIL\JpegImagePlugin.py", line 630, in _save
    rawmode = RAWMODE[im.mode]
KeyError: 'P'

The above exception was the direct cause of the following exception:  

Traceback (most recent call last):
  File "c:\Users\USER\Desktop\AutoTube\main.py", line 31, in <module> 
    redditbot.save_image(post)
  File "c:\Users\USER\Desktop\AutoTube\utils\RedditBot.py", line 73, in save_image
    scale_gif(image_path, scale)
  File "c:\Users\USER\Desktop\AutoTube\utils\Scalegif.py", line 19, in scale_gif
    gif.save(path)
  File "C:\Users\USER\Desktop\AutoTube\myvenv\lib\site-packages\PIL\Image.py", line 2240, in save
    save_handler(self, fp, filename)
  File "C:\Users\USER\Desktop\AutoTube\myvenv\lib\site-packages\PIL\JpegImagePlugin.py", line 632, in _save
    raise OSError(f"cannot write mode {im.mode} as JPEG") from e      
OSError: cannot write mode P as JPEG
  • Could you share [a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Benjamin Loison Oct 16 '22 at 18:21
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 16 '22 at 19:04
  • always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. – furas Oct 16 '22 at 19:56
  • it seems you have `im.mode = "P"` and you try to get `RAWMODE[im.mode]` which means `RAWMODE["P"]` but you don't have `RAWMODE = {"P": ....}`. Maybe you need `RAWMODE = { "P":"L", ...}` – furas Oct 16 '22 at 19:58
  • @furas i add the line u said and got this error ValueError: No packer found from P to L – Osama Alawneh Oct 16 '22 at 20:14
  • @furas i added the full error please help me fix this – Osama Alawneh Oct 16 '22 at 20:17
  • maybe it needs to convert to different format - ie. `RGB`. – furas Oct 16 '22 at 20:21
  • @furas what should i do then? – Osama Alawneh Oct 16 '22 at 20:23
  • BTW: maybe if you would search `cannot write mode P as JPEG` in Google or in Stackoverflow then maybe you would find [python - Getting "cannot write mode P as JPEG" while operating on JPG image - Stack Overflow](https://stackoverflow.com/questions/21669657/getting-cannot-write-mode-p-as-jpeg-while-operating-on-jpg-image) – furas Oct 16 '22 at 20:23
  • you may convert it to `RGB` on your own `image = image.convert("RGB")` or you can try to add `{"P": "RGB",...}`. But it will remove transparency - so if you want to keep transparency then don't use `JPG` but `PNG`. – furas Oct 16 '22 at 20:26
  • @furas i saw it but i really dont know where to add the code i added it but like it seemes to be missing some variables – Osama Alawneh Oct 16 '22 at 20:27
  • error shows you have `redditbot.save_image(post)` and if `post` means image then you should convert it `redditbot.save_image( post.convert('RGB') )` – furas Oct 16 '22 at 20:30
  • BTW: searching `redditbot save_image` I found question for the same problem [python - OSError: cannot write mode P as JPEG - Stack Overflow](https://stackoverflow.com/questions/72723338/oserror-cannot-write-mode-p-as-jpeg) and in comment you can see someone suggest to send this problem to author of `redditbot` There is even link to `issues` on GitHub – furas Oct 16 '22 at 20:34
  • @furas im getting this now Traceback (most recent call last): File "c:\Users\USER\Desktop\AutoTube\main.py", line 31, in redditbot.save_image( post.convert('RGB') ) File "C:\Users\USER\Desktop\AutoTube\myvenv\lib\site-packages\praw\models\reddit\base.py", line 35, in __getattr__ return getattr(self, attribute) File "C:\Users\USER\Desktop\AutoTube\myvenv\lib\site-packages\praw\models\reddit\base.py", line 36, in __getattr__ raise AttributeError( AttributeError: 'Submission' object has no attribute 'convert – Osama Alawneh Oct 16 '22 at 20:34
  • @furas i sent it to him and messaged the guy on discord but he seems to be offline for more than 3 weeks now – Osama Alawneh Oct 16 '22 at 20:36
  • in GitHub you can see who forked code - [Forks](https://github.com/ClarityCoders/AutoTube/network/members) - and maybe someone already made some changes. See also [Network](https://github.com/ClarityCoders/AutoTube/network) for changes – furas Oct 16 '22 at 20:44

0 Answers0