-
Notifications
You must be signed in to change notification settings - Fork 690
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
Optimize QRColorMask apply_mask method for enhanced performance #372
base: main
Are you sure you want to change the base?
Conversation
This commit introduces optimizations to the apply_mask method in the QRColorMask class to improve performance. Changes include: 1. Replacing getpixel and putpixel with direct pixel manipulation using the load() method, which speeds up the process. 2. Implementing a caching mechanism to reuse color transformations for identical pixel colors, reducing redundant calculations. 3. Adding conditions to skip processing for background color pixels to reduce computational load. These optimizations have significantly reduced the method's execution time. In some experiments, these changes have resulted in performance improvements of over ten times compared to the original method, especially for larger images.
Hi! Just wanted to follow up on this PR. I believe these changes can significantly improve performance, especially for larger images. Let me know if there's anything that needs to be adjusted or clarified! |
Hi @smalyu! Thank you for this! |
Hi @maribedran! Below, I've provided a simple test case that demonstrates the improved performance of the This example generates a QR code with custom styles and measures the time it takes to create the image. You can use this code to measure the performance on your system. To compare it with the previous version, you would need to swap in the original apply_mask method and run the test again to see the difference. import time
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.colormasks import SolidFillColorMask
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
start_time = time.time()
# Create QR code object with parameters
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=100,
border=2
)
# Add data to the QR code
qr.add_data(12345678901234567890123456789012345678901234567890)
# Generate the QR code image with custom styles
qr_img = qr.make_image(
image_factory=StyledPilImage,
module_drawer=RoundedModuleDrawer(),
eye_drawer=RoundedModuleDrawer(),
color_mask=SolidFillColorMask(front_color=(239, 49, 35)),
)
# Save the image
qr_img.save("result.png")
end_time = time.time()
print(f"QR code generation time: {end_time - start_time:.4f} seconds") On my MacBook Pro M1 Pro 2021, the execution time of the original method was 20.3019 seconds, and with the proposed optimizations, it improved significantly to 1.5854 seconds. You can run this script to verify the performance improvements on your setup and see the impact of the changes firsthand. Let me know if you need further explanations or additional tests! |
… colors - Resolved cache issues in `apply_mask` by setting `use_cache` to `False` by default, preventing errors in masks where pixel position affects color (e.g., RadialGradientColorMask). - Enabled `use_cache` for `SolidFillColorMask`, as pixel position is not relevant, preserving performance where applicable.
Hi @maribedran! Thanks for reviewing this. I realized I hadn't accounted for all use cases of |
This PR is great. I am utilizing the library with basic QR code functionality. When I started using VerticalBarsDrawer with a colormask, the QR code generation process took approximately 20 seconds. This PR does improve it by making it just 1 second. |
This commit introduces optimizations to the apply_mask method in the QRColorMask class to improve performance. Changes include:
These optimizations have significantly reduced the method's execution time. In some experiments, these changes have resulted in performance improvements of over ten times compared to the original method, especially for larger images.