Skip to content

Instantly share code, notes, and snippets.

@Opposite34
Last active October 30, 2022 19:17
Show Gist options
  • Select an option

  • Save Opposite34/97645762884abc536c9929ba14008187 to your computer and use it in GitHub Desktop.

Select an option

Save Opposite34/97645762884abc536c9929ba14008187 to your computer and use it in GitHub Desktop.
BlueHensCTF2022 - PurQRatory Solve Scripts
#edited: knew after writing this that pdfimages from poppler.utils does the same job but easier...
from pdf2image import convert_from_path
#doing this in batches so it doesn't killed itself (+ forlooping broke somehow soooo yea)
dpi = 300
path = "./PurQRatory.pdf"
pages = convert_from_path(path, dpi=500, output_folder="",last_page=26)
pg_no = 1
for page in pages:
page.save(f'./converted/qr{pg_no}.jpg', 'JPEG')
pg_no += 1
pages = convert_from_path(path, dpi=dpi, first_page=27, last_page=52)
for page in pages:
page.save(f'./converted/qr{pg_no}.jpg', 'JPEG')
pg_no += 1
pages = convert_from_path(path, dpi=dpi, first_page=53, last_page=78)
for page in pages:
page.save(f'./converted/qr{pg_no}.jpg', 'JPEG')
pg_no += 1
pages = convert_from_path(path, dpi=dpi, first_page=79, last_page=107)
for page in pages:
page.save(f'./converted/qr{pg_no}.jpg', 'JPEG')
pg_no += 1
from PIL import Image
qr_count = 0
for pg in range(1,108):
path = f"./converted/qr{pg}.jpg"
im = Image.open(path)
width, height = im.size
padding = 320
bot = height-1025
top = height/3 + 200
for i in range(4):
if i == 0:
im_cropped = im.crop((padding,padding,width/2,top))
elif i == 1:
im_cropped = im.crop((width/2,padding,width-padding,top))
elif i == 2:
im_cropped = im.crop((padding,top,width/2,bot))
else:
im_cropped = im.crop((width/2,top,width-padding,bot))
qr_count += 1
wc,hc = im_cropped.size
res_im = im_cropped.resize((int(wc/5),int(hc/5)),resample=Image.Resampling.NEAREST)
res_im.save(f"./converted/final/{qr_count}.jpg")
import cv2
path = "./converted/final"
f = open('val.txt', 'w')
for i in range(1, 428):
img=cv2.imread(f"{path}/{i}.jpg")
det=cv2.QRCodeDetector()
val, pts, st_code=det.detectAndDecode(img)
f.write(val+'\n')
f.close()
@Opposite34
Copy link
Author

Opposite34 commented Oct 30, 2022

part1 -> pdf to image
part2 -> each of the QRs in the image into it's separated thing (+ resizing so the reader works better)
part3 -> qrcode reader in opencv, was able to read all but one qrcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment