{"id":444,"date":"2026-02-21T08:55:24","date_gmt":"2026-02-21T00:55:24","guid":{"rendered":"https:\/\/connectword.dpdns.org\/?p=444"},"modified":"2026-02-21T08:55:24","modified_gmt":"2026-02-21T00:55:24","slug":"a-coding-guide-to-high-quality-image-generation-control-and-editing-using-huggingface-diffusers","status":"publish","type":"post","link":"https:\/\/connectword.dpdns.org\/?p=444","title":{"rendered":"A Coding Guide to High-Quality Image Generation, Control, and Editing Using HuggingFace Diffusers"},"content":{"rendered":"<p>In this tutorial, we design a practical image-generation workflow using the <a href=\"https:\/\/github.com\/huggingface\/diffusers\"><strong>Diffusers<\/strong><\/a> library. We start by stabilizing the environment, then generate high-quality images from text prompts using Stable Diffusion with an optimized scheduler. We accelerate inference with a LoRA-based latent consistency approach, guide composition with ControlNet under edge conditioning, and finally perform localized edits via inpainting. Also, we focus on real-world techniques that balance image quality, speed, and controllability.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">!pip -q uninstall -y pillow Pillow || true\n!pip -q install --upgrade --force-reinstall \"pillow&lt;12.0\"\n!pip -q install --upgrade diffusers transformers accelerate safetensors huggingface_hub opencv-python\n\n\nimport os, math, random\nimport torch\nimport numpy as np\nimport cv2\nfrom PIL import Image, ImageDraw, ImageFilter\nfrom diffusers import (\n   StableDiffusionPipeline,\n   StableDiffusionInpaintPipeline,\n   ControlNetModel,\n   StableDiffusionControlNetPipeline,\n   UniPCMultistepScheduler,\n)\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We prepare a clean and compatible runtime by resolving dependency conflicts and installing all required libraries. We ensure image processing works reliably by pinning the correct Pillow version and loading the Diffusers ecosystem. We also import all core modules needed for generation, control, and inpainting workflows.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">def seed_everything(seed=42):\n   random.seed(seed)\n   np.random.seed(seed)\n   torch.manual_seed(seed)\n   torch.cuda.manual_seed_all(seed)\n\n\ndef to_grid(images, cols=2, bg=255):\n   if isinstance(images, Image.Image):\n       images = [images]\n   w, h = images[0].size\n   rows = math.ceil(len(images) \/ cols)\n   grid = Image.new(\"RGB\", (cols*w, rows*h), (bg, bg, bg))\n   for i, im in enumerate(images):\n       grid.paste(im, ((i % cols)*w, (i \/\/ cols)*h))\n   return grid\n\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\ndtype = torch.float16 if device == \"cuda\" else torch.float32\nprint(\"device:\", device, \"| dtype:\", dtype)<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We define utility functions to ensure reproducibility and to organize visual outputs efficiently. We set global random seeds so our generations remain consistent across runs. We also detect the available hardware and configure precision to optimize performance on the GPU or CPU.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">seed_everything(7)\nBASE_MODEL = \"runwayml\/stable-diffusion-v1-5\"\n\n\npipe = StableDiffusionPipeline.from_pretrained(\n   BASE_MODEL,\n   torch_dtype=dtype,\n   safety_checker=None,\n).to(device)\n\n\npipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)\n\n\nif device == \"cuda\":\n   pipe.enable_attention_slicing()\n   pipe.enable_vae_slicing()\n\n\nprompt = \"a cinematic photo of a futuristic street market at dusk, ultra-detailed, 35mm, volumetric lighting\"\nnegative_prompt = \"blurry, low quality, deformed, watermark, text\"\n\n\nimg_text = pipe(\n   prompt=prompt,\n   negative_prompt=negative_prompt,\n   num_inference_steps=25,\n   guidance_scale=6.5,\n   width=768,\n   height=512,\n).images[0]<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We initialize the base Stable Diffusion pipeline and switch to a more efficient UniPC scheduler. We generate a high-quality image directly from a text prompt using carefully chosen guidance and resolution settings. This establishes a strong baseline for subsequent improvements in speed and control.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">LCM_LORA = \"latent-consistency\/lcm-lora-sdv1-5\"\npipe.load_lora_weights(LCM_LORA)\n\n\ntry:\n   pipe.fuse_lora()\n   lora_fused = True\nexcept Exception as e:\n   lora_fused = False\n   print(\"LoRA fuse skipped:\", e)\n\n\nfast_prompt = \"a clean product photo of a minimal smartwatch on a reflective surface, studio lighting\"\nfast_images = []\nfor steps in [4, 6, 8]:\n   fast_images.append(\n       pipe(\n           prompt=fast_prompt,\n           negative_prompt=negative_prompt,\n           num_inference_steps=steps,\n           guidance_scale=1.5,\n           width=768,\n           height=512,\n       ).images[0]\n   )\n\n\ngrid_fast = to_grid(fast_images, cols=3)\nprint(\"LoRA fused:\", lora_fused)\n\n\nW, H = 768, 512\nlayout = Image.new(\"RGB\", (W, H), \"white\")\ndraw = ImageDraw.Draw(layout)\ndraw.rectangle([40, 80, 340, 460], outline=\"black\", width=6)\ndraw.ellipse([430, 110, 720, 400], outline=\"black\", width=6)\ndraw.line([0, 420, W, 420], fill=\"black\", width=5)\n\n\nedges = cv2.Canny(np.array(layout), 80, 160)\nedges = np.stack([edges]*3, axis=-1)\ncanny_image = Image.fromarray(edges)\n\n\nCONTROLNET = \"lllyasviel\/sd-controlnet-canny\"\ncontrolnet = ControlNetModel.from_pretrained(\n   CONTROLNET,\n   torch_dtype=dtype,\n).to(device)\n\n\ncn_pipe = StableDiffusionControlNetPipeline.from_pretrained(\n   BASE_MODEL,\n   controlnet=controlnet,\n   torch_dtype=dtype,\n   safety_checker=None,\n).to(device)\n\n\ncn_pipe.scheduler = UniPCMultistepScheduler.from_config(cn_pipe.scheduler.config)\n\n\nif device == \"cuda\":\n   cn_pipe.enable_attention_slicing()\n   cn_pipe.enable_vae_slicing()\n\n\ncn_prompt = \"a modern cafe interior, architectural render, soft daylight, high detail\"\nimg_controlnet = cn_pipe(\n   prompt=cn_prompt,\n   negative_prompt=negative_prompt,\n   image=canny_image,\n   num_inference_steps=25,\n   guidance_scale=6.5,\n   controlnet_conditioning_scale=1.0,\n).images[0]<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We accelerate inference by loading and fusing a LoRA adapter and demonstrate fast sampling with very few diffusion steps. We then construct a structural conditioning image and apply ControlNet to guide the layout of the generated scene. This allows us to preserve composition while still benefiting from creative text guidance.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">mask = Image.new(\"L\", img_controlnet.size, 0)\nmask_draw = ImageDraw.Draw(mask)\nmask_draw.rectangle([60, 90, 320, 170], fill=255)\nmask = mask.filter(ImageFilter.GaussianBlur(2))\n\n\ninpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(\n   BASE_MODEL,\n   torch_dtype=dtype,\n   safety_checker=None,\n).to(device)\n\n\ninpaint_pipe.scheduler = UniPCMultistepScheduler.from_config(inpaint_pipe.scheduler.config)\n\n\nif device == \"cuda\":\n   inpaint_pipe.enable_attention_slicing()\n   inpaint_pipe.enable_vae_slicing()\n\n\ninpaint_prompt = \"a glowing neon sign that says 'CAF\u00c9', cyberpunk style, realistic lighting\"\n\n\nimg_inpaint = inpaint_pipe(\n   prompt=inpaint_prompt,\n   negative_prompt=negative_prompt,\n   image=img_controlnet,\n   mask_image=mask,\n   num_inference_steps=30,\n   guidance_scale=7.0,\n).images[0]\n\n\nos.makedirs(\"outputs\", exist_ok=True)\nimg_text.save(\"outputs\/text2img.png\")\ngrid_fast.save(\"outputs\/lora_fast_grid.png\")\nlayout.save(\"outputs\/layout.png\")\ncanny_image.save(\"outputs\/canny.png\")\nimg_controlnet.save(\"outputs\/controlnet.png\")\nmask.save(\"outputs\/mask.png\")\nimg_inpaint.save(\"outputs\/inpaint.png\")\n\n\nprint(\"Saved outputs:\", sorted(os.listdir(\"outputs\")))\nprint(\"Done.\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We create a mask to isolate a specific region and apply inpainting to modify only that part of the image. We refine the selected area using a targeted prompt while keeping the rest intact. Finally, we save all intermediate and final outputs to disk for inspection and reuse.<\/p>\n<p>In conclusion, we demonstrated how a single Diffusers pipeline can evolve into a flexible, production-ready image generation system. We explained how to move from pure text-to-image generation to fast sampling, structural control, and targeted image editing without changing frameworks or tooling. This tutorial highlights how we can combine schedulers, LoRA adapters, ControlNet, and inpainting to create controllable and efficient generative pipelines that are easy to extend for more advanced creative or applied use cases.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Deep%20Learning\/diffusers_image_generation_control_editing_marktechpost.py\" target=\"_blank\" rel=\"noreferrer noopener\">Full Codes here<\/a>.\u00a0<\/strong>Also,\u00a0feel free to follow us on\u00a0<strong><a href=\"https:\/\/x.com\/intent\/follow?screen_name=marktechpost\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Twitter<\/mark><\/a><\/strong>\u00a0and don\u2019t forget to join our\u00a0<strong><a href=\"https:\/\/www.reddit.com\/r\/machinelearningnews\/\" target=\"_blank\" rel=\"noreferrer noopener\">100k+ ML SubReddit<\/a><\/strong>\u00a0and Subscribe to\u00a0<strong><a href=\"https:\/\/www.aidevsignals.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Newsletter<\/a><\/strong>. Wait! are you on telegram?\u00a0<strong><a href=\"https:\/\/t.me\/machinelearningresearchnews\" target=\"_blank\" rel=\"noreferrer noopener\">now you can join us on telegram as well.<\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/www.marktechpost.com\/2026\/02\/20\/a-coding-guide-to-high-quality-image-generation-control-and-editing-using-huggingface-diffusers\/\">A Coding Guide to High-Quality Image Generation, Control, and Editing Using HuggingFace Diffusers<\/a> appeared first on <a href=\"https:\/\/www.marktechpost.com\/\">MarkTechPost<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we design a &hellip;<\/p>\n","protected":false},"author":1,"featured_media":29,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=444"}],"version-history":[{"count":0,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/media\/29"}],"wp:attachment":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}