تحسين مطالبات الصور تكراريًا
تحليل الصور المُنشأة وتعديل المطالبات بطريقة منهجية
تحسين مطالبات الصور تكراريًا درس مجاني في AI Prompt Engineering على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Prompt Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.
لماذا التنقيح التكراري؟
نادراً ما تنتج مطالبات الصور الأولية النتيجة التي تريدها تماماً. والتنقيح التكراري سير عمل منهجي: ولّد الصورة، وحللها، وحدد الفجوات، وعدّل المطالبة، ثم أعد التوليد. وتقلل كل دورة الفجوة بين القصد والمخرج.
حلقة التنقيح
تتكون حلقة التنقيح التكراري من أربع خطوات: التوليد (إنشاء صورة من المطالبة الحالية)، والتحليل (تحديد ما هو خاطئ أو مفقود)، والتعديل (تعديل المطالبة لمعالجة المشكلات)، ثم إعادة التوليد. كرر ذلك حتى تشعر بالرضا أو تتوقف بسبب قيود الميزانية أو الوقت.
class PromptRefinementSession:
def __init__(self, initial_prompt, negative_prompt=''):
self.history = []
self.current_prompt = initial_prompt
self.current_negative = negative_prompt
self.iteration = 0
def record_iteration(self, issues_found, adjustments_made):
self.history.append({
'iteration': self.iteration,
'prompt': self.current_prompt,
'negative': self.current_negative,
'issues': issues_found,
'adjustments': adjustments_made
})
self.iteration += 1
def update_prompt(self, new_prompt, new_negative=None):
self.current_prompt = new_prompt
if new_negative is not None:
self.current_negative = new_negative
def get_history(self):
return self.history
# Usage
session = PromptRefinementSession(
initial_prompt='a woman walking in a rainy city at night',
negative_prompt='blurry, low quality'
)
print('Refinement session started. Iteration 0.')تصنيف مشكلات تحليل صورة مولّدة
عند تحليل ما حدث من أخطاء، صنّف المشكلات حسب نوعها. فهذا يحدد الجزء من المطالبة الذي يحتاج إلى تعديل — إضافة تفاصيل أو إزالة تعارض أو ضبط الأوزان.
ISSUE_TAXONOMY = {
'Subject issues': [
'Subject missing or wrong species/gender/age',
'Key detail absent (clothing, expression, props)',
'Pose or action incorrect',
'Background wrong or distracting'
],
'Style issues': [
'Wrong art style (photo when painting expected)',
'Too stylized/not stylized enough',
'Style inconsistency (mixing styles incoherently)'
],
'Lighting issues': [
'Wrong time of day',
'Too dark or too bright',
'Shadows wrong direction',
'Missing dramatic effect'
],
'Composition issues': [
'Wrong framing (too close/far)',
'Subject cropped awkwardly',
'Rule of thirds not applied',
'Cluttered vs. desired clean composition'
],
'Quality issues': [
'Blurry or low detail',
'Anatomical distortion (extra fingers)',
'Watermark or text artifact',
'Overexposed/underexposed areas'
]
}
for category, issues in ISSUE_TAXONOMY.items():
print(f'{category}: {issues[0]}')التكرار 1: قبل وبعد
إليك مثالاً عملياً يوضح، من خلال المقارنة بين الحالة قبل التعديل وبعده، كيف يؤدي تحديد المشكلات وتعديل المطالبة إلى تحسين النتائج.
# ITERATION 0: Initial prompt (too vague)
prompt_v0 = 'a woman walking in a rainy city at night'
# Issues found:
# - Style unspecified -> model defaulted to generic illustration
# - Lighting unspecified -> flat even light, no mood
# - No detail on clothing or setting
# - No composition direction
# ITERATION 1: Add style, lighting, detail
prompt_v1 = (
'a young woman in a yellow raincoat walking down '
'a rain-soaked Tokyo street at night, '
'neon signs reflected in puddles, steam rising from grates, '
'cinematic photography style, street photography, '
'warm neon glow, wet pavement reflections, '
'medium shot, slightly low angle, bokeh background'
)
negative_v1 = 'blurry, low quality, watermark, extra fingers, cartoon'
# Remaining issues after v1:
# - Raincoat not yellow (model defaulted to dark colors)
# - Woman facing wrong way
print('V0 length:', len(prompt_v0.split()))
print('V1 length:', len(prompt_v1.split()))
print('Iteration adds: style, lighting, composition, specific details')التكرار 2: إصلاح عناصر محددة
يستهدف التكرار الثاني المشكلات المتبقية بدقة — لا تعِد كتابة المطالبة بأكملها، بل عالج المشكلات المحددة التي عثرت عليها في التكرار الأول.
# ITERATION 1 issues:
# - Raincoat not yellow (model defaulted to dark)
# - Woman facing wrong way (walking away from camera)
# ITERATION 2: targeted fixes
prompt_v2 = (
'a young woman in a BRIGHT YELLOW raincoat '
'walking TOWARD the camera '
'down a rain-soaked Tokyo street at night, '
'neon signs reflected in puddles, steam rising from grates, '
'face visible, slight smile, carrying groceries, '
'cinematic photography style, street photography, '
'warm neon glow, wet pavement reflections, '
'medium shot, slightly low angle, bokeh background'
)
# Changes made:
# 1. "BRIGHT YELLOW" capitalization + adjective for emphasis
# 2. Added "walking TOWARD the camera" to fix direction
# 3. Added "face visible" to prevent back-to-camera result
# 4. Added specific detail: "carrying groceries, slight smile"
# Best practice: track what you changed and why
changelog = {
'v0_to_v1': 'Added style, lighting, composition, city details',
'v1_to_v2': 'Fixed raincoat color, fixed walking direction, added face constraint'
}
print('Changelog:', changelog)استخدام بذور ثابتة للمقارنة
عند مقارنة تنويعات المطالبة، استخدم بذرة عشوائية ثابتة حتى يكون المتغير الوحيد هو التغيير في المطالبة. ومن دون بذرة ثابتة، لن تتمكن من معرفة ما إذا كانت التغييرات في المخرج ناتجة عن تعديلك للمطالبة أم عن العشوائية.
import requests
SD_API_URL = 'http://localhost:7860/sdapi/v1/txt2img'
def compare_prompt_versions(prompts_dict, negative='blurry, low quality',
seed=12345, steps=30):
results = {}
for version, prompt in prompts_dict.items():
payload = {
'prompt': prompt,
'negative_prompt': negative,
'seed': seed, # FIXED SEED for fair comparison
'steps': steps,
'cfg_scale': 7,
'width': 512,
'height': 512
}
response = requests.post(SD_API_URL, json=payload)
results[version] = response.json().get('images', [None])[0]
print(f'{version}: generated with seed {seed}')
return results
promptvariants = {
'v0': 'a woman walking in a rainy city at night',
'v1': 'cinematic, rainy Tokyo night, yellow raincoat, neon reflections',
'v2': 'BRIGHT YELLOW raincoat, facing camera, cinematic Tokyo rain night'
}
# compare_prompt_versions(prompt_variants, seed=42)النهج الطرحي
قد تصبح المطالبات متضخمة بعد تكرارات عديدة. يبدأ النهج الطرحي بمطالبة مفصلة، ثم يزيل المصطلحات واحداً تلو الآخر لمعرفة المصطلحات التي تؤدي دوراً فعلياً — وتلك التي تضيف ضوضاء.
# Start with a detailed prompt
full_prompt = (
'young woman, yellow raincoat, Tokyo, rain, neon, night, '
'street photography, cinematic, bokeh, wet pavement, '
'medium shot, warm tones, highly detailed, 8K, masterpiece, '
'award winning, beautiful, stunning, gorgeous'
)
# Remove terms and test if output quality degrades
test_removed = [
'masterpiece, award winning, beautiful, stunning, gorgeous', # quality tokens
'8K, highly detailed', # resolution tokens
'wet pavement', # specific detail
'cinematic', # style term
]
# Results typically show:
# - Generic quality tokens (masterpiece, beautiful) have minimal effect
# - Specific scene details (wet pavement, neon) matter most
# - Remove token: if output unchanged, that term is not contributing
print('Subtractive testing: remove terms and observe impact')
print('Terms that do not change output when removed can be discarded')
print('This produces lean, effective prompts')التنقيح لمعالجة أنماط فشل محددة
لكل نمط من أنماط فشل توليد الصور الشائعة إصلاحات معروفة. ويسرّع تحويل هذه المعرفة إلى قائمة تحقق منهجية عملية التنقيح.
FAILURE_FIXES = {
'Extra or deformed fingers': [
'Add to negative: extra fingers, deformed hands, bad anatomy',
'Add to positive: perfect hands, anatomically correct',
'Use inpainting to fix the specific area'
],
'Text artifacts / watermarks': [
'Add to negative: watermark, text, signature, logo',
'Increase CFG scale slightly',
'Use a different model checkpoint'
],
'Wrong style (cartoonish when photo expected)': [
'Add to negative: cartoon, anime, illustration, painted',
'Add to positive: photorealistic, DSLR, film photography',
'Use a photorealism-focused checkpoint'
],
'Background too busy / distracting': [
'Add to positive: simple background, clean background, blurred background',
'Add: shallow depth of field, bokeh background',
'Add to negative: cluttered background, busy background'
],
'Wrong color (model ignores color spec)': [
'Emphasize color: BRIGHT RED (caps), crimson red, deep scarlet',
'Add color to multiple places in prompt',
'Use img2img with a color reference image'
]
}
for failure, fixes in list(FAILURE_FIXES.items())[:3]:
print(f'\nISSUE: {failure}')
for fix in fixes:
print(f' FIX: {fix}')إدارة إصدارات المطالبات لتوليد الصور
تتبّع إصدارات المطالبات ومخرجاتها بصورة منهجية. فهذا ينشئ مكتبة مرجعية للمشروعات المستقبلية، ويكشف أنماطاً فيما ينجح مع موضوعات وأساليب محددة.
import json
from datetime import datetime
from pathlib import Path
def save_refinement_session(session_name, iterations, output_dir='prompt_sessions'):
Path(output_dir).mkdir(exist_ok=True)
session_data = {
'name': session_name,
'created': datetime.now().isoformat(),
'iterations': iterations
}
filepath = f'{output_dir}/{session_name}.json'
with open(filepath, 'w') as f:
json.dump(session_data, f, indent=2)
print(f'Session saved: {filepath}')
# Example session record
session = [
{
'version': 'v0',
'prompt': 'a woman walking in a rainy city at night',
'issues': ['too vague', 'no style', 'no lighting'],
'seed': 12345
},
{
'version': 'v1',
'prompt': 'young woman, yellow raincoat, Tokyo night rain, neon, cinematic',
'issues': ['raincoat not yellow', 'facing wrong way'],
'seed': 12345
},
{
'version': 'v2',
'prompt': 'BRIGHT YELLOW raincoat, facing camera, Tokyo rain, neon, cinematic',
'issues': [],
'seed': 12345,
'status': 'accepted'
}
]
save_refinement_session('tokyo_rain_woman', session)ميزانية التنقيح: كم عدد التكرارات؟
تتناقص فوائد التنقيح بمرور الوقت. إليك إطاراً عملياً لتحديد عدد التكرارات التي ينبغي استثمارها استناداً إلى حالة الاستخدام:
ITERATION_BUDGET_GUIDE = {
'Quick internal mockup': {
'budget': '2-3 iterations',
'goal': 'Good enough to communicate concept',
'stopping_criteria': 'Main subject correct, rough style established'
},
'Marketing asset': {
'budget': '4-6 iterations',
'goal': 'Professional quality, brand-consistent',
'stopping_criteria': 'Color, style, composition match brief exactly'
},
'Hero image / campaign visual': {
'budget': '8-12 iterations + final manual touchup',
'goal': 'Publication quality, no visible artifacts',
'stopping_criteria': 'Zero artifacts, passes creative director review'
},
'Generative art piece': {
'budget': 'Unlimited — creative exploration',
'goal': 'Discover unexpected aesthetic direction',
'stopping_criteria': 'Emotional resonance with creator\'s intent'
}
}
for use_case, guide in ITERATION_BUDGET_GUIDE.items():
print(f'{use_case}: {guide["budget"]}')
print(f' Stop when: {guide["stopping_criteria"]}')
print()تنقيح المطالبات بمساعدة LLM
استخدم LLM نصياً للمساعدة في تحليل مشكلات الصور واقتراح تحسينات للمطالبات. ويجمع ذلك بين استدلال LLM وتوليد الصور لإنشاء حلقة تنقيح فوقية.
import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
REFINEMENT_ADVISOR_PROMPT = '''I am generating an image with this prompt:
Current prompt: {current_prompt}
Negative prompt: {current_negative}
The image has these problems:
{issues}
Suggest specific changes to the prompt that would fix these problems.
Provide:
1. Modified positive prompt (full, ready to use)
2. Modified negative prompt (full, ready to use)
3. Explanation of each change
Keep your changes minimal — only fix the stated issues, do not redesign the image.'''
def get_refinement_suggestion(current_prompt, current_negative, issues):
response = client.messages.create(
model='claude-opus-4-5', max_tokens=1000,
messages=[{'role': 'user', 'content':
REFINEMENT_ADVISOR_PROMPT.format(
current_prompt=current_prompt,
current_negative=current_negative,
issues='\n'.join(f'- {i}' for i in issues)
)}]
)
return response.content[0].text
suggestion = get_refinement_suggestion(
current_prompt='a woman in a raincoat at night',
current_negative='blurry',
issues=['raincoat appears dark not yellow', 'background too busy']
)
print(suggestion[:300], '...')اختبار سريع
تريد مقارنة نسختين من مطالبة صورة لمعرفة ما إذا كان تعديلك قد حسّن النتائج. ما الذي يجب أن تبقيه ثابتاً لإجراء مقارنة عادلة؟
ملخص التنقيح التكراري
يُعد التنقيح التكراري لمطالبات الصور مهارة منهجية قابلة للتعلم:
- الحلقة: التوليد → التحليل → التعديل → إعادة التوليد
- تصنيف المشكلات: صنّف المشكلات إلى موضوع أو أسلوب أو إضاءة أو تكوين أو جودة
- البذرة الثابتة: قارن دائماً إصدارات المطالبة باستخدام البذرة نفسها
- التعديلات المستهدفة: أصلح المشكلات المحددة، ولا تعِد كتابة المطالبة بأكملها
- النهج الطرحي: أزل المصطلحات لتحديد المصطلحات التي تسهم فعلياً
- تتبّع الإصدارات: سجّل المطالبة والمشكلات والتغييرات في كل تكرار
- مراعاة الميزانية: من 2 إلى 3 تكرارات للنماذج الأولية، ومن 8 إلى 12 للأصول الرئيسية
الأسئلة الشائعة
هل درس «تحسين مطالبات الصور تكراريًا» مجاني؟
نعم — نص درس «تحسين مطالبات الصور تكراريًا» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Prompt Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.
ماذا ستتعلم في «تحسين مطالبات الصور تكراريًا»؟
تحليل الصور المُنشأة وتعديل المطالبات بطريقة منهجية تتمرن على AI Prompt Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Prompt Engineering؟
لا تُشترط خبرة سابقة. AI Prompt Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تحسين مطالبات الصور تكراريًا»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Prompt Engineering هذا؟
نعم. كل درس في AI Prompt Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تشريح مطالبة إنشاء الصور
- تحديد الأسلوب والوسيط الفني
- المطالبات السلبية والاستبعادات
- تحسين مطالبات الصور تكراريًا