6. How to remove size cappings on high resolution images in ComfyUI and Krita AI - minsky91/krita-ai-diffusion-hires GitHub Wiki

There are multiple cappings in various components of the Comfy server and the Krita AI Diffusion plugin that limit input image file size. Here are the steps to remove them:

Comfy server:

  1. To remove the general input image or workflow size capping, use the --max-upload-size command line option (default 100 MB). Will raise “Maximum request body size 1048576 exceeded, actual body size” message or “Request entity too large” failure returned by the server when the limit is exceeded.

  2. Line 47 in nodes.py found in the main Comfy folder:

MAX_RESOLUTION=16384

defines a maximal resolution of 16K for the longest dimension for images to be processed by the core nodes such as LoadImage or ImageScale. Changing the constant to a much higher value - like 128*1024 for instance - lifts this limitation enabling further processing with no issues whatsoever (provided a sufficient VRAM amount on the GPU, of course). A validation error is raised by the node being processed when this limit is exceeded.

  1. Line 88 in Image.py from the PIL imaging library used by the server and found in venv (ComfyUI\venv\Lib\site-packages\PIL):

# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)

limits the maximum image size processed by the library. Submitting a larger than the limit image results in an error returned by the server: “Exception Type: PIL.Image.DecompressionBombError- Exception Message: Image size (xxx pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.” Set the value after the colon to None to remove the size limit check.

  1. Line 91 in PngImagePlugin.py module found in the same folder:

MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK

limits the png chunk size and thereby disables processing of very large png images. May raise a failure "Decompressed data too large for PngImagePlugin.MAX_TEXT_CHUNK" or "Too much memory used in text chunks:" when the related limit is exceeded. Set the value on the right side to something like ImageFile.SAFEBLOCK * 8 to lift the capping.

Krita AI plugin:

  1. The Maximum Pixel Count parameter in the plugin’s Configure Image Diffusion / Performance tab sets the capping on the total pixel image size that can be processed in the non-tiled Generate / Refine workspace without enforced scaling down. The maximal value of 99 MP allows processing of images of up to 10x10 K dimension (the tiled Upscale / Refine workspace doesn’t have this limitation). The default value is only a few MPs, so setting it to a much higher value is recommended for GPUs with substantial amounts of VRAM. In Krita AI Hires, activating Tiled Diffusion will automatically remove this capping altogether.

  2. Automatic saving of a generated image within the main Krita document (as an entry in the preview thumbnail sequence) throws an exception in image.py when the resolution of the image exceeds 16K. This is due to the fact that such images are saved by the plugin in the webp format which doesn’t support images larger than that size. In Krita Hires, a workaround for this issue was implemented by saving images this large in the jpeg format instead (at such sizes they are not expected to contain masks which jpeg doesn’t support).

  3. (not a capping per se, just something to adjust) Lines 242-244 in the comfy_cleint.py module:

async for websocket in websockets.connect(
f"{url}/ws?clientId={self._id}", max_size=2**30, ping_timeout=60
)

initiate a websocket connection to exchange data between the Comfy server and the client, Krita AI plugin. When receiving a large png file, the transfer can take longer for large pang files (and much longer for ultra hires ones) than the timeout of 60 seconds specified in the code, disregarding whether it happens over a remote wifi-connected client or on the same machine. This is due to the inherent slowness of the Python websocket-based protocol used. Change the timeout value to 180 to enable receiving of images as large as 100+ MB without causing the plugin to hang indefinitely, until restarting Krita. Note that with Krita Hires this modification is unnecessary, since this version uses a new, optimized download method that uses the http protocol with much faster receive times as the result.