Mutlu
New member
import re
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time
import requests
from PIL import Image, ImageEnhance, ImageDraw, ImageFont
from io import BytesIO
import os
import random
# Base folder path
base_path = r"C:\Users\Kullanıcı\Desktop\20siteprojesi\1.olwechsel.de\Differential"
def sanitize_search_term(search_term):
search_term = re.sub(r'[^a-zA-Z0-9\s]', '', search_term) # Remove special characters
return search_term.strip()
def add_text_to_image(img, text, font_size=20):
draw = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("arial.ttf", font_size)
except IOError:
font = ImageFont.load_default()
text_bbox = draw.textbbox((0, 0), text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
img_width, img_height = img.size
position = (img_width - text_width - 10, img_height - text_height - 10)
draw.text(position, text, font=font, fill="white")
return img
def enhance_image(img):
brightness_factor = random.uniform(1.05, 1.15)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(brightness_factor)
sharpness_factor = random.uniform(1.05, 1.15)
enhancer = ImageEnhance.Sharpness(img)
img = enhancer.enhance(sharpness_factor)
return img
def get_random_image_url(image_urls):
return random.choice(image_urls)
def search_images(search_term, headless=True):
search_url = f"https://yandex.com/images/search?from=tabbar&text={search_term}"
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("window-size=1200x600")
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get(search_url)
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
image_urls = []
img_tags = soup.find_all('img', {'src': True})
for img in img_tags:
img_url = img['src']
if 'avatars.mds.yandex.net' in img_url:
if img_url.startswith('//'):
img_url = 'https:' + img_url
image_urls.append(img_url)
driver.quit()
return image_urls
def process_images(txt_file, headless=True):
search_term = sanitize_search_term(txt_file.replace('.txt', ''))
print(f"Searching for: {search_term}")
image_urls = search_images(search_term, headless=headless)
if not image_urls:
print(f"No images found for: {search_term}")
return
images = []
for _ in range(min(2, len(image_urls))): # Only fetch 2 images
try:
img_url = get_random_image_url(image_urls)
response = requests.get(img_url)
if 'image' in response.headers.get('Content-Type', ''):
img = Image.open(BytesIO(response.content))
img = add_text_to_image(img, "olwechsel.net")
img = enhance_image(img)
images.append(img)
else:
print(f"URL does not contain an image: {img_url}")
except Exception as e:
print(f"Error downloading image from {img_url}: {e}")
continue
if len(images) < 2:
print(f"Insufficient images for: {search_term}")
return
# Combine the 2 images into one
width, height = images[0].size
total_width = width * 2
total_height = height
combined_img = Image.new('RGB', (total_width, total_height))
combined_img.paste(images[0], (0, 0))
combined_img.paste(images[1], (width, 0))
combined_filename = f"{search_term}_combined.jpg"
combined_path = os.path.join(base_path, combined_filename)
combined_img.save(combined_path)
print(f"Saved combined image: {combined_path}")
folder_path = os.path.join(base_path, search_term)
os.makedirs(folder_path, exist_ok=True)
txt_path = os.path.join(base_path, txt_file)
os.rename(txt_path, os.path.join(folder_path, txt_file))
print(f"Moved txt file to: {folder_path}")
if os.path.exists(combined_path):
os.rename(combined_path, os.path.join(folder_path, combined_filename))
print(f"Moved combined image to: {folder_path}")
def main():
print("Selenium arka planda mı çalışsın? (1: Evet, 2: Hayır)")
choice = input("Seçiminizi yapın (1 veya 2): ").strip()
headless = choice == '1'
for txt_file in os.listdir(base_path):
if txt_file.endswith(".txt"):
process_images(txt_file, headless=headless)
time.sleep(3)
if __name__ == "__main__":
main()
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time
import requests
from PIL import Image, ImageEnhance, ImageDraw, ImageFont
from io import BytesIO
import os
import random
# Base folder path
base_path = r"C:\Users\Kullanıcı\Desktop\20siteprojesi\1.olwechsel.de\Differential"
def sanitize_search_term(search_term):
search_term = re.sub(r'[^a-zA-Z0-9\s]', '', search_term) # Remove special characters
return search_term.strip()
def add_text_to_image(img, text, font_size=20):
draw = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("arial.ttf", font_size)
except IOError:
font = ImageFont.load_default()
text_bbox = draw.textbbox((0, 0), text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
img_width, img_height = img.size
position = (img_width - text_width - 10, img_height - text_height - 10)
draw.text(position, text, font=font, fill="white")
return img
def enhance_image(img):
brightness_factor = random.uniform(1.05, 1.15)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(brightness_factor)
sharpness_factor = random.uniform(1.05, 1.15)
enhancer = ImageEnhance.Sharpness(img)
img = enhancer.enhance(sharpness_factor)
return img
def get_random_image_url(image_urls):
return random.choice(image_urls)
def search_images(search_term, headless=True):
search_url = f"https://yandex.com/images/search?from=tabbar&text={search_term}"
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("window-size=1200x600")
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get(search_url)
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
image_urls = []
img_tags = soup.find_all('img', {'src': True})
for img in img_tags:
img_url = img['src']
if 'avatars.mds.yandex.net' in img_url:
if img_url.startswith('//'):
img_url = 'https:' + img_url
image_urls.append(img_url)
driver.quit()
return image_urls
def process_images(txt_file, headless=True):
search_term = sanitize_search_term(txt_file.replace('.txt', ''))
print(f"Searching for: {search_term}")
image_urls = search_images(search_term, headless=headless)
if not image_urls:
print(f"No images found for: {search_term}")
return
images = []
for _ in range(min(2, len(image_urls))): # Only fetch 2 images
try:
img_url = get_random_image_url(image_urls)
response = requests.get(img_url)
if 'image' in response.headers.get('Content-Type', ''):
img = Image.open(BytesIO(response.content))
img = add_text_to_image(img, "olwechsel.net")
img = enhance_image(img)
images.append(img)
else:
print(f"URL does not contain an image: {img_url}")
except Exception as e:
print(f"Error downloading image from {img_url}: {e}")
continue
if len(images) < 2:
print(f"Insufficient images for: {search_term}")
return
# Combine the 2 images into one
width, height = images[0].size
total_width = width * 2
total_height = height
combined_img = Image.new('RGB', (total_width, total_height))
combined_img.paste(images[0], (0, 0))
combined_img.paste(images[1], (width, 0))
combined_filename = f"{search_term}_combined.jpg"
combined_path = os.path.join(base_path, combined_filename)
combined_img.save(combined_path)
print(f"Saved combined image: {combined_path}")
folder_path = os.path.join(base_path, search_term)
os.makedirs(folder_path, exist_ok=True)
txt_path = os.path.join(base_path, txt_file)
os.rename(txt_path, os.path.join(folder_path, txt_file))
print(f"Moved txt file to: {folder_path}")
if os.path.exists(combined_path):
os.rename(combined_path, os.path.join(folder_path, combined_filename))
print(f"Moved combined image to: {folder_path}")
def main():
print("Selenium arka planda mı çalışsın? (1: Evet, 2: Hayır)")
choice = input("Seçiminizi yapın (1 veya 2): ").strip()
headless = choice == '1'
for txt_file in os.listdir(base_path):
if txt_file.endswith(".txt"):
process_images(txt_file, headless=headless)
time.sleep(3)
if __name__ == "__main__":
main()