[Resolved] Enlarge image without distorting it

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

[Resolved] Enlarge image without distorting it

Post by firace »

What would be a good way of enlarging a simple monochrome PNG image with right angles only (such as a typical QR code) without distorting it?

I have tried this approach, which introduces heavy shape distorsion, making the QR code unusable:

Code: Select all

InitNetwork()
UsePNGImageDecoder()

imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

*queryresponse = ReceiveHTTPMemory(imageURL$)

img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

CopyImage(img, imgEnlarged)


OpenWindow(0, 50, 0, 900, 300, "")


ImageGadget(350,   0,    0,  0, 0, ImageID(img))  

ResizeImage(imgEnlarged, ImageWidth(img)*1.2, ImageHeight(img)*1.2, #PB_Image_Raw)   ;;; enlarging by 120%


ImageGadget(351,   450,  0,  0, 0, ImageID(imgEnlarged))  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 

Last edited by firace on Sun Jun 06, 2021 11:02 am, edited 1 time in total.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Enlarge image without distorting it

Post by Keya »

possibly try enlarging by 2.0 or 4.0 etc first, then reduce back to 1.2 ?
or you might need to look at the various upscaling algorithms
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Enlarge image without distorting it

Post by firace »

Nice idea! Will play around with it, thanks.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Enlarge image without distorting it

Post by BarryG »

firace wrote: Fri Jun 04, 2021 9:30 amheavy shape distorsion, making the QR code unusable
QR codes are pretty much readable by any camera, even from distorted angles. They're not meant to be 100% perfect images when read, so I doubt resizing it will be a problem. As a test, I scanned both your original and resized "distorted" code with my phone's camera and they both linked to this webpage:

https://www.example.com/cs/000036864:ba ... 805712e88d

They also showed a block of text lines in the below format. Is that what both QR codes are meant to hold? If so, your resizing works and you have nothing to worry about.

Code: Select all

|000047104:3a62faf1617ef164083c2bbf8c457b9d56eb15ec
|047937600:6e6379c5bbaf0641d3cf7bc22eda8b90bde6d495
|006839696:0b3ade6e70f36202bf358045d8f89d9bd5968802
...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Enlarge image without distorting it

Post by RASHAD »

Hi
BarryG report that there is no any problem
But just in case try the next [For Windows]

Code: Select all

InitNetwork()
UsePNGImageDecoder()

imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

*queryresponse = ReceiveHTTPMemory(imageURL$)

img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

imgH = CopyImage_(ImageID(img),#IMAGE_BITMAP	,1.2*ImageWidth(img),1.2*ImageHeight(img),0)

OpenWindow(0, 50, 0, 900, 300, "")
ImageGadget(350,   0,    0,  0, 0, ImageID(img))
ImageGadget(351,   450,  0,  0, 0, imgH)  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
Egypt my love
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Enlarge image without distorting it

Post by Saki »

Have fun

Code: Select all

; Use on Windows DPI aware, this is needed for clear output on high scalings !

EnableExplicit

InitNetwork()
UsePNGImageDecoder()

Define imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

Define *queryresponse = ReceiveHTTPMemory(imageURL$)

Define img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

Define scaling_x.d=1.2
Define scaling_y.d=1.2

Define imgEnlarged=CreateImage(#PB_Any, ImageWidth(img)*DesktopResolutionX()*scaling_x, ImageHeight(img)*DesktopResolutionY()*scaling_y)

StartVectorDrawing(ImageVectorOutput(imgEnlarged))
ScaleCoordinates(DesktopResolutionX()*scaling_x, DesktopResolutionY()*scaling_y)
DrawVectorImage(ImageID(img))
StopVectorDrawing()

OpenWindow(0, 50, 0, 900, 300, "")

ImageGadget(350,   0,    0,  0, 0, ImageID(img))  

ImageGadget(351,   450,  0,  0, 0, ImageID(imgEnlarged))  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
Last edited by Saki on Fri Jun 04, 2021 7:06 pm, edited 2 times in total.
地球上の平和
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Enlarge image without distorting it

Post by firace »

Thanks for all responses!

@BarryG, yes that's the expected output, but it seems some phones (including my own) can't always recognize the enlarged version

Saki's approach seems to help!
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Enlarge image without distorting it

Post by Saki »

Yes, it's not that easy to do.
Otherwise, you always have to increase the size by a factor of 2, otherwise it will never work,
because something always has to be added somewhere to achieve the desired size.

The usual way with the QR code is to completely recreate it in the appropriate size.
地球上の平和
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Enlarge image without distorting it

Post by BarryG »

firace wrote: Fri Jun 04, 2021 5:23 pmsome phones (including my own) can't always recognize the enlarged version
I'd say your phone needs a software update, or you should download a modern QR app that can handle codes.

As I said, QR codes are designed to be read from any angle. For example, these scan perfectly for my 3-year-old phone directly from my monitor, and without me rotating my phone to match (ie. just holding the camera vertical):

https://i.imgur.com/SM7njDO.jpg

https://i.imgur.com/K2jwKqC.jpg
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Enlarge image without distorting it

Post by Saki »

I don't know if he just wants to use that for a QR code.
It's just with a QR code that it should be as clean as possible to be readable even under bad conditions.
It also looks unprofessional, even if it still works.
地球上の平和
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Enlarge image without distorting it

Post by Tenaja »

firace wrote: Fri Jun 04, 2021 9:30 am What would be a good way of enlarging a simple monochrome PNG image with right angles only (such as a typical QR code) without distorting it?

I have tried this approach, which introduces heavy shape distorsion, making the QR code unusable:

Code: Select all

InitNetwork()
UsePNGImageDecoder()

imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

*queryresponse = ReceiveHTTPMemory(imageURL$)

img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

CopyImage(img, imgEnlarged)


OpenWindow(0, 50, 0, 900, 300, "")


ImageGadget(350,   0,    0,  0, 0, ImageID(img))  

ResizeImage(imgEnlarged, ImageWidth(img)*1.2, ImageHeight(img)*1.2, #PB_Image_Raw)   ;;; enlarging by 120%


ImageGadget(351,   450,  0,  0, 0, ImageID(imgEnlarged))  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 

Have you searched the forum? A few years back there was a very impressive enlarger that left nearly no artifacts on cartoon style images with sharp edges.

Since qr codes also have sharp edges, I would presume it would also work for you.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Enlarge image without distorting it

Post by Saki »

@Tenaja
As sample.
Imagine you have an image with 100 vertical lines with a thickness of one pixel.
Now you want to enlarge it by 20%.
Then you may only add another line to 20 lines to enlarge the image 20%.
Then you have 80 lines with a width of one pixel and 20 lines with a width of two pixels.
地球上の平和
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Enlarge image without distorting it

Post by Fig »

If you want to decode a QRCode from a picture (scan, photo...), I suggest you use ZBAR in command line for that purpose...
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
[blendman]
Enthusiast
Enthusiast
Posts: 297
Joined: Thu Apr 07, 2011 1:14 pm
Location: 3 arks
Contact:

Re: Enlarge image without distorting it

Post by [blendman] »

Hi
You could use sprite and zoomsprite (), with spritequality() set to 0.
I use it on all'my 2d applications, and it works great ;).
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Enlarge image without distorting it

Post by firace »

Thanks for all the good explanations and comments. I'm happy with the approaches suggested so I am marking this as resolved.
Post Reply