Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint errors #15

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 68 additions & 91 deletions handtype.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@
],
"source": [
"# Install the 'Pillow' library for image processing\n",
"!pip install Pillow \n",
"\n"
"!pip install Pillow\n",
"\n",
"# In environments like Jupyter Notebooks, it's sometimes necessary to\n",
"# run \"!pip install Pillow\" before imports to ensure the library is\n",
"# present. This may cause a lint warning due to unconventional ordering.\n",
"# However, this is a trade-off for functionality in such environments.\n",
"# If the library is pre-installed, the typical import order (per PEP8)\n",
"# can be maintained without issues. To suppress specific lint warnings,\n",
"# one can append \"# noqa\" to the problematic line.\n",
"import matplotlib.pyplot as plt # noqa: E402\n",
"import random as random # noqa: E402\n",
"import numpy as np # noqa: E402\n",
"from PIL import Image, ImageDraw, ImageFont # noqa: E402"
]
},
{
Expand Down Expand Up @@ -130,106 +141,79 @@
}
],
"source": [
"# Import necessary libraries for image processing\n",
"from PIL import Image, ImageDraw, ImageFont # Import modules for working with images\n",
"import matplotlib.pyplot as plt # Import the 'matplotlib' library for displaying images\n",
"import random as random # Import the 'random' module for generating random values\n",
"\n",
"# Create an instance of the Image class and open an image file\n",
"image_path = \"input_image2.jpeg\"\n",
"image = Image.open(image_path)\n",
"\n",
"# Create a new image file path where the modified image will be saved\n",
"output_path = \"output_image.jpg\"\n",
"\n",
"# Prepare the text to be written on the image (multi-line)\n",
"text = \"1. What are different techniques to gather information for software development? \\n Ans: Different techniques for software development information gathering include interviews, surveys, workshops, observations, prototypes, focus groups, document analysis, user stories, use cases, and more. These methods involve interacting with stakeholders, users, and existing systems to understand needs, preferences, and requirements, ultimately guiding the software development process. 2. List verification and validation techniques for requirements. Ans: Verification and validation techniques for requirements include reviewing documents for errors, walkthroughs with stakeholders, prototyping, simulations, traceability analysis, use case testing, model validation, acceptance testing, and gathering user feedback. These methods ensure accurate and aligned requirements also.\"\n",
"\n",
"# Specify the path to a custom TrueType Font (TTF) file and font size\n",
"font_path = \"custom_font.ttf\" # Replace with the path to your TTF font file\n",
"text = (\n",
" \"1. What are different techniques to gather information for \"\n",
" \"software development? \\n\"\n",
" \"Ans: Different techniques for software development information \"\n",
" \"gathering include interviews, surveys, workshops, observations, \"\n",
" \"prototypes, focus groups, document analysis, user stories, use cases, \"\n",
" \"and more. These methods involve interacting with stakeholders, users, \"\n",
" \"and existing systems to understand needs, preferences, and requirements, \"\n",
" \"ultimately guiding the software development process. \\n\"\n",
" \"2. List verification and validation techniques for requirements. \\n\"\n",
" \"Ans: Verification and validation techniques for requirements include \"\n",
" \"reviewing documents for errors, walkthroughs with stakeholders, \"\n",
" \"prototyping, simulations, traceability analysis, use case testing, \"\n",
" \"model validation, acceptance testing, and gathering user feedback. \"\n",
" \"These methods ensure accurate and aligned requirements also.\"\n",
")\n",
"\n",
"font_path = \"custom_font.ttf\"\n",
"font_size = 50\n",
"\n",
"# Load the custom font using the specified font file and font size\n",
"font = ImageFont.truetype(font_path, font_size)\n",
"\n",
"# Define a function to wrap text within a specified width\n",
"def wrap_text(text, font, max_width):\n",
" \n",
" # Split the text into words\n",
" words = text.split()\n",
" wrapped = [] # To store the wrapped lines of text\n",
" current_line_width=0\n",
" current_line = [] # To store words for the current line\n",
" wrapped = []\n",
" current_line_width = 0\n",
" current_line = []\n",
"\n",
" for word in words[1:]:\n",
" \n",
" # Add random spaces to simulate a justified text look\n",
" for i in range(0,random.randint(1, 3)):\n",
" word+=\" \"\n",
" for i in range(0, random.randint(1, 3)):\n",
" word += \" \"\n",
"\n",
" # Calculate the width of the word based on font size\n",
" fontsize = font.getsize(word)[1]\n",
"\n",
" # Check if adding the word exceeds the maximum width\n",
" if(current_line_width+len(word)*fontsize<=max_width):\n",
" # Add the word to the current line\n",
" current_line.append(word)\n",
" current_line_width +=len(word)*fontsize\n",
" if current_line_width + len(word) * fontsize <= max_width:\n",
" current_line.append(word)\n",
" current_line_width += len(word) * fontsize\n",
" else:\n",
" # Start a new line\n",
" current_line.append(\"\\n\")\n",
" wrapped.append(\"\".join(current_line))\n",
" current_line = []\n",
" current_line.append(word)\n",
" current_line.append(\" \")\n",
" current_line_width = 0\n",
"\n",
"\n",
" current_line.append(\"\\n\")\n",
" wrapped.append(\"\".join(current_line))\n",
" current_line = []\n",
" current_line.append(word)\n",
" current_line_width = 0\n",
"\n",
" return wrapped\n",
"# Set the desired width for the wrapped text (based on image width)\n",
"text_width = image.width *1.5\n",
" return wrapped\n",
"\n",
"# Wrap the text using the defined function\n",
"text = wrap_text(text,font,text_width)\n",
"text_width = image.width * 1.5\n",
"\n",
"# Print the wrapped text (for debugging)\n",
"print(text)\n",
"text = wrap_text(text, font, text_width)\n",
"\n",
"\n",
"# Create a drawing object for adding text to the image\n",
"draw = ImageDraw.Draw(image)\n",
"\n",
"# Determine the position to place the text\n",
"#text_width, text_height = draw.textsize(text, font)\n",
"image_width, image_height = image.size\n",
"\n",
"# Define the initial position (x, y) for adding text on the image\n",
"x = 10\n",
"y = 10\n",
"\n",
"# Set the color for the text (RGB format)\n",
"text_color = (0, 0, 50) # You can adjust the color as needed\n",
"text_color = (0, 0, 50)\n",
"\n",
"# Define the line spacing between text lines\n",
"line_spacing = 8\n",
"\n",
"# Iterate through the wrapped text and add it to the image\n",
"for i in text:\n",
" # Add the line of text to the image\n",
" draw.text((x, y), i, font=font, fill=text_color)\n",
"\n",
" # Update the y-coordinate for the next line of text\n",
" y += int(font.getsize(i)[1] + line_spacing)\n",
"\n",
" draw.text((x, y), i, font=font, fill=text_color)\n",
" y += int(font.getsize(i)[1] + line_spacing)\n",
"\n",
"# Save the modified image with the added text\n",
"image.save(output_path)\n",
"\n",
"# Display the 'image' using the 'imshow' function from the 'matplotlib.pyplot' library\n",
"plt.imshow(image)\n",
"plt.axis('off') # Turn off axes\n",
"plt.show()\n"
"plt.axis('off')\n",
"plt.show()"
]
},
{
Expand All @@ -253,46 +237,39 @@
}
],
"source": [
"# Import additional libraries/modules for image blending\n",
"from PIL import Image # Import the 'Image' module from the Pillow library\n",
"import numpy as np # Import the 'numpy' library for numerical operations\n",
"\n",
"# Load the input images\n",
"input_image_path = 'output_image.jpg' # Path to the modified image\n",
"overlay_image_path = 'overlay.jpeg' # Path to the overlay image\n",
"input_image_path = 'output_image.jpg'\n",
"overlay_image_path = 'overlay.jpeg'\n",
"\n",
"input_image = Image.open(input_image_path) # Open and load the modified image\n",
"overlay_image = Image.open(overlay_image_path) # Open and load the overlay image\n",
"# Open and load the images\n",
"input_image = Image.open(input_image_path)\n",
"overlay_image = Image.open(overlay_image_path)\n",
"\n",
"# Convert the images to numpy arrays for image blending\n",
"input_array = np.array(input_image) # Convert the input image to a numpy array\n",
"overlay_array = np.array(overlay_image) # Convert the overlay image to a numpy array\n",
"input_array = np.array(input_image)\n",
"overlay_array = np.array(overlay_image)\n",
"\n",
"# Resize the overlay image to match the size of the input image\n",
"overlay_array_resized = np.array(overlay_image.resize(input_image.size, Image.ANTIALIAS))\n",
"resize_dims = input_image.size\n",
"overlay_array_resized = np.array(\n",
" overlay_image.resize(resize_dims, Image.ANTIALIAS)\n",
")\n",
"\n",
"# Define the transparency level (alpha channel) for blending\n",
"alpha = 0.5\n",
"\n",
"# Blend the images using numpy to create the final output image\n",
"output_array = (input_array * (1 - alpha) + overlay_array_resized * alpha).astype(np.uint8)\n",
"output_array = (\n",
" input_array * (1 - alpha) + overlay_array_resized * alpha\n",
").astype(np.uint8)\n",
"\n",
"# Create a PIL image from the numpy array\n",
"output_image = Image.fromarray(output_array)\n",
"\n",
"# Save and display the final output image\n",
"output_image.save('output_image_final.jpg') # Save the final image\n",
"output_image.show() # Display the final image\n"
"output_image.save('output_image_final.jpg')\n",
"output_image.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "csPpc_Ikmi8I"
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down